18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11<br />

Pretty cool right? However, sometimes it would be nice to be able to ship some<br />

preconfigured image styles with your module. For instance, a popular <strong>Drupal</strong><br />

contributed module is the Pirate module, which translates your entire website to<br />

pirate-speak on Talk Like A Pirate Day. It might be cool for the Pirate module to<br />

be able to ship a totally preconfigured image style that adds the YAR! watermark<br />

to images. Rather than having to create this image style themselves, administrators<br />

could simply install the module and it would be all set up for them. Not only that,<br />

but because this style is in code, it is more easily deployable between development<br />

environments. Thankfully, this is quite easy to do and will provide our final<br />

demonstration of <strong>Drupal</strong> 7's Image API.<br />

Creating image styles from a module<br />

At this point in your <strong>Drupal</strong> experience, you can probably guess what is involved<br />

in creating an image style in your module—a hook and an associative array. The<br />

hook is hook_image_default_styles() and the array is a simple definition of the<br />

components of your style.<br />

For those familiar with views in <strong>Drupal</strong> 6, this is very similar to exporting<br />

views to code using hook_views_default_view().<br />

/**<br />

* Implements hook_image_default_styles().<br />

*/<br />

function watermark_image_default_styles() {<br />

$styles = array();<br />

$styles['yar'] = array(<br />

'effects' => array(<br />

array(<br />

'name' => 'watermark',<br />

'data' => array(<br />

'text_color' => '#000000',<br />

'text' => 'YAR!'<br />

),<br />

'weight' => 0,<br />

),<br />

),<br />

);<br />

return $styles;<br />

}<br />

[ 339 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!