Express - custom template: How to show image/file attribute as image?
Permalink 1 user found helpful
I want to have a list of teachers with photo and some information about the person.
So i have created an express data object with several text attributes and one image/file attribute.
Now i'm trying to modify view.php of the express_entry_list block as a custom template for my list.
These text attributes are displayed correctly on a page by the express_entry_list block, but as soon as i'm trying to add the image/file type attribute to my custom template in the same way as i have added the text attributes:
i'm getting an error:
What am i doing wrong? How do i get the URL of an image from an image attribute in a custom template?
So i have created an express data object with several text attributes and one image/file attribute.
Now i'm trying to modify view.php of the express_entry_list block as a custom template for my list.
[...] $results = $result->getItemListObject()->getResults(); if (count($results)) { foreach ($result->getItems() as $item) { ?> <div class="sprechstliste-item"> <div><?php echo $item->getEntry()->getNameTeacher(); ?></div> <div><?php echo $item->getEntry()->getSubjectTeacher(); ?></div> <div><?php echo $item->getEntry()->getConsultHour(); ?></div> </div> <?php } ?> <?php if ($pagination) { ?> <?=$pagination ?> <?php } ?> <?php } else { ?> <p><?=t('No "%s" entries can be found', $entity->getName())?></p>
Viewing 15 lines of 17 lines. View entire code block.
These text attributes are displayed correctly on a page by the express_entry_list block, but as soon as i'm trying to add the image/file type attribute to my custom template in the same way as i have added the text attributes:
<div><img src="<?php $item->getEntry()->getPhotoTeacher()->getUrl(); ?>" alt=""></div>
i'm getting an error:
Call to a member function getUrl() on null
What am i doing wrong? How do i get the URL of an image from an image attribute in a custom template?
Yes...!! Works perfectly!
Can't thank you enough for your precious help! I would never have figured this out by myself.
I wish there were a collection of plain code examples for Express templates like the one you're generously providing here. I'm not sure if everybody knows how to make an image display from an attribute... Maybe not only for me this whole Express thing seems so mighty and useful, but is also quite challenging!
Can't thank you enough for your precious help! I would never have figured this out by myself.
I wish there were a collection of plain code examples for Express templates like the one you're generously providing here. I'm not sure if everybody knows how to make an image display from an attribute... Maybe not only for me this whole Express thing seems so mighty and useful, but is also quite challenging!
Thanks @MrKDilkington. I spend a very long time trying to figure this out from the documentation and ended up compromising by using a regex to extract the URL. This is a much better solution.
Thanks. Just what I was looking for in a project I'm working on.
I believe the problem is "$item->getEntry()->getPhotoTeacher()->getUrl()" returns a File object (Concrete\Core\Entity\File\File) or null. The File class does not have getUrl() available.
Instead I think you can get the File Version, then get the relative path (or full URL using getURL()).