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.

[...]
    $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>

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?

okapi
 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi okapi,

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()).
$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>
            <?php
            $photoFileObject = $item->getEntry()->getPhotoTeacher();
            if (is_object($photoFileObject)) {
                $photoFileObjectVersion = $photoFileObject->getVersion();
                $photoRelativePath = $photoFileObjectVersion->getRelativePath();
                ?>
                <div>
                    <img src="<?php echo $photoRelativePath; ?>">
okapi replied on at Permalink Reply
okapi
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!
regans replied on at Permalink Reply
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.
stewblack23 replied on at Permalink Reply
stewblack23
Thanks. Just what I was looking for in a project I'm working on.