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.

Creating Your First <strong>Module</strong><br />

Here's our 'block info' hook implementation declaring a single block:<br />

/**<br />

* Implements hook_block_info().<br />

*/<br />

function first_block_info() {<br />

$blocks = array();<br />

$blocks['list_modules'] = array(<br />

'info' => t('A listing of all of the enabled modules.'),<br />

'cache' => DRUPAL_NO_CACHE,<br />

);<br />

}<br />

return $blocks;<br />

Once again, this function is preceded by a doc block. And since we are writing<br />

a trivial implementation of hook_block_info(), we needn't add anything other<br />

than the standard documentation.<br />

An implementation of hook_block_info() takes no arguments and is expected<br />

to return an associative array.<br />

Associative arrays: <strong>Drupal</strong>'s data structure of choice<br />

Arrays in PHP are very fast. They are well supported, and because they<br />

serve double duty as both indexed arrays and dictionary-style associative<br />

arrays, they are flexible. For those reasons <strong>Drupal</strong> makes heavy use of<br />

arrays—often in places where one would expect objects, linked lists,<br />

maps, or trees.<br />

The returned array should contain one entry for every block that this module declares,<br />

and the entry should be of the form $name => array($property => $value).<br />

Thus, the important part of our function above is this piece:<br />

$blocks['list_modules'] = array(<br />

'info' => t('A listing of all of the enabled modules.'),<br />

'cache' => DRUPAL_NO_CACHE,<br />

);<br />

[ 44 ]

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

Saved successfully!

Ooh no, something went wrong!