Skip to content

Instantly share code, notes, and snippets.

@carwin
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carwin/f72fb8a0c0d372736bdc to your computer and use it in GitHub Desktop.
Save carwin/f72fb8a0c0d372736bdc to your computer and use it in GitHub Desktop.
D7: Using the Picture module in code
<?php
/**
* Helper function.
*
* Prepares the necessary array for use with theme_picture.
*
* @param array $variables
* The image array loaded from field_get_items.
*
* @param string $group
* The breakpoint group to use for breakpoint mappings.
*
* @return array
* A fully prepared picture array.
*/
function your_module_prepare_picture($variables, $group) {
$mappings = picture_mapping_load($group);
$picture_arr = array(
'item' => $variables[0],
'uri' => $variables[0]['uri'],
'height' => $variables[0]['metadata']['height'],
'width' => $variables[0]['metadata']['width'],
'breakpoints' => $mappings->mapping,
'style_name' => 'original',
);
return $picture_arr;
}
// Usage in say... hook_preprocess_node().
function your_module_preprocess_node(&vars) {
$some_img_field = field_get_items('node', $node_obj, 'field_some_field_name');
$some_img_array = your_module_prepare_picture($some_img_field, 'breakpointgroup');
if (!empty($some_img_array)) {
$variables['some_image'] = theme('picture', $some_img_array);
}
}
// Now in your template:
<?php print render($some_image); ?>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment