Wrap a link around an image in a render array

Published by Lennart Van Vaerenbergh on July 31, 2015

When you want to make an image clickable and you're dealing with a render array, these are the properties to add to it. Mind the highlighted lines.
As long as you're dealing with the theme function 'image_formatter', you can use the #path property to wrap a link around it.
Because I didn't find it in a snap on the web, I'm posting it here, not to forget.
 
<?php
    $build['image'] = array(
      '#theme' => 'image_formatter',
      '#item' => array(
        'uri' => 'misc/druplicon.png',
      ),
      '#path' => array(
        'path' => 'node/1',
        'options' => array('html' => TRUE), // Optional.
      ),
    );
?>
Source: http://data.agaric.com/wrapping-link-around-renderable-array-images-drupal-7

 

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This challenge is for testing whether or not you are a human visitor and to prevent automated spam submissions.