block_example.module

  1. examples
    1. 6 block_example/block_example.module
    2. 7 block_example/block_example.module
    3. 8 block_example/block_example.module
  2. drupal
    1. 4.6 developer/examples/block_example.module
    2. 4.7 developer/examples/block_example.module
    3. 5 developer/examples/block_example.module

This is an example outlining how a module can be define blocks to be displayed on each page.

Functions & methods

NameDescription
block_example_blockImplementation of hook_block().
block_example_contentsA block content function.
block_example_helpImplementation of hook_help().

File

developer/examples/block_example.module
View source
  1. /**
  2. * @file
  3. * This is an example outlining how a module can be define blocks to be
  4. * displayed on each page.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. *
  9. * Throughout Drupal, hook_help() is used to display help text at the top of
  10. * pages. Some other parts of Drupal pages get explanatory text from these hooks
  11. * as well. We use it here to provide a description of the module on the
  12. * module administration page.
  13. */
  14. function block_example_help($section) {
  15. switch ($section) {
  16. case 'admin/modules#description':
  17. // This description is shown in the listing at admin/modules.
  18. return t('An example module showing how to define a block.');
  19. }
  20. }
  21. /**
  22. * Implementation of hook_block().
  23. *
  24. * This hook both declares to Drupal what blocks are provided by the module, and
  25. * generates the contents of the blocks themselves.
  26. */
  27. function block_example_block($op = 'list', $delta = 0, $edit = array()) {
  28. // The $op parameter determines what piece of information is being requested.
  29. switch ($op) {
  30. case 'list':
  31. // If $op is "list", we just need to return a list of block descriptions.
  32. // This is used to provide a list of possible blocks to the administrator,
  33. // end users will not see these descriptions.
  34. $blocks[0]['info'] = t('Example: configurable text string');
  35. $blocks[1]['info'] = t('Example: empty block');
  36. return $blocks;
  37. case 'configure':
  38. // If $op is "configure", we need to provide the administrator with a
  39. // configuration form. The $delta parameter tells us which block is being
  40. // configured. In this example, we'll allow the administrator to customize
  41. // the text of the first block.
  42. $form = array();
  43. if ($delta == 0) {
  44. // All we need to provide is a text field, Drupal will take care of
  45. // the other block configuration options and the save button.
  46. $form['block_example_string'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('Block contents'),
  49. '#size' => 60,
  50. '#description' => t('This string will appear in the example block.'),
  51. '#default_value' =>
  52. variable_get('block_example_string', t('Some example content.')),
  53. );
  54. }
  55. return $form;
  56. case 'save':
  57. // If $op is "save", we need to save settings from the configuration form.
  58. // Since the first block is the only one that allows configuration, we
  59. // need to check $delta to make sure we only save it.
  60. if ($delta == 0) {
  61. // Have Drupal save the string to the database.
  62. variable_set('block_example_string', $edit['block_example_string']);
  63. }
  64. return;
  65. case 'view': default:
  66. // If $op is "view", then we need to generate the block for display
  67. // purposes. The $delta parameter tells us which block is being requested.
  68. switch ($delta) {
  69. case 0:
  70. // The subject is displayed at the top of the block. Note that it
  71. // should be passed through t() for translation.
  72. $block['subject'] = t('Title of block #1');
  73. // The content of the block is typically generated by calling a custom
  74. // function.
  75. $block['content'] = block_example_contents(1);
  76. // These next three settings are be registered when the block is first
  77. // loaded at admin/block, and from there can be changed manually via
  78. // block administration.
  79. $block['weight'] = -6;
  80. $block['enabled'] = 0;
  81. // Note that if you set a region that isn't available in a given
  82. // theme, the block will be registered instead to that theme's
  83. // default region (the first item in the _regions array).
  84. $block['region'] = 'right';
  85. break;
  86. case 1:
  87. // This time we'll use the more concise array declaration style.
  88. $block = array('subject' => t('Title of block #2'),
  89. 'content' => block_example_contents(2), 'weight' => -7,
  90. 'enabled' => 1, 'region' => 'footer');
  91. break;
  92. }
  93. return $block;
  94. }
  95. }
  96. /**
  97. * A block content function.
  98. */
  99. function block_example_contents($which_block) {
  100. if ($which_block == 1) {
  101. // Modules would typically perform some database queries to fetch the
  102. // content for their blocks. Here, we'll just use the variable set in the
  103. // block configuration or, if none has set, a default value.
  104. return variable_get('block_example_string', t('A default value.'));
  105. }
  106. if ($which_block == 2) {
  107. // It is possible that your block will not have any content, since it is
  108. // probably dynamically constructed. In this case, Drupal will not display
  109. // the block at all.
  110. return;
  111. }
  112. }
Login or register to post comments