3

I have this array in PHP

$link = array(
    '#type'    => 'link',
    '#title'   => 'link1',
    '#href'    => 'http://www.link1.example',
    '#suffix'  => '<br />',
  );

when i call the render() functions i get the correct html

<a href="http://www.link1.example">link1</a>

How i can change the above render array in order to include an image as a link?

In other words i want this as output from the render() function

<a href="http://www.link1.example"><img src='images/link1img.png' /></a>

I have tried to use the theme('image',$array) function but when the result of theme() is passed to the render() i don't see the image to my page but the actual html code.

0

2 Answers 2

4

The pre-render function for a 'link' element type is drupal_pre_render_link()...looking at the docs, you can pass these parameters:

  • #title: The link text to pass as argument to l().
  • #href: The URL path component to pass as argument to l().
  • #options: (optional) An array of options to pass to l().

You can force a link prepared through l() to display HTML by passing 'html' => TRUE as an option, so you should be able to do the same through the render array:

$image = theme('image', array('path' => 'images/link1img.png'));

$link = array(
  '#type'    => 'link',
  '#title'   => $image,
  '#href'    => 'http://www.link1.example',
  '#options' => array('html' => TRUE, 'title' => 'link1'),
  '#suffix'  => '<br />',
);
2
  • I tried the above code with no luck. The problem is that in my browser i get that &lt;img typeof=&quot;foaf:Image&quot; src=&quot;localhost/drupal/images/link1img.png&quot; alt=&quot;&quot; /&gt; It's like the htmlspecialchar() PHP functions runs to my code and does not permit the img to appear!
    – user16194
    Apr 2, 2013 at 7:33
  • I forgot to set the $options. OK now! :)
    – user16194
    Apr 2, 2013 at 7:44
1

Better maybe to create a single render array which optionally can be altered through out the render process, ie.:

$image = array(
  '#theme' => 'image_formatter',
  '#item' => array(
    'uri' => '/images/link1img.png',
  ),
  '#suffix'  => '<br />',
  '#path' => array(
    'path' => 'http://www.link1.example',
    'options' => array(
      'attributes' => array(
        'title' => 'link1',
      ),
    ),
  ),
);

renders into:

<a href="http://www.link1.example" title="link1"><img typeof="foaf:Image" src="/images/link1img.png" alt="" /></a><br />

If the image has been uploaded using the Field or File module, image_formatter can also be used to apply an image style, ie. thumbnail:

$image = array(
  '#theme' => 'image_formatter',
  '#image_style' => 'thumbnail',
  '#item' => array(
    'uri' => 'public://images/link1img.png',
  ),
  '#suffix'  => '<br />',
  '#path' => array(
    'path' => 'http://www.link1.example',
    'options' => array(
      'attributes' => array(
        'title' => 'link1',
      ),
    ),
  ),
);

renders into:

<a href="http://www.link1.example" title="link1"><img typeof="foaf:Image" src="http://DOMAIN.TLD/sites/SITENAME/files/styles/thumbnail/public/field/images/link1img.png?itok=Bo4ZpxnG" alt="" /></a><br />

For more info, see https://api.drupal.org/api/drupal/modules!image!image.field.inc/function/theme_image_formatter/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.