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.

Working with Files and Images<br />

This is about as simple as <strong>Drupal</strong> hooks get, especially given everything that<br />

has already been covered previously in this chapter. Our implementation of<br />

hook_image_default_styles() returns $styles, an associative array of image<br />

style definitions, keyed on a unique string. This string will act as the style's name<br />

and will be displayed on the image styles admin page.<br />

Each style definition is a set of image effects, along with their effect-specific settings,<br />

if any. The keys this array can contain are as follows:<br />

• name: The unique name of the effect we're implementing.<br />

• data: An optional array of configuration settings. These should be keyed on<br />

the same text string that are used in the form callback defined by the effect.<br />

Note that default values defined in the form callback will NOT be honored if<br />

you do not include them. You will have to enter them by hand here.<br />

• weight: An optional weight, used to control effect ordering when multiple<br />

effects are included in a single style. As is <strong>Drupal</strong> standard, lower weights<br />

'float to the top' and higher weights 'sink to the bottom'.<br />

That is really all there is to it. If you load the image styles listing, you will now see<br />

your style listed (you may need to clear the <strong>Drupal</strong> cache first).<br />

One nice aspect about image styles defined by a module is that they are protected<br />

from being changed or modified by end users (or accidentally broken by the<br />

developers themselves). This is implemented in two ways. First, in order to modify<br />

the settings of a style implemented through hook_image_default_styles(), you<br />

must specifically take the extra step of saying you want to override it. Take a look<br />

at the following screen:<br />

[ 340 ]

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

Saved successfully!

Ooh no, something went wrong!