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 todrupal_render()
by passing a render array with a #theme key todrupal_render()
, which in turn callstheme()
.
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.
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.$attributes['src'] = file_create_url($variables['path'])
andreturn '<img' . drupal_attributes($attributes) . ' />'
lines...$render = array( '#theme' => 'image', '#path' => $file->uri, ); return render($render);
?