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')),
);
}

Thursday, January 13, 2011

Five New Features of Drupal 7 .info file

1. Drupal 7 now supports a dynamic-loading code registry.To support it, all modules must now declare any code files containing class or interface declarations in the .info file


files[] = example.test

2. As of version Drupal 7.x, the path of the module's (main) configuration page can be given in .info file. This will be the path of the "Configure" link for this particular module on the modules overview page.


configure = admin/config/example

3. As of version Drupal 7.x, modules and themes may specify that they should not be visible on the modules page by adding hidden = TRUE.


4. As of version Drupal 7.x, modules and themes may specify that they are absolutely required and should never be disabled by adding required = TRUE. These modules will be enabled automatically during install. In most cases it should only be used with the Drupal core required modules (e.g. Node, User, etc.).


5. Stylesheets can be included in drupal 7 .info in below format:

stylesheets[all][] = sample.css

Drupal 7 page Layout

The Whole drupal rendering page is now controlled by html.tpl.php and not page.tpl.php as in drupal 6. So to override it copy "html.tpl.php" from system drupal module (Core) and paste it in default theme to override the whole page layout of drupal 7.

But make sure the cache(admin/settings/performance) is cleared after the files are updated in theme.

To override the overlay popup of admin pages, make sure you copy the html.php to theme named "seven".