Error with echo $media->value('dcterms:date')->asHtml();

Hi,
To be able to add the date to the caption of an image, i have done the following:

  1. Installed the Numeric Data Types module
  2. Created a ‘Photo’ template, which besides the Dublin Core Titel also has the Dublin Core Date as a value
  3. Set the data type of the Date value to Numeric|Timestamp

To display the caption beneath the photo (media), I use the following:

echo $media->displayTitle(); ?><p>Date: <?php echo $media->value('dcterms:date')->asHtml();

This works fine for all photos where the Date has been set. I however get the following error for those photos where no Date has been set and the value is empty.

Error: Call to a member function asHtml() on null in /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/themes/thedaily/view/omeka/site/item/show.phtml:35
Stack trace:
#0 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-view/src/Renderer/PhpRenderer.php(505): include()
#1 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-view/src/View.php(206): Laminas\View\Renderer\PhpRenderer->render()
#2 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-view/src/View.php(235): Laminas\View\View->render()
#3 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-view/src/View.php(199): Laminas\View\View->renderChildren()
#4 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-mvc/src/View/Http/DefaultRenderingStrategy.php(104): Laminas\View\View->render()
#5 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-eventmanager/src/EventManager.php(321): Laminas\Mvc\View\Http\DefaultRenderingStrategy->render()
#6 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-eventmanager/src/EventManager.php(170): Laminas\EventManager\EventManager->triggerListeners()
#7 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-mvc/src/Application.php(366): Laminas\EventManager\EventManager->triggerEvent()
#8 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/vendor/laminas/laminas-mvc/src/Application.php(347): Laminas\Mvc\Application->completeRequest()
#9 /mnt/web119/d1/39/51980039/htdocs/www_whitcombs/index.php(21): Laminas\Mvc\Application->run()
#10 {main}

I removed ->asHtml() and now it works fine again for those photos where no Date has been set. However for the photos where the Date has been set, it renders it as Date: 1975-10-28 instead of Date: October 28, 1975, which is the format I would like it to be.

Any way to resolve this issue?

You need to add a conditional to your code. Something like:

<?php $dateValue = $media->value('dcterms:date'); ?>
<?php echo $media->displayTitle(); ?><p>Date: <?php echo $dateValue ? $dateValue->asHtml() : '[no date]';
1 Like

Thanks a lot, that worked great!

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.