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.

JavaScript in <strong>Drupal</strong><br />

The keys to the array, to find what you're looking for, are dependent on the type of<br />

JavaScript that was added.<br />

• File and external JavaScript is identified by the path to the file that was<br />

passed in for its location.<br />

• Settings are available at $javascript['settings'].<br />

• Inline JavaScript is not easily identifiable. Each time an inline script is<br />

added to a page it is added with a numeric value starting at 0. If two inline<br />

scripts were added to a page, they would be added at $javascript[0] and<br />

$javascript[1]. The numbers are based on the order in which they were<br />

added to a page, which is unreliable.<br />

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

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

Altering CSS<br />

CSS can be altered in a similar manner to JavaScript using hook_css_alter(). An<br />

array containing all the CSS about to be added to the page is passed though<br />

hook_css_alter() as a last opportunity to make changes before the CSS is rendered<br />

into HTML and added to the page. An example use of the hook_css_alter() would<br />

be to remove the system.css file provided by core, as seen here:<br />

/**<br />

* Implements hook_css_alter().<br />

*/<br />

function example_css_alter(&$css) {<br />

unset($css[drupal_get_path('module', 'system') . '/system.css']);<br />

}<br />

Identifying items in the array passed in for CSS files is similar to JavaScript files. The<br />

key for internal and external files is the path to the file used when it was originally<br />

added. Like inline JavaScript, inline CSS is added using numeric keys in the order<br />

the CSS was added.<br />

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

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

[ 300 ]

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

Saved successfully!

Ooh no, something went wrong!