Drupal Module Block Functions

Administrator's picture

Drupal Module Block Functions

To enable your Drupal module content to display as a block, then the block hook must be used. This function can take 3 parameters - $op, $delta and $edit.

$op is set to one of four values, which tell Drupal what to expect from the function. The possible values are list, view, configure and save. 'List' is passed when module information is needed, like on the administrator's module listing page. For displaying actual content via the module, 'view' must be used. The 'configure' and 'save' values are used by admin users to configure the block.

Here is some example code showing how these parameters are passed in the hook_block function.

function modulename_block($op=’list’, $delta=0, $edit=array()) { switch ($op) { case ‘list’: $blocks[0][‘info’] = t(‘My Test Module Name’); return $blocks; case ‘view’: $blocks[‘subject’] = t(‘Test Module Title’); $blocks[‘content’] = t(‘Main block content goes here’); } }

When the list case is selected, the information provided is placed into an array of arrays which contains information about how the block should be used. The ‘info’ field in the example is required, but other information can be placed in here, such as a default region or weight. This is what the multi-dimensional array looks like:

array( [0] => ( ‘info’ => ‘My Test Module Name’ ) )

The view case assigns values to the block display. In this case it will have a title of ‘Test Module Title’ and the block content will display ‘Main block content goes here’.

The $delta function parameter is used when the module provides more than one block – the value of $delta (usually an integer, although it doesn’t have to be) decides which block to return. As this simple example only has one block, this is set to 0.

Finally, the $edit parameter is used when a block is being configured by an admin. When their changes are saved (and therefore $op=’save’) then $edit will contain an array with the submitted configuration changes. Otherwise $edit is ignored – in this example it is set as an empty array, but can be left out entirely.

65 people like this