block_example.module

You are here

  1. 7 block_example/block_example.module
  2. 6 block_example/block_example.module
  3. 8 block_example/block_example.module

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

Functions

Namesort descending Description
block_example_block Implementation of hook_block().
block_example_contents A block content function.

File

block_example/block_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is an example outlining how a module can define blocks to be
  5. * displayed on each page.
  6. */
  7. /**
  8. * @defgroup block_example Example: Block
  9. * @ingroup examples
  10. * @{
  11. * Block defined by a module. (drupal 6)
  12. *
  13. * This example is part of the Examples for Developers Project which you can download
  14. * and experiment with here: http://drupal.org/project/examples
  15. */
  16. /**
  17. * Implementation of hook_block().
  18. *
  19. * This hook both declares to Drupal what blocks are provided by the module, and
  20. * generates the contents of the blocks themselves.
  21. */
  22. function block_example_block($op = 'list', $delta = '', $edit = array()) {
  23. // The $op parameter determines what piece of information is being requested.
  24. switch ($op) {
  25. case 'list':
  26. // If $op is "list", we just need to return a list of block descriptions.
  27. // This is used to provide a list of possible blocks to the administrator;
  28. // end users will not see these descriptions.
  29. $blocks['configurable-text'] = array(
  30. 'info' => t('Example: configurable text string'),
  31. );
  32. // A block can provide default settings. In this case we'll enable the
  33. // block and make it visible only on the 'node/*' pages.
  34. $blocks['empty'] = array(
  35. 'info' => t('Example: empty block'),
  36. 'status' => TRUE,
  37. 'weight' => 0,
  38. 'visibility' => 1,
  39. 'pages' => 'node/*',
  40. );
  41. return $blocks;
  42. case 'configure':
  43. // If $op is "configure", we need to provide the administrator with a
  44. // configuration form. The $delta parameter tells us which block is being
  45. // configured. In this example, we'll allow the administrator to customize
  46. // the text of the first block.
  47. $form = array();
  48. if ($delta == 'configurable-text') {
  49. // All we need to provide is a text field, Drupal will take care of
  50. // the other block configuration options and the save button.
  51. $form['block_example_string'] = array(
  52. '#type' => 'textfield',
  53. '#title' => t('Block contents'),
  54. '#size' => 60,
  55. '#description' => t('This string will appear in the example block.'),
  56. '#default_value' => variable_get('block_example_string', t('Some example content.')),
  57. );
  58. }
  59. return $form;
  60. case 'save':
  61. // If $op is "save", we need to save settings from the configuration form.
  62. // Since the first block is the only one that allows configuration, we
  63. // need to check $delta to make sure we only save it.
  64. if ($delta == 'configurable-text') {
  65. // Have Drupal save the string to the database.
  66. variable_set('block_example_string', $edit['block_example_string']);
  67. }
  68. return;
  69. case 'view':
  70. // If $op is "view", then we need to generate the block for display
  71. // purposes. The $delta parameter tells us which block is being requested.
  72. switch ($delta) {
  73. case 'configurable-text':
  74. // The subject is displayed at the top of the block. Note that it
  75. // should be passed through t() for translation.
  76. $block['subject'] = t('Title of configurable-text block');
  77. // The content of the block is typically generated by calling a custom
  78. // function.
  79. $block['content'] = block_example_contents(1);
  80. break;
  81. case 'empty':
  82. $block['subject'] = t('Title of block #2');
  83. $block['content'] = block_example_contents(2);
  84. break;
  85. }
  86. return $block;
  87. }
  88. }
  89. /**
  90. * A block content function.
  91. */
  92. function block_example_contents($which_block) {
  93. switch ($which_block) {
  94. case 1:
  95. // Modules would typically perform some database queries to fetch the
  96. // content for their blocks. Here, we'll just use the variable set in the
  97. // block configuration or, if none has set, a default value.
  98. return variable_get('block_example_string', t('A default value.'));
  99. case 2:
  100. // It is possible that your block will not have any content, since it is
  101. // probably dynamically constructed. In this case, Drupal will not display
  102. // the block at all.
  103. return;
  104. }
  105. }
  106. /**
  107. * @} End of "defgroup block_example".
  108. */