Friday, January 14, 2011

Six new Hooks in Drupal 7

1. hook_node_load

- Act on nodes being loaded from the database. This hook is invoked during node loading. This hook should only be used to add information that is not in the node or node revisions table, not to replace information that is in these tables. For performance reasons, information for all available nodes should be loaded in a single query where possible.


2. hook_block split in to 3 hooks as below:

2.1 hook_block_info - Define all blocks provided by the module. In hook_block_info(), each block your module provides is given a unique identifier referred to as "delta" (the array key in the return value). Delta values only need to be unique within your module.

function training_block_info() {
$blocks['customblock1']['info'] = 'Block Created using Training Module';
$blocks['customblock1']['cache'] = DRUPAL_NO_CACHE;
return $blocks;
}

2.2 hook_block_view - Return a rendered or renderable view of a block.

function training_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'customblock1':
$block['subject'] = 'Custom Block1';
$block['content'] = 'Content Created by Custom Block';
break;
}
return $block;
}

2.3 hook_block_configure - Return a rendered or renderable view of a block.

function training_block_configure($delta = '') {
switch($delta) {
case 'customblock1':
$form['customlement'] = array(
'#type' => 'select',
'#title' => t('Number of items to display'),
'#options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
);
return $form;
}
}

3. hook_css_alter

- Alter CSS files before they are output on the page.

function hook_css_alter(&$css) {
// Remove defaults.css file.
unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
}

4. hook_drupal_goto_alter

- Change the page the user is sent to by drupal_goto().

function hook_drupal_goto_alter(&$path, &$options, &$http_response_code) {
$http_response_code = 500;
}

5. hook_user_view

- The user's account information is being displayed. The module should format its custom additions for display and add them to the $account->content array.

function training_user_view($account, $view_mode, $langcode) {
$account->content['summary']['blog'] = array(
'#type' => 'user_profile_item',
'#title' => t('Blog11'),
'#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
'#attributes' => array('class' => array('blog')),
);
}

5. hook_node_view

- Act on a node that is being assembled before rendering. The module may add elements to $node->content prior to rendering. This hook will be called after hook_view().

function training_user_view($account, $view_mode, $langcode) {
$account->content['summary']['blog'] = array(
'#type' => 'user_profile_item',
'#title' => t('Blog11'),
'#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
'#attributes' => array('class' => array('blog')),
);
}

6 comments:

bangalore web design said...

Really very interesting article. I learnt so many things from your post. I look forward more and more post from you. Thanks for sharing this useful article!

web design company said...

That was really interesting to know about drupal and its significance.Thanks for ur post !

Firman Way said...

thanks for info,....

extendcode said...

Really, I am very exiting to know about Drupal and its signification.Good information..
software development india

Unknown said...
This comment has been removed by the author.
Unknown said...

it was very intresting actually this post contains major useful informations and also as well as you can find more details at Drupal CMS