Show customer logos on an easier way?

Hello.

I have some customer logos that I want to show at two places. The logos should be linked to the customers url.

  1. At the “customers” page within a grid.
  2. Five random logos at the start page.

My first thought was, to create an invisible page for every customer and then fetch the image and the url of every page.

But: Is there a smarter way to do that?
And: How can I display random logos at the start page?

Thanks so far.

You could use a structure field instead of subpages.

For random content, you can use the shuffle() and limit() methods.

Thanks! The structure field is working so far – except of one thing:
Images I selected over the type “image” are not displayed in the frontend.
There is only a broken link-image.

This is my yml-structure:

  customers:
    label: Kunden
    type: structure
    entry: >
      {{customerlogo}}<br />
      {{url}}
    fields:
      customerlogo:
        label: Kundenlogo
        type: image
      url:
        label: URL
        type: url

And that’s my PHP:

                <?php foreach($page->customers()->toStructure() as $customers): ?>
                    <div class="col-md-4 col-sm-6">
                        <a href="<?php echo $customers->url() ?>" target="_blank">    
                            <img class="img-responsive" src="<?php echo $customers->customerlogo()->url() ?>">   
                        </a>
                    </div>
                <?php endforeach ?>

This should be

<?php echo $customers->customerlogo()->toFile()->url() ?>

Edit: The reason for this is that the field value contains the filename only. So you have to convert that to a file object first, in order to be able to use the url() method on it.

Try the following:

<?php foreach($page->customers()->toStructure() as $customers): ?>
    <div class="col-md-4 col-sm-6">
        <a href="<?php echo $customers->url() ?>" target="_blank">    
            <?php if($logo = $customers->customerlogo()->toFile()): ?>
            <img class="img-responsive" src="<?php echo $logo->url() ?>">
            <?php endif ?>
        </a>
    </div>
<?php endforeach ?>

Edit: @texnixe was faster. My solution includes a check if the file exists, but if you make the image field required, you don’t need that necessarily.

Well, the check makes sense, because if the file is removed after the filename was saved to the file, it might still result in an error.

1 Like

Woah. That was really fast. :slight_smile:
Thanks for that.