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.

Chapter 10<br />

The second argument can contain an array of options to be used including group,<br />

scope, and preprocess. Once CSS is added, with a weight of CSS_THEME set to not<br />

preprocess, it will look like:<br />

$options = array(<br />

'type' => 'inline',<br />

'group' => CSS_THEME,<br />

'preprocess' => FALSE,<br />

);<br />

drupal_add_css("body { color: #ffffff; }", $options);<br />

API Documentation for drupal_add_css() is available at<br />

http://api.drupal.org/api/function/drupal_add_css/7<br />

Using the Library API<br />

<strong>Drupal</strong> 7 provides a Library API where libraries and plugins of JavaScript and CSS<br />

can be defined and programmatically added later. jQuery and the other libraries<br />

provided with <strong>Drupal</strong> are all defined using the system modules implementation of<br />

hook_library(). We can use farbtastic, a jQuery color picker included with <strong>Drupal</strong>,<br />

as an example. Included in farbtastic is a JavaScript file and a CSS file. To add<br />

farbtastic to a page it would look as follows:<br />

drupal_add_library('system', 'farbtastic');<br />

This will not only add JavaScript and CSS, but will also add any dependent libraries.<br />

An example of this can be seen in the overlay module where drupal_add_library()<br />

adds the overlay. A call is made to add the parent overlay JavaScript that looks<br />

like this:<br />

drupal_add_library('overlay', 'parent');<br />

In the function overlay_library(), parent is set to have jQuery BBQ and jQuery<br />

UI core as dependencies to the overlay parent. This means those two libraries<br />

will be added to the page before the overlay. <strong>Drupal</strong> knows the library chain of<br />

dependencies, so you don't have to.<br />

API documentation about drupal_add_library() is available at<br />

http://api.drupal.org/api/function/drupal_add_library/7.<br />

[ 295 ]

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

Saved successfully!

Ooh no, something went wrong!