Drupal Blocks:What Changed in D6 and D7

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值