4

I have a custom module that outputs some images. Now, I would like to render them properly.

Currently, I use theme() function, kinda similarly to the way shown here1 and here1:

echo theme('image_style', array(
  'path' => $image['uri'],
  'style_name' => 'product_page_thumbnail',
  'attributes' => array(
    'alt' => $image['ImageAlt'],
    'title' => $image['ImageTitle'],
  )
));

Now, the theme() docs says:

Avoid calling this function directly. It is preferable to replace direct calls to the theme() function with calls to drupal_render() by passing a render array with a #theme key to drupal_render(), which in turn calls theme().

So how would I do that?

I know renderable array for links, it's as simple as:

$link = array(
  '#type'    => 'link',
  '#title'   => $title,
  '#href'    => 'http://example.com',
  '#options' => array('html' => TRUE, 'title' => 'link'),
);

So I want the same, but for images. Somehow I can't find any documented example of it.


1Internal Stack Exchange links, just for reference.

  • I'm in the process of experimenting now, as search failed me miserably, resulting with either direct calls to theme() I want to avoid, or with even more crude way of directly concatenating parts of <img> tag in tpl file, what I want to avoid even more. – Mołot Dec 19 '13 at 8:34
  • ha Molot your question is dup now drupal.stackexchange.com/questions/68381/… – Bala Dec 19 '13 at 8:49
  • 2
    @Mołot I don't think that there is an image element.. Because theme_image() function itself uses somekind of $attributes['src'] = file_create_url($variables['path']) and return '<img' . drupal_attributes($attributes) . ' />' lines... – xurshid29 Dec 19 '13 at 9:07
  • 1
    $render = array( '#theme' => 'image', '#path' => $file->uri, ); return render($render); ? – xurshid29 Dec 19 '13 at 9:22
  • 1
    @milkovsky usually the more specific questions are closed as duplicates of more generic ones, or ones with lower scores as duplicates of higher ones. Age does not matter much, unless one question was asked recently. But oh well. I won't lose any rep if this will get closed, or anything. In 2013 your vote might really help me ;) – Mołot Jun 9 '15 at 14:12
13

There's nothing specific for images, but there is for any theme function. Your old code using theme() would be equivalent to:

$build = array(
  '#theme' => 'image_style',
  '#path' => $image['uri'],
  '#style_name' => 'product_page_thumbnail',
  '#alt' => $image['ImageAlt'],
  '#title' => $image['ImageTitle'],
);

Basically you pass the named variables for the theme function as #-prefixed properties, and the render system will do the rest.

  • 2
    Thanks, copy-paste worked, now I just need to move things where they belong :) – Mołot Dec 19 '13 at 9:49
  • 5
    I believe in Drupal 8, #path has now become #uri – Nic Jun 7 '16 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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