Posted By

brownrl on 02/12/13


Tagged

content drupal Blocks


Versions (?)

Drupal Auto Blocks


 / Published in: PHP
 

URL: http://www.itsgotto.be/cv.php

This is a hookblockinfo and hookblockview template that will allow you to quickly create blocks in a Drupal module. When you use this all you have to do is create a directory "blocks" in the module and then create block.blockname.php in the directory. Inside the php you need to define a function blockblocknamecontent().

  1. <?php
  2. /**
  3.  * @file
  4.  * This file defines the robmod module.
  5.  */
  6.  
  7.  
  8.  
  9.  
  10. /**
  11.  * Implements hook_block_info().
  12.  */
  13. function robmod_block_info() {
  14.  
  15. // Get the module name
  16. $module = basename(__FILE__, '.module');
  17.  
  18. // Build a glob to get all the blocks defined.
  19. $module_path = drupal_get_path("module", $module);
  20. $block_path = $module_path . "/" . "blocks";
  21. $block_files = glob($block_path . "/" . "block.*.php");
  22.  
  23. // The empty block info array.
  24. $r = array();
  25.  
  26. foreach($block_files as $file) {
  27. if(preg_match("/block\.([a-z_]+)\.php/", $file, $matches)) {
  28. // Block name
  29. $block = $matches[1];
  30. // Name can be translated why not?
  31. $block_name = ucwords(str_replace("_", " ", $block));
  32. $r[$block]['info'] = t($block_name);
  33. // Assume no cache.
  34. $r[$block]['cache'] = DRUPAL_NO_CACHE;
  35. }
  36. }
  37.  
  38. return $r;
  39. }
  40.  
  41.  
  42. /**
  43.  * Implements hook_block_view().
  44.  */
  45. function robmod_block_view($delta = '') {
  46.  
  47. // Get the module name
  48. $module = basename(__FILE__, '.module');
  49.  
  50. // Setup a blank block.
  51. $block = array();
  52. $block['subject'] = t($delta);
  53. $block['content'] = t('No Content Defiend for: ') . $delta;
  54.  
  55. // Build a path to the block file.
  56. $module_path = drupal_get_path("module", $module);
  57. $block_path = $module_path . "/" . "blocks";
  58. $block_file = $block_path . "/" . "block." . $delta . ".php";
  59.  
  60. // Does the file exists?
  61. if(file_exists($block_file)) {
  62. // Yes it does so let's include it.
  63. include_once($block_file);
  64. // We should now have a function from that.
  65. $function = "block_".$delta."_content";
  66. // Does that function exist?
  67. if(function_exists($function)) {
  68. // Yes assign the return to the block content.
  69. $block['content'] = $function();
  70. }
  71. }
  72.  
  73. return $block;
  74. }

Report this snippet  

You need to login to post a comment.