I need to preprocess an image to output into my drupal 7 theme. We are using the picture module, but I am having issues figuring out how I can output the correct
markup.
I found reference to a theme_picture function for d8 (which I assume is basically the same for 7). This is what I have so far:
$vars['banner'] = theme_picture(array('breakpoints'=>array('600'), 'style_name'=>'medium', 'uri'=>$vars['node']->field_banner_image_tall['und'][0]['uri']));
However, I don't think I am setting up the breakpoinsts/style_name variables correctly. I assume there is a way to correlate a breakpoint with a style_name? Could someone point me to or provide an example for how to accomplish this?
Thanks!
Comments
Comment #1
tragic.rich commentedIn case anyone is also trying to do this, I *finally* figured this out! Instead of trying to re-architect my breakpoints by hand I found some handy helper functions in the picture.module file:
Load the picture map via your mapping computer name, in this case:'banner_'
Pass the picture map to the picture_get_mapping_breakpoints to get your breakpoints array:
Then pass the breakpoints to theme_picture:
And voila:
Comment #2
sgdev commentedThanks for sharing the formatting for this. I had been working through deciphering the proper options for the theme_picture function, and this helped greatly.
A couple of comments to further help people who visit this post. I'm not sure if there was a change in versions newer than Picture 7.x-2.7, but the picture_mapping_load format mentioned in post #1 does not work in 7.x-2.9. It should be without the underscore, otherwise the function returns an error:
Also while it is possible to render the function using theme_picture, I believe the recommended Drupal format is to use the theme function. Therefore in the example presented in #1, it would be formatted as follows:
Hope this helps others!
Comment #3
sgdev commentedForgot to update that my comment applies to 7.x-2.9...
Comment #4
tragic.rich commentedThanks for the updated example @ron_s!
Comment #5
brunorios1 commentedThanks @tragic.rich and @ron_s.
This should be in the documentation =)
Comment #6
DrCord commentedThank you for these posts, this went from a deep mystery to a reasonable problem thanks to you folks.
This really should be in the documentation!!