How tog et separate fields for url and link text to control markup?
Permalink Browser Info Environment
Hey Mate,
I'm output the href and text fields separately? When I try to grab it using the code from your site like this:
I see the following error where line 5 is the code above:
I was also trying to use the C5 helper like this (which doesn't work):
Not sure what I've missed sorry - any help or pointers in the right direction would be much appreciated.
Cheers
Ben
I'm output the href and text fields separately? When I try to grab it using the code from your site like this:
<?php $href_website = $item->websitelink->getHref(); $text_website = $item->websitelink->getText(); ?> <p><a class="blog-address-url" itemprop="url" href="<?= $href_website; ?>"><? $text_website; ?></a></p>
I see the following error where line 5 is the code above:
Fatal error: Call to a member function getHref() on a non-object in /path/to/website/blocks/dcp_address/view.php on line 5
I was also trying to use the C5 helper like this (which doesn't work):
Not sure what I've missed sorry - any help or pointers in the right direction would be much appreciated.
Cheers
Ben
Type: | Discussion |
---|---|
Status: | In Progress |
Sorry, can't seem to edit the typos above - hopefully you get what I mean.
Hi Ben,
I'm not sure if the code you posted is the entire view.php file, or just a snippet.
If that's the whole thing, then you're missing the "for" loop that goes through the repeating items. So you'd need to do something like this instead:
If, however, you are already doing that (and just left it out of your code sample), then the problem is most likely a typo with the field name... I would double-check that your field handle is "websitelink" (maybe it has an underscore in it, or different capitalization)?
If you've checked both of those things and are still having the problem, then I would need to look more closely at your site. So let me know if the above suggestions work. If not, we can take further steps. One way or another though I will get this working for you!
Thanks,
Jordan
EDIT: Fixed typo in my code sample
I'm not sure if the code you posted is the entire view.php file, or just a snippet.
If that's the whole thing, then you're missing the "for" loop that goes through the repeating items. So you'd need to do something like this instead:
<?php foreach ($controller->getRepeatingItems() as $item) { $href_website = $item->websitelink->getHref(); $text_website = $item->websitelink->getText(); ?> <p><a class="blog-address-url" itemprop="url" href="<?= $href_website; ?>"><?= $text_website; ?></a></p> <?php } ?>
If, however, you are already doing that (and just left it out of your code sample), then the problem is most likely a typo with the field name... I would double-check that your field handle is "websitelink" (maybe it has an underscore in it, or different capitalization)?
If you've checked both of those things and are still having the problem, then I would need to look more closely at your site. So let me know if the above suggestions work. If not, we can take further steps. One way or another though I will get this working for you!
Thanks,
Jordan
EDIT: Fixed typo in my code sample
Hi Ben,
Just in case you're only looking at your email notifications... I had a small typo in my prior code sample and have since updated it, so check the actual forum page for the sample code (not just your email notification).
-Jordan
Just in case you're only looking at your email notifications... I had a small typo in my prior code sample and have since updated it, so check the actual forum page for the sample code (not just your email notification).
-Jordan
Thanks mate,
I'm not sure what I'm doing wrong sorry - it will be basic I'm sure. Here's the full template plus an attached screenshot of the fields from the c5 dashboard.
I'm not sure what I'm doing wrong sorry - it will be basic I'm sure. Here's the full template plus an attached screenshot of the fields from the c5 dashboard.
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); // separate href from link text to output separately $href_website = $item->websitelink->getHref(); $text_website = $item->websitelink->getText(); $href_tripadvisor = $item->tripadvisor->getHref(); $text_tripadvisor = $item->tripadvisor->getText(); ?> <?php foreach ($controller->getRepeatingItems() as $item): ?> <div class="blog-address"> <? if (isset($item->company_name)) : ?> <h4 class="blog-address-name" itemprop="name"><? $item->company_name->display(); ?></h4> <hr> <? endif ?> <? if (isset($item->websitelink)) : ?>
Viewing 15 lines of 43 lines. View entire code block.
Sorry mate, I'm not setting the variables within the foreach loop!
No worries. If you run into any other problems please don't hesitate to write back.
-Jordan
-Jordan
Sorry Jorden, I always get confused with how to output markup only if there's content - for example:
This code outputs markup even if there's nothing in the field:
I realise this is outside your support scope! But after searching and reading, I'm even more confused as to why !empty doesn't work in this instance.
Any tips would be much appreciated.
Cheers
This code outputs markup even if there's nothing in the field:
<? if (!empty($item->phone)) : ?> <span class="blog-address-phone"><i class="fa fa-phone"></i> <a itemprop="tel" href="tel:<? $item->phone->display(); ?>"><? $item->phone->display(); ?></a></span> <? endif ?>
I realise this is outside your support scope! But after searching and reading, I'm even more confused as to why !empty doesn't work in this instance.
Any tips would be much appreciated.
Cheers
Hi Ben,
This is totally in the scope of support, and I agree that it is quite confusing. (I have an update that I am very close to releasing that will address this problem, btw).
The reason that "empty()" doesn't work as you'd expect is because that variable is a php object which itself contains lots of different values and functions (for example, if it's a link it has ->getHref() and ->getText())... and as far as PHP is concerned if an object exists then it is not empty (even if the values inside of it are empty).
So, to check if the value is empty or not, you need to just do an "if" statement around the value you want to check. For example:
(Note that this assumes it's a textbox or textarea field. If it were a link field, you'd check "if ($item->my_field->getHref()) { ... }" , etc).
Like I said, it can be quite confusing and I'm working on simplifying it (but for the time being you'll need to do something like the code I have above).
Hope that helps, and please don't hesitate to ask more questions as they arise!
-Jordan
This is totally in the scope of support, and I agree that it is quite confusing. (I have an update that I am very close to releasing that will address this problem, btw).
The reason that "empty()" doesn't work as you'd expect is because that variable is a php object which itself contains lots of different values and functions (for example, if it's a link it has ->getHref() and ->getText())... and as far as PHP is concerned if an object exists then it is not empty (even if the values inside of it are empty).
So, to check if the value is empty or not, you need to just do an "if" statement around the value you want to check. For example:
<?php if ($item->phone->getText()) : ?> <!-- output markup here... --> <?php endif; ?>
(Note that this assumes it's a textbox or textarea field. If it were a link field, you'd check "if ($item->my_field->getHref()) { ... }" , etc).
Like I said, it can be quite confusing and I'm working on simplifying it (but for the time being you'll need to do something like the code I have above).
Hope that helps, and please don't hesitate to ask more questions as they arise!
-Jordan
Awesome, thanks Jordan - I sort of understand: If I ever see -> then the code is calling a method so I need to be sure the right method is being called?
e.g ->display(); is not pulling the data from the field, it's trying to display it so I can't check if it's empty?
Anyway, is there anything wrong with using the getText method once and assigning it to a variable at the top, then outputting?
That way I'm using ->getText once and there's also less code in the markup - or is that inefficient because I'm assigning all the fields (14) to variables instead of just displaying them?
e.g ->display(); is not pulling the data from the field, it's trying to display it so I can't check if it's empty?
Anyway, is there anything wrong with using the getText method once and assigning it to a variable at the top, then outputting?
That way I'm using ->getText once and there's also less code in the markup - or is that inefficient because I'm assigning all the fields (14) to variables instead of just displaying them?
I don't see anything wrong with putting the name into a variable first and then checking it. I don't think there will be a noticeable different in terms of performance either way (maybe if there were thousands or millions of records it would matter, but DCP would be the wrong tool in that case anyway). So I'd go with whatever makes the code easier for you to read and work with. As the old saying goes "CPU time is cheap, developer time is expensive" :)
As for the empty / methods / ->function stuff... well it's kind of complicated and depends on the situation. Whenever you see the "arrow" (->), it just means you're referring to something that is inside another object. But you could have a function or a variable in the object (the difference would be that a function must have the parentheses after the name, but a variable does not have parentheses).
In the case of the "display()" function, I think you're pretty much correct. That function actually never "returns" anything, it only "echo"'s things out to the browser. Whereas the other functions like ->getHref() and ->getText() actually return the values and it's up to you to "echo" them out to the browser. This is super confusing and I'm actually doing away with the "display()" function in the next version (it will still be there for backwards compatibility, but it won't be recommended)... But the point is that the reason these differences exist isn't so much because it has to be that way in PHP, but rather because of how I set up the objects and functions and variables for this addon. As time goes on I am trying to simplify it, so hopefully soon it will be way less confusing (and much better documented).
Hope that clears things up more than it confuses them :)
As for the empty / methods / ->function stuff... well it's kind of complicated and depends on the situation. Whenever you see the "arrow" (->), it just means you're referring to something that is inside another object. But you could have a function or a variable in the object (the difference would be that a function must have the parentheses after the name, but a variable does not have parentheses).
In the case of the "display()" function, I think you're pretty much correct. That function actually never "returns" anything, it only "echo"'s things out to the browser. Whereas the other functions like ->getHref() and ->getText() actually return the values and it's up to you to "echo" them out to the browser. This is super confusing and I'm actually doing away with the "display()" function in the next version (it will still be there for backwards compatibility, but it won't be recommended)... But the point is that the reason these differences exist isn't so much because it has to be that way in PHP, but rather because of how I set up the objects and functions and variables for this addon. As time goes on I am trying to simplify it, so hopefully soon it will be way less confusing (and much better documented).
Hope that clears things up more than it confuses them :)
Thanks Jordan, very, very helpful (as always) - thanks heaps.