Skip to content

Instantly share code, notes, and snippets.

@jamiemagique
Last active February 27, 2019 15:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiemagique/e3063fa7cb1ea163a11c to your computer and use it in GitHub Desktop.
Save jamiemagique/e3063fa7cb1ea163a11c to your computer and use it in GitHub Desktop.
Drupal 7 - Render an image using the picture module mappings programatically (via preprocess_node() in this gist)
// Get image settings array from a field on the node
$image = field_get_items('node', $node, 'INSERT_IMAGE_FIELD_MACHINE_NAME');
// Picture settings
$picture_mapping = picture_mapping_load('INSERT_PICTURE_MAPPING_MACHINE_NAME');
$fallback = 'INSERT_IMAGE_STYLE_MACHINE_NAME';
$breakpoints = picture_get_mapping_breakpoints($picture_mapping, $fallback);
// Create a new variable to pass through to the node template
$variables['INSERT_NEW_VARIABLE_NAME_TO_PASS_TO_NODE_TEMPLATE'] = theme('picture', array('uri' => $image[0]['uri'], 'style_name' => $fallback, 'breakpoints' => $breakpoints));
@MrUpsidown
Copy link

Well done and very useful! Thanks

@Nikro
Copy link

Nikro commented Dec 3, 2016

Thanks! :)

@angarsky
Copy link

It saved my time! Thanks!

@akerbel
Copy link

akerbel commented Jan 16, 2018

Thanks a lot!

@al77ex1
Copy link

al77ex1 commented May 24, 2018

It's great! Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment