Wednesday, January 5, 2011

Changes in hook of Drupal 7

Changes in hooks of Drupal7

1.hook_nodeapi, hook_node_type, hook_user, and hook_block removed and replaced with families of related functions

In Drupal 7, several hooks that previously allowed modules to do a group of related operations, with a $op argument, were split into individual hooks for each operation. Gábor posted a note in the developer mailing list about this if you'd like to read more about it.

The hooks that were changed in this way:

hook_nodeapi() in Drupal 6 became (see Node API page) hook_node_delete(), hook_node_insert(), hook_node_load(), hook_node_prepare(), hook_node_prepare_translation(), hook_node_search_result(), hook_node_presave(), hook_node_update(), hook_node_update_index(), hook_node_validate(), and hook_node_view().

hook_node_type() in Drupal 6 became (see Node API page) hook_node_type_delete(), hook_node_type_insert(), andhook_node_type_update().

hook_user() in Drupal 6 became (see User API page) hook_user_categories(), hook_user_insert(), hook_user_load(), hook_user_login(), hook_user_logout(), hook_user_presave(), hook_user_update(), and hook_user_view().

hook_block() in Drupal 6 became (see Block API page) hook_block_info(), hook_block_configure(), hook_block_save(), and hook_block_view().

The "form" and "register" operations have been removed. Use hook_form_alter instead.

Here are some examples - Drupal 6.x:

/**
* Implements hook_nodeapi().
*/
function book_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'load':
// Load a book.
}
}
/**
* Implements hook_node_type().
*/
function mymodule_node_type($op, $info) {
switch ($op){
case 'delete':
// Delete, insert or update according to $op.
}
}
/**
* Implements hook_user().
*/
function block_user($type, $edit, &$account, $category = NULL) {
switch ($type) {
case 'form':
// Construct the user form.
}
}
/**
* Implements hook_block().
*/
function system_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
// List blocks.
}
}
?>

Drupal 7.x:
/**
* Implements hook_node_load().
*/
function book_node_load($nodes, $types) {
// Load a book.
}
/**
* Implements hook_user_form().
*/
function block_user_form(&$edit, &$account, $category = NULL) {
// Construct the user form.
}

/**
* Implements hook_block_info().
*/

function system_block_info() {
// List blocks.
}

/**
* Implements hook_node_type_delete().
*/

function mymodule_node_type_delete($info) {
variable_del('comment_' . $info->type);
}

/**
* Implements hook_node_update().
*/

function hook_node_update($node) {
db_update('mytable')
->fields(array('extra' => $node->extra))
->condition('nid', $node->nid)
->execute();
}

/**
* Implements hook_node_insert().
*/

function hook_node_insert($node) {
db_insert('mytable')
->fields(array(
'nid' => $node->nid,
'extra' => $node->extra,
))
->execute();
}
?>

2. hook_perm() renamed to hook_permission()

3.hook_footer() was removed, $closure became $page_bottom, $page_top added

4.hook_filter() and hook_filter_tips() replaced by hook_filter_info()
hook_filter() has been removed and replaced with hook_filter_info(), to allow modules to declare the input filters they provide using an associative array (hook_menu-alike registry structure), consistently with others info hooks in core.hook_filter_tips() is not required any more, since a 'tips callback' is provided in the hook_filter_info declaration, and used to generate filter tips.

No comments:

Post a Comment