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 define blocks to be displayed on each page.

Functions & methods

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

File

developer/examples/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. * Implementation of hook_block().
  9. *
  10. * This hook both declares to Drupal what blocks are provided by the module, and
  11. * generates the contents of the blocks themselves.
  12. */
  13. function block_example_block($op = 'list', $delta = 0, $edit = array()) {
  14. // The $op parameter determines what piece of information is being requested.
  15. switch ($op) {
  16. case 'list':
  17. // If $op is "list", we just need to return a list of block descriptions.
  18. // This is used to provide a list of possible blocks to the administrator,
  19. // end users will not see these descriptions.
  20. $blocks[0]['info'] = t('Example: configurable text string');
  21. $blocks[1]['info'] = t('Example: empty block');
  22. return $blocks;
  23. case 'configure':
  24. // If $op is "configure", we need to provide the administrator with a
  25. // configuration form. The $delta parameter tells us which block is being
  26. // configured. In this example, we'll allow the administrator to customize
  27. // the text of the first block.
  28. $form = array();
  29. if ($delta == 0) {
  30. // All we need to provide is a text field, Drupal will take care of
  31. // the other block configuration options and the save button.
  32. $form['block_example_string'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Block contents'),
  35. '#size' => 60,
  36. '#description' => t('This string will appear in the example block.'),
  37. '#default_value' =>
  38. variable_get('block_example_string', t('Some example content.')),
  39. );
  40. }
  41. return $form;
  42. case 'save':
  43. // If $op is "save", we need to save settings from the configuration form.
  44. // Since the first block is the only one that allows configuration, we
  45. // need to check $delta to make sure we only save it.
  46. if ($delta == 0) {
  47. // Have Drupal save the string to the database.
  48. variable_set('block_example_string', $edit['block_example_string']);
  49. }
  50. return;
  51. case 'view': default:
  52. // If $op is "view", then we need to generate the block for display
  53. // purposes. The $delta parameter tells us which block is being requested.
  54. switch ($delta) {
  55. case 0:
  56. // The subject is displayed at the top of the block. Note that it
  57. // should be passed through t() for translation.
  58. $block['subject'] = t('Title of block #1');
  59. // The content of the block is typically generated by calling a custom
  60. // function.
  61. $block['content'] = block_example_contents(1);
  62. break;
  63. case 1:
  64. $block['subject'] = t('Title of block #2');
  65. $block['content'] = block_example_contents(2);
  66. break;
  67. }
  68. return $block;
  69. }
  70. }
  71. /**
  72. * A block content function.
  73. */
  74. function block_example_contents($which_block) {
  75. if ($which_block == 1) {
  76. // Modules would typically perform some database queries to fetch the
  77. // content for their blocks. Here, we'll just use the variable set in the
  78. // block configuration or, if none has set, a default value.
  79. return variable_get('block_example_string', t('A default value.'));
  80. }
  81. if ($which_block == 2) {
  82. // It is possible that your block will not have any content, since it is
  83. // probably dynamically constructed. In this case, Drupal will not display
  84. // the block at all.
  85. return;
  86. }
  87. }
Login or register to post comments