Blocks in Drupal 6 (and 5) always felt a little less loved thanother portions of Drupal. While the node and theming APIs offeredstraightforward and often elegant methods to modify behaviourwithout modifying other peoples' code, blocks weren't so fortunate.Lacking API support, working with blocks often meant writing uglycode. Last week helping with the Drupal Block API documentation Idiscovered this has changed in Drupal 7. I thought I'd share what Ilearned.
The information I'll discuss is a summary of the block informationfrom:
Converting 6.x modules to 7.x (http://drupal.org/node/224333)
Converting 6.x modules to 7.x - Categorical(http://drupal.org/node/394070)
Block API Documentation: block.api.php
Examples Module: block_examples.module (From: Examples forDevelopers project)
hook_block() has been split up
The Drupal 7 Block API's most noticeable change is the splitting ofhook_block():
hook_block_info() replaces hook_block($op = 'list)
hook_block_view() replaces hook_block($op = 'view')
hook_block_configure() replaces hook_block($op = 'configure')
hook_block_save() replaces hook_block($op = 'save')
Hopefully gone are those sometimes awkward select statements.
Hook Alters
The Drupal 7 Block API now has alter hooks! These new hook providea clean way to modify other blocks in the system:
hook_block_info_alter()
This hook allows you to change a block definition before it's savedto the database.
hook_block_list_alter()
This hook allows you to add, remove or modify block definitions inthe block list. This happen before the blocks are rendered, so youcan even override the contents of the block. This hook can replacethe use of db_rewrite_sql() or hook_query_alter() for altering theblocks list.
hook_block_view_alter()
This hook allows you to modify the contents of any block returnedby hook_block_view().
hook_block_view_MODULE_DELTA_alter
A convenience hook of sorts, this hook allows you modify thecontents of a specific block rather than implementinghook_block_view_alter() and testing every call for the block ofinterest.
$delta module ID is text
The unique block ID within a module is known as the block "delta"and it's now a string instead of an integer. Blocks can now begiven meaningful names. For example, instead of being identified as1, a block might be identified as "index" or "groups". CSS classesand theming suggestions use this name as well, improvingreadability.
If you are a module maintainer be sure to read the notes onupdating your block configuration from Drupal 6 to Drupal 7.There's a helper function provided.
Renderable Arrays as Output
Blocks should now return their output as renderable arrays. Forexample:
$block['content'] = array('#markup' => t('This is mystring of text to display'));
This is part of an overall Drupal 7 change and not specific toblocks. Strings will continue to work, but a renderable array isstrongly recommended.
Database Table Changes
The block tables have been renamed:
'blocks' renamed to 'block'
'blocks_roles' renamed to 'block_role'
'boxes' renamed to 'block_custom'
Block Module Optional
Although enabled by default, the block module is not required andcan be disabled. If you write code that relies on the block modulebe sure to declare it as a dependency in your module .info file orwrap the code in a statement that verifies the block module isenabled. For example: if (module_exists('block')) {...}
Permission String Change
The permission string "use PHP for settings" is replaced by "usePHP for block visibility".
Cache Constants Renamed
The BLOCK_CACHE_ prefix for cache constants is nowDRUPAL_CACHE_.
See http://api.drupal.org/api/function/hook_block_info/7 for thelist of constants.
The information I'll discuss is a summary of the block informationfrom:
Converting 6.x modules to 7.x (http://drupal.org/node/224333)
Converting 6.x modules to 7.x - Categorical(http://drupal.org/node/394070)
Block API Documentation: block.api.php
Examples Module: block_examples.module (From: Examples forDevelopers project)
hook_block() has been split up
The Drupal 7 Block API's most noticeable change is the splitting ofhook_block():
hook_block_info() replaces hook_block($op = 'list)
hook_block_view() replaces hook_block($op = 'view')
hook_block_configure() replaces hook_block($op = 'configure')
hook_block_save() replaces hook_block($op = 'save')
Hopefully gone are those sometimes awkward select statements.
Hook Alters
The Drupal 7 Block API now has alter hooks! These new hook providea clean way to modify other blocks in the system:
hook_block_info_alter()
This hook allows you to change a block definition before it's savedto the database.
hook_block_list_alter()
This hook allows you to add, remove or modify block definitions inthe block list. This happen before the blocks are rendered, so youcan even override the contents of the block. This hook can replacethe use of db_rewrite_sql() or hook_query_alter() for altering theblocks list.
hook_block_view_alter()
This hook allows you to modify the contents of any block returnedby hook_block_view().
hook_block_view_MODULE_DELTA_alter
A convenience hook of sorts, this hook allows you modify thecontents of a specific block rather than implementinghook_block_view_alter() and testing every call for the block ofinterest.
$delta module ID is text
The unique block ID within a module is known as the block "delta"and it's now a string instead of an integer. Blocks can now begiven meaningful names. For example, instead of being identified as1, a block might be identified as "index" or "groups". CSS classesand theming suggestions use this name as well, improvingreadability.
If you are a module maintainer be sure to read the notes onupdating your block configuration from Drupal 6 to Drupal 7.There's a helper function provided.
Renderable Arrays as Output
Blocks should now return their output as renderable arrays. Forexample:
$block['content'] = array('#markup' => t('This is mystring of text to display'));
This is part of an overall Drupal 7 change and not specific toblocks. Strings will continue to work, but a renderable array isstrongly recommended.
Database Table Changes
The block tables have been renamed:
'blocks' renamed to 'block'
'blocks_roles' renamed to 'block_role'
'boxes' renamed to 'block_custom'
Block Module Optional
Although enabled by default, the block module is not required andcan be disabled. If you write code that relies on the block modulebe sure to declare it as a dependency in your module .info file orwrap the code in a statement that verifies the block module isenabled. For example: if (module_exists('block')) {...}
Permission String Change
The permission string "use PHP for settings" is replaced by "usePHP for block visibility".
Cache Constants Renamed
The BLOCK_CACHE_ prefix for cache constants is nowDRUPAL_CACHE_.
See http://api.drupal.org/api/function/hook_block_info/7 for thelist of constants.