Date attribute within composer

Permalink
Hi - I'm using a date attribute within composer, but I'm having troubles having it display on the actual page. I have it displaying properly in the blog page list with this:
$date = $page->getAttribute('date_time');
echo date('F j, Y', strtotime($date));

But that doesn't work when I place it in an html block within composer.

Any ideas?

Thanks
Kurtopsy

Kurtopsy
 
hutman replied on at Permalink Reply
hutman
1) You can not run PHP code within the HTML block, you need a PHP Code Block (http://www.concrete5.org/marketplace/addons/simple-php-block/ orhttp://www.concrete5.org/marketplace/addons/code-blocks/).... Even from the PHP Code Block I'm not sure you can access Concrete5 Page Attributes. If you are using Concrete5.7 you can setup Page Attributes to show on Page Types through the Dashboard.
2) If you are trying to get the attribute value of the current page you need to use $c not $page
Kurtopsy replied on at Permalink Reply
Kurtopsy
Thanks hutman. I am using 5.6 and I tried to use the php block and to no avail. Must not be able to call page attributes in composer.
hutman replied on at Permalink Reply
hutman
I think the issue is that you can't call Page Attributes from blocks, not really anything to do with Composer.

One cheat that I have done before is to create a Custom Template for the Content/HTML block that has the desired code in it, and then add a blank block of that type with the Custom Template to your page where you want the attribute to display. If you do that you will need to put this at the top of your Custom Template code

$c = Page::getCurrentPage();
Kurtopsy replied on at Permalink Reply
Kurtopsy
Good idea! It sort of worked. It's displaying a date, but not the date in my attribute. Using the same php as above, but replaced $page with $c; outputted December 31, 1969.
hutman replied on at Permalink Reply
hutman
Can you provide the code that you have in your Custom Template?
Kurtopsy replied on at Permalink Reply
Kurtopsy
<?php defined('C5_EXECUTE') or die("Access Denied."); 
$c = Page::getCurrentPage();
?>
<div id="HTMLBlock<?php echo intval($bID)?>" class="HTMLBlock">
<?php echo $content; ?>
<span>
   <i class="fa fa-calendar"></i>
   <?php $date = $c->getAttribute('date_time'); echo date('F j, Y', strtotime($date)); ?>
</span>
</div>
hutman replied on at Permalink Reply
hutman
And you have this block added to the page which has the attribute with handle 'date_time' set, correct?
Kurtopsy replied on at Permalink Reply
Kurtopsy
Correct.
hutman replied on at Permalink Reply
hutman
I guess I'm not sure, this should work. You will just need to put some echo or var_dump statements in there to see what your variables are returning and see where the disconnect is I think.
Kurtopsy replied on at Permalink Reply
Kurtopsy
Sounds good. Thanks so much for all your help.
Kurtopsy replied on at Permalink Reply
Kurtopsy
I got it to work, so I figured I'd share. Instead of using the date attribute, I simply called the timestamp from composer. I added the following to the page type that is assigned to composer.
$date = date('F j, Y', strtotime($c->getCollectionDatePublic()));
echo $date;