1

I am trying to render a responsive picture module image from within a module. I cant find any functions that I can pass the picture mapping into. If I render the whole node from within the module it works fine but if I just render the image it does not work. Any thoughts would be great.

3 Answers 3

2

Here is my solution to build the render array of the drupal 7 picture module v2

//Load picture renderable array

$picture_name = 'mapping_name'
$fallback_image_style = 'large';

$image = $node->field_image[LANGUAGE_NONE][0];

$picture_mappings = picture_mapping_load($picture_name);

$breakpoint_styles = picture_get_mapping_breakpoints($picture_mappings, $fallback_image_style);

$picture = array(
        '#theme' => 'picture',
        '#width' => isset($image['width']) ? $image['width'] : NULL,
        '#height' => isset($image['height']) ? $image['height'] : NULL,
        '#style_name' => $fallback_image_style,
        '#breakpoints' => $breakpoint_styles,
        '#uri' => $image['uri'],
        '#alt' => isset($image['alt']) ? $image['alt'] : '',
        '#attributes' => isset($image['attributes']) ? $image['attributes'] : NULL,
        '#timestamp' => $image['timestamp'],
      );

print render($picture);
1
  • Note the $image value if you only have a system path you can use image_get_info('public:image/in/files.png')
    – Duncanmoo
    Nov 12, 2015 at 10:24
1

You could use the field_view_field() function.
eg.

$output = field_view_field('node', $node, 'field_name');

read more about the correct way to render node fields at this blog post

1
  • I have tried this but it does not give the Picture module responsive version of the image, just the original.
    – pete80
    Sep 22, 2014 at 11:45
1

Adding 'default' to the functions arguments sorted it out

$field = field_view_field('node', $node, 'field_image', 'default');

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