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 10<br />

From here onwards, when we want to use the hello world library, we would add it<br />

with the following call:<br />

drupal_add_library('hello_world', 'hello_world_library');<br />

The first argument is the module that defined the library and the second argument is<br />

the key for the library defined.<br />

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

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

Altering information in hook_library<br />

<strong>Drupal</strong> provides a hook_library_alter() function, where modules can intercept<br />

the libraries defined by hook_library() and either act or make changes to them.<br />

A simple example could be another module providing a more recent version<br />

of the hello_world.js script. For the example the module will be called Hello<br />

World Update. In the file hello_world_update.module we have the following<br />

implementation of hook_library_alter():<br />

/**<br />

* Implements hook_library_alter().<br />

*/<br />

function hello_world_library_alter(&$libraries, $module) {<br />

if ($module == 'hello_world' &&<br />

isset($libraries['hello_world_library'])) {<br />

// Verify existing version is older than the one we are<br />

// updating to.<br />

if (version_compare($libraries['hello_world_library']['version'],<br />

'2.0', '

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

Saved successfully!

Ooh no, something went wrong!