Blocks In Drupal

6,670 views

Published on

This ppt is about block implementation in drupal.It explain how to create region and block with small code.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,670
On SlideShare
0
From Embeds
0
Number of Embeds
57
Actions
Shares
0
Downloads
70
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Blocks In Drupal

  1. 1. BLOCKS IN DRUPAL By : Shailesh Hule [email_address]
  2. 2. What is Block? <ul><li>B locks 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. </li></ul><ul><li>“ Block are not nodes and don’t follow the same rules nodes do .“ </li></ul>
  3. 3. Define Block <ul><li>We can define block in two ways </li></ul><ul><li>1:Drupal web interface. </li></ul><ul><li>The block configuration page is located at Administer > site building > Blocks </li></ul><ul><li>2:Programmatically using Block API. </li></ul>
  4. 4. Control Block <ul><li>About the block we can control the following option: </li></ul><ul><li>1:Region placement. </li></ul><ul><li>2:User specific visibility settings. </li></ul><ul><li>3:Role specific visibility settings. </li></ul><ul><li>4:Page specific visibility settings. </li></ul>
  5. 5. Block Region <ul><li>First step to create block: Define Region </li></ul><ul><li>It is not actual first step because it is about block region. </li></ul><ul><li>You can use default regions or we create new region. </li></ul><ul><li>How to create new region? </li></ul><ul><li>Block region defined in template.php file which is placed in theme folder. </li></ul><ul><li>Path: drupal hemes heme_name </li></ul>
  6. 6. <ul><li>code for regions in template.php file </li></ul><ul><li>function themename _regions() { </li></ul><ul><li>return array( </li></ul><ul><li>'left' => t('left sidebar'), </li></ul><ul><li>'right' => t('right sidebar'), </li></ul><ul><li>'content_top' => t('content top'), </li></ul><ul><li>'content_bottom' => t('content bottom'), </li></ul><ul><li>'header' => t('header'), </li></ul><ul><li>'footer' => t('footer') </li></ul><ul><li>); </li></ul><ul><li>} </li></ul>
  7. 7. How To Define Block? <ul><li>Second Step: Block Defining </li></ul><ul><li>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. </li></ul>
  8. 8. Database schema for Block
  9. 9. <ul><li>Block properties for every block are stored in the blocks table. </li></ul><ul><li>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 . </li></ul><ul><li>Lastly, blocks_roles stores the role-based permissions for each block. </li></ul>
  10. 10. <ul><li>module: This column contains the name of the module that defined the block. </li></ul><ul><li>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(). </li></ul><ul><li>theme: Because blocks can be defined for multiple themes, Drupal needs to store the name of the theme for which the block is enabled. </li></ul>
  11. 11. <ul><li>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. </li></ul><ul><li>throttle: When the throttle module is enabled, this column tracks which blocks should be throttled.0 indicate throttle disable,1 indicate it is enable. </li></ul>
  12. 12. <ul><li>Again come to hook_block()….. </li></ul><ul><li>Signature of hook_block() </li></ul><ul><li>function hook_block( $op = 'list', $delta = 0, $edit = array()) </li></ul><ul><li>Parameter List: </li></ul><ul><li>$op : This parameter defines the phases a block passes through. </li></ul><ul><li>list: Return an array of all blocks defined by the module. Array keys are the delta. </li></ul>
  13. 13. <ul><li>Possible list values, and their defaults, follow: </li></ul><ul><li>info: This value is required . A translatable string </li></ul><ul><li>provides a description of the block suitable for site administrators. </li></ul><ul><li>status : Is the block enabled— TRUE or FALSE? Default is FALSE . </li></ul><ul><li>region: The default region is left . </li></ul><ul><li>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. </li></ul>
  14. 14. <ul><li>pages: Defines the default pages the node should be visible on. </li></ul><ul><li>custom: TRUE means this is a custom block and FALSE means that it’s a block implemented by a module. </li></ul><ul><li>title: The default block title . </li></ul><ul><li>configure: Return a form array of fields for block-specific settings. This is merged with the overall form on the block configuration page. </li></ul><ul><li>save: Called when the configuration form is submitted. </li></ul>
  15. 15. <ul><li>view: The block is being displayed. Return an array containing the block’s title and content. </li></ul><ul><li>$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. </li></ul><ul><li>$edit When $op is save, $edit contains the submitted form data from the block configuration form . </li></ul>
  16. 16. Building a Block <ul><li>Let’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: </li></ul><ul><li>; $Id$ </li></ul><ul><li>name = Module_name </li></ul><ul><li>description = Blocks for facilitating pending content workflow. </li></ul><ul><li>version = &quot;$name$&quot; </li></ul>
  17. 17. <ul><li>Then, add module_name .module as well: </li></ul><ul><li><?php </li></ul><ul><li>// $Id$ </li></ul><ul><li>/** </li></ul><ul><li>* @file </li></ul><ul><li>* Implements various blocks to improve pending content workflow. </li></ul><ul><li>*/ </li></ul><ul><li>Once you’ve created these files, enable the module via Administer > Site building >Modules </li></ul>
  18. 18. <ul><li>Let’s add our block hook and implement the list operation </li></ul><ul><li>/** </li></ul><ul><li>* Implementation of hook_block(). </li></ul><ul><li>*/ </li></ul><ul><li>function molulename _block($op = 'list', $delta = 0, $edit = array()) { </li></ul><ul><li>switch ($op) { </li></ul><ul><li>case 'list': </li></ul><ul><li>$blocks[0]['info'] = t('Pending comments'); </li></ul><ul><li>return $blocks; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. You should now see your block listed on the block overview page.
  20. 20. <ul><li>See following code which create configuration form for our </li></ul><ul><li>Block. Add this code in maodule_name.module file. </li></ul><ul><li>case 'configure': </li></ul><ul><li>$form['approval_block_num_posts'] = array( </li></ul><ul><li>'#type' => 'textfield', </li></ul><ul><li>'#title' => t('Number of pending comments to display'), </li></ul><ul><li>'#default_value' => variable_get(‘modulename_block_num_posts', 5), </li></ul><ul><li>); </li></ul><ul><li>return $form; </li></ul>
  21. 21. Block configuration form with the block’s custom fields appended
  22. 22. <ul><li>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 : </li></ul><ul><li>case 'save': </li></ul><ul><li>variable_set(‘ modulename _block_num_posts', </li></ul><ul><li>(int) $edit['approval_block_num_posts']); </li></ul><ul><li>break; </li></ul>
  23. 23. <ul><li>Finally, add the 'view' operation and return a list of pending comments when the block is viewed: </li></ul><ul><li>case 'view ': </li></ul><ul><li>// Retrieve the number of pending comments to display that </li></ul><ul><li>// we saved earlier in the 'save' op, defaulting to 5. </li></ul><ul><li>if (user_access('administer comments')) { </li></ul><ul><li>$num_posts = variable_get(‘ modulename _block_num_posts', 5); </li></ul><ul><li>$result = db_query_range('SELECT c.* FROM {comments} c WHERE c.status = %d ORDER BY c.timestamp', COMMENT_NOT_PUBLISHED, 0,$num_posts); </li></ul>
  24. 24. <ul><li>// Preserve our current location so user can return after editing. </li></ul><ul><li>$destination = drupal_get_destination(); </li></ul><ul><li>$items = array(); </li></ul><ul><li>while ($comment = db_fetch_object($result)) { </li></ul><ul><li>$items[ ] = l($comment->subject, 'node/'. $comment->nid, array(), </li></ul><ul><li>NULL, 'comment-'. $comment->cid). ' '. </li></ul><ul><li>l(t('[edit]'), 'comment/edit/'. $comment->cid, array(), $destination); </li></ul><ul><li>} </li></ul>
  25. 25. <ul><li>$block['subject'] = t('Pending comments'); </li></ul><ul><li>// We theme our array of links as an unordered list. </li></ul><ul><li>$block['content'] = theme('item_list', $items); </li></ul><ul><li>} </li></ul><ul><li>return $block; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//close this function…. </li></ul>
  26. 26. <ul><li>Following code shows who to implements more than one block in hook function. </li></ul><ul><li>function modulename _block($op = 'list', $delta = 0, $edit = array()) { </li></ul><ul><li>switch ($op) { </li></ul><ul><li>case 'list': </li></ul><ul><li>$blocks[0]['info'] = t('Pending comments'); //first block </li></ul><ul><li>$blocks[1]['info'] = t('Unpublished nodes'); //second block </li></ul><ul><li>return $blocks; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 27. <ul><li>case 'configure': </li></ul><ul><li>// Only in block 0 (the Pending comments block) can one </li></ul><ul><li>// set the number of comments to display. </li></ul><ul><li>if ($delta == 0 ) { // for first block </li></ul><ul><li>$form['approval_block_num_posts'] = array( </li></ul><ul><li>‘ #type' => 'textfield', </li></ul><ul><li>'#title' => t('Number of pending comments to display'), </li></ul><ul><li>'#default_value' => variable_get(‘ modulename _block_num_posts', 5), </li></ul><ul><li>); </li></ul><ul><li>} </li></ul>
  28. 28. Enabling a Block <ul><li>Third step: Enabling a Block </li></ul><ul><li>There are two ways to enabling a block: </li></ul><ul><li>1:Using Drupal Administrator page </li></ul><ul><li>2:We can enable block when module is installed. </li></ul><ul><li>This is fairly straightforward, and is done through a query that inserts </li></ul><ul><li>the block settings directly into the blocks table. The query goes within </li></ul><ul><li>hook_install(), located in your module’s .install file . Here’s an example </li></ul><ul><li>of the user module enabling the user login block when Drupal is being </li></ul><ul><li>Installed. </li></ul><ul><li>db_query(&quot; INSERT INTO {blocks} (module, delta, theme, status) VALUES ('user', 0, '%s', 1)&quot;, variable_get('theme_default', 'garland')); </li></ul>
  29. 29. <ul><li>Thanks You! </li></ul>

×