BLOCKS IN DRUPAL
By :Shailesh Hule [email protected]
What is Block?
Blocks are snippets of text or functionality that usually live outside the main content area of a web site, such as in the left or right sidebars, in the header, in the footer, and so on.
“ Block are not nodes and don’t follow the same rules nodes do .“
Define Block
We can define block in two ways
1:Drupal web interface. The block configuration page is located at
Administer > site building > Blocks 2:Programmatically using Block API.
Control Block
About the block we can control the following option:
1:Region placement.
2:User specific visibility settings.
3:Role specific visibility settings.
4:Page specific visibility settings.
Block Region
First step to create block: Define Region
It is not actual first step because it is about block region.
You can use default regions or we create new region.
How to create new region?
Block region defined in template.php file which is placed in theme folder.
Path: drupal\themes\theme_name
code for regions in template.php filefunction themename_regions() {return array('left' => t('left sidebar'),'right' => t('right sidebar'),'content_top' => t('content top'),'content_bottom' => t('content bottom'),'header' => t('header'),'footer' => t('footer'));}
How To Define Block?
Second Step: Block Defining
Blocks are defined within modules by using hook_block(), and a module can implement multiple blocks within this single hook. Once a block is defined, it will be shown on the block administration page. Additionally, a site administrator can manually create custom blocks through the web interface.
Database schema for Block
• Block properties for every block are stored in the blocks table.
• Additional data for blocks created from within the block configuration interface, such as their content and input format type, are stored in the boxes table.
• Lastly, blocks_roles stores the role-based permissions for each block.
• module: This column contains the name of the module that defined the block.
• delta: Because modules can define multiple blocks within hook_block(), the delta column stores a key for each block that’s unique only for each implementation of hook_block().
• theme: Because blocks can be defined for multiple themes, Drupal needs to store the name of the theme for which the block is enabled.
• status: This tracks whether the block is enabled. A value of 1 means that it’s enabled, while 0 means it’s disabled. When a block doesn’t have a region associated with it, Drupal sets the status flag to 0.
• throttle: When the throttle module is enabled, this column tracks which blocks should be throttled.0 indicate throttle disable,1 indicate it is enable.
Again come to hook_block()…..
Signature of hook_block()
function hook_block( $op = 'list', $delta = 0, $edit = array())
Parameter List:
$op :This parameter defines the phases a block passes through.
list: Return an array of all blocks defined by the module. Array keys are the delta.
Possible list values, and their defaults, follow:
info: This value is required. A translatable string
provides a description of the block suitable for site administrators.
status: Is the block enabled—TRUE or FALSE? Default is FALSE.
region: The default region is left.
weight: This controls the arrangement of a block when displayed within its region. A block with a heavy weight will sink to the bottom or to the right of theregion. The default weight is 0.
pages: Defines the default pages the node should be visible on.
custom: TRUE means this is a custom block and FALSE means that it’s a block implemented by a module.
title: The default block title.
configure: Return a form array of fields for block-specific settings. This is merged with the overall form on the block configuration page.
save: Called when the configuration form is submitted.
view: The block is being displayed. Return an array containing the block’s title and content.
$delta This is ID of the block to return. You can use an integer or a string value for $delta. Note that $delta is ignored when the $op state is list.
$edit When $op is save, $edit contains the submitted form data from the block configuration form.
Building a BlockLet’s create a new module named module_name.module to
hold our block code. Create a new folder named module_name within sites/all/modules/custom (you might need to create the modules and custom folders if they don’t exist). Next, add module_name.info to the folder:
; $Id$
name = Module_name
description = Blocks for facilitating pending content workflow.
version = "$name$"
Then, add module_name.module as well:<?php// $Id$/*** @file* Implements various blocks to improve pending content
workflow.*/Once you’ve created these files, enable the module via
Administer > Site building >Modules
Let’s add our block hook and implement the list operation /** * Implementation of hook_block(). */
function molulename_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('Pending comments'); return $blocks; } }
You should now see your block listed on the block overview page.
See following code which create configuration form for our
Block. Add this code in maodule_name.module file.
case 'configure':
$form['approval_block_num_posts'] = array(
'#type' => 'textfield',
'#title' => t('Number of pending comments to display'),
'#default_value' => variable_get(‘modulename_block_num_posts', 5),
);
return $form;
Block configuration form with the block’s custom fields appended
When the block configuration form shown in previous is submitted, it will trigger the next $op, which is 'save'. You’ll use this next phase to save the value of the form field:
case 'save':
variable_set(‘modulename_block_num_posts', (int) $edit['approval_block_num_posts']); break;
Finally, add the 'view' operation and return a list of pending comments when the block is viewed:
case 'view ': // Retrieve the number of pending comments to display that
// we saved earlier in the 'save' op, defaulting to 5.
if (user_access('administer comments')) {
$num_posts = variable_get(‘modulename_block_num_posts', 5);
$result = db_query_range('SELECT c.* FROM {comments} c WHERE c.status = %d ORDER BY c.timestamp', COMMENT_NOT_PUBLISHED, 0,$num_posts);
// Preserve our current location so user can return after editing. $destination = drupal_get_destination();
$items = array();
while ($comment = db_fetch_object($result)) {
$items[ ] = l($comment->subject, 'node/'. $comment->nid, array(),NULL, 'comment-'. $comment->cid). ' '.
l(t('[edit]'), 'comment/edit/'. $comment->cid, array(), $destination);}
$block['subject'] = t('Pending comments');
// We theme our array of links as an unordered list.
$block['content'] = theme('item_list', $items);
}
return $block;
}
}
//close this function….
Following code shows who to implements more than one block in hook function.
function modulename_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('Pending comments'); //first block $blocks[1]['info'] = t('Unpublished nodes'); //second block return $blocks; } }
case 'configure':
// Only in block 0 (the Pending comments block) can one// set the number of comments to display. if ($delta == 0) { //for first block $form['approval_block_num_posts'] = array( ‘#type' => 'textfield', '#title' => t('Number of pending comments to display'), '#default_value' => variable_get(‘modulename_block_num_posts',
5), ); }
Enabling a Block
Third step: Enabling a BlockThere are two ways to enabling a block:1:Using Drupal Administrator page2:We can enable block when module is installed.This is fairly straightforward, and is done through a query that inserts the block settings directly into the blocks table. The query goes within hook_install(), located in your module’s .install file. Here’s an example of the user module enabling the user login block when Drupal is being Installed.db_query("INSERT INTO {blocks} (module, delta, theme, status)
VALUES ('user', 0, '%s', 1)", variable_get('theme_default', 'garland'));
Thanks You!
Top Related