18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Working with Files and Images<br />

);<br />

}<br />

return $form;<br />

Effect form callbacks return partial Form API structures. You only need to provide<br />

the fields you would like users to enter, and <strong>Drupal</strong> will take care of the submission<br />

details. We have defined two form elements to hold our configuration options,<br />

namely, text_color and text. In general these are pretty standard Form API<br />

arrays, but there a couple of things to point out.<br />

Your form callback also takes a single argument—$data—which is an associative<br />

array containing the current values of our form options, if any. You should use this<br />

information to fill your form elements with the user's current options or, if they are<br />

empty, to fill in sane defaults. We have set the default color here to be white, and the<br />

default text to <strong>Drupal</strong> loves kittens!<br />

One final thing to note is that we are using the #element_validate property to<br />

specify an array of callbacks, which will be used to validate this specific element.<br />

This is a Form API property we haven't seen before, but you can use it in any form<br />

element definition. <strong>Drupal</strong> contains the function image_effect_color_validate()<br />

to verify that hex colors are properly entered, and we take advantage of this function<br />

by specifying it here.<br />

Now let's make a customized message based on the user's options, to be displayed<br />

in the image style's effect listing. This just allows us to give a little more detail to the<br />

user, and makes it more obvious what our effect is doing.<br />

/**<br />

* Theme callback for image watermark effect summary output.<br />

*<br />

* @param $variables<br />

* An associative array containing configuration data.<br />

*/<br />

function theme_watermark_summary($variables) {<br />

$data = $variables['data'];<br />

return t('with color @textcolor', array('@textcolor' => $data['text_<br />

color']));<br />

}<br />

As you can see we just return the text we're using along with the color that<br />

has been configured. This gets appended to the label we specified in our initial<br />

effect declaration.<br />

[ 336 ]

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

Saved successfully!

Ooh no, something went wrong!