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:
<?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):
<? if (isset($item->websitelink)) : ?>
<p><? $item->website->display(array('class' => 'blog-address-url', 'itemprop' => 'url')); ?></p>
<? endif ?>


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
cmscss
View Replies:
cmscss replied on at Permalink Reply
cmscss
Sorry, can't seem to edit the typos above - hopefully you get what I mean.
theblockery replied on at Permalink Reply
theblockery
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:
<?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
theblockery replied on at Permalink Reply
theblockery
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
cmscss replied on at Permalink Reply 1 Attachment
cmscss
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.

<?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)) : ?>
cmscss replied on at Permalink Reply
cmscss
Sorry mate, I'm not setting the variables within the foreach loop!
theblockery replied on at Permalink Reply
theblockery
No worries. If you run into any other problems please don't hesitate to write back.

-Jordan
cmscss replied on at Permalink Reply
cmscss
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:
<? 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
theblockery replied on at Permalink Reply
theblockery
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:
<?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
cmscss replied on at Permalink Reply
cmscss
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?
<?php
    defined('C5_EXECUTE') or die(_("Access Denied."));
    foreach ($controller->getRepeatingItems() as $item) :
        $name = $item->company_name->getText();
?>
  <? if (!empty($name)) : ?>
    <h4 class="blog-address-name" itemprop="name"><?= $name; ?></h4>
    <hr>
  <? endif ?>
<? endforeach ?>

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?
theblockery replied on at Permalink Reply
theblockery
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 :)
cmscss replied on at Permalink Reply
cmscss
Thanks Jordan, very, very helpful (as always) - thanks heaps.

concrete5 Environment Information

# concrete5 Version
5.6.3.1

# concrete5 Packages
Advanced Forms (1.13.1), Area Splitter (2.1), Designer Content (3.1.1), Designer Content Pro (1.2.2), Designer Content Pro - Custom Block Storage (1.0), Gallery (1.0), Page Selector Attribute (1.1), Pro Blog (12.4.3).

# concrete5 Overrides
blocks/trackback, blocks/figure, blocks/problog_date_archive, blocks/box, blocks/page_list, blocks/problog_list, blocks/latest_comments, blocks/form, blocks/related_page_list, blocks/content, blocks/autonav, blocks/user_blog_list, blocks/search, blocks/icon_holder, blocks/gallery, blocks/tags, blocks/sixeightforms, blocks/html, blocks/related_pages, blocks/matogertel_area_splitter, blocks/image, languages/ar, languages/tr_TR, languages/he_IL, languages/es_MX, languages/es_PE, languages/zh_TW, languages/sl_SI, languages/nb_NO, languages/it_IT, languages/da_DK, languages/es_ES, languages/sv_SE, languages/pt_PT, languages/vi_VN, languages/ro_RO, languages/zh_CN, languages/ja_JP, languages/fi_FI, languages/es_AR, languages/el_GR, languages/ru_RU, languages/nl_NL, languages/fa_IR, languages/de_DE, languages/sk_SK, languages/fr_FR, languages/cs_CZ, single_pages/blogsearch.php, themes/vila-mourisca

# concrete5 Cache Settings
Block Cache - Off
Overrides Cache - Off
Full Page Caching - Off

# Server Software
Apache/2.2.22 (Debian)

# Server API
apache2handler

# PHP Version
5.4.4-14+deb7u14

# PHP Extensions
apache2handler, apc, bcmath, bz2, calendar, Core, ctype, curl, date, dba, dom, ereg, exif, fileinfo, filter, ftp, gd, gettext, hash, iconv, imagick, json, libxml, mbstring, mcrypt, mhash, mysql, mysqli, openssl, pcre, PDO, pdo_mysql, Phar, posix, Reflection, session, shmop, SimpleXML, soap, sockets, SPL, standard, sysvmsg, sysvsem, sysvshm, tokenizer, wddx, xml, xmlreader, xmlwriter, zip, zlib.

# PHP Settings
max_execution_time - 360
apc.max_file_size - 1M
log_errors_max_len - 1024
max_file_uploads - 20
max_input_nesting_level - 64
max_input_time - 360
max_input_vars - 1000
memory_limit - 256M
post_max_size - 64M
sql.safe_mode - Off
upload_max_filesize - 20M
mysql.max_links - Unlimited
mysql.max_persistent - Unlimited
mysqli.max_links - Unlimited
mysqli.max_persistent - Unlimited
pcre.backtrack_limit - 1000000
pcre.recursion_limit - 100000
session.cache_limiter - nocache
session.gc_maxlifetime - 7200
soap.wsdl_cache_limit - 5

Browser User-Agent String

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.