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

Module file for block_example.

Functions & methods

NameDescription
block_example_block_configureImplements hook_block_configure().
block_example_block_infoImplements hook_block_info().
block_example_block_list_alterImplements hook_block_list_alter().
block_example_block_saveImplements hook_block_save().
block_example_block_viewImplements hook_block_view().
block_example_block_view_alterImplements hook_block_view_alter().
block_example_contentsA module-defined block content function.
block_example_menuImplements hook_menu().
block_example_pageSimple page function to explain what the block example is about.

File

block_example/block_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for block_example.
  5. */
  6. /**
  7. * @defgroup block_example Example: Block
  8. * @ingroup examples
  9. * @{
  10. * Demonstrates code creation of blocks.
  11. *
  12. * This is an example outlining how a module can define blocks that can be
  13. * displayed on various pages of a site, or how to alter blocks provided by
  14. * other modules.
  15. */
  16. /**
  17. * Implements hook_menu().
  18. *
  19. * Provides a default page to explain what this module does.
  20. */
  21. function block_example_menu() {
  22. $items['examples/block_example'] = array(
  23. 'page callback' => 'block_example_page',
  24. 'access callback' => TRUE,
  25. 'title' => 'Block Example',
  26. );
  27. return $items;
  28. }
  29. /**
  30. * Simple page function to explain what the block example is about.
  31. */
  32. function block_example_page() {
  33. $page = array(
  34. '#type' => 'markup',
  35. '#markup' => t('The Block Example provides two sample blocks which demonstrate the various block APIs. To experiment with the blocks, enable and configure them on <a href="@url">the block admin page</a>.', array('@url' => url('admin/structure/block'))),
  36. );
  37. return $page;
  38. }
  39. /**
  40. * Implements hook_block_info().
  41. *
  42. * This hook declares what blocks are provided by the module.
  43. */
  44. function block_example_block_info() {
  45. // This hook returns an array, each component of which is an array of block
  46. // information. The array keys are the 'delta' values used in other block
  47. // hooks.
  48. // The required block information is a block description, which is shown
  49. // to the site administrator in the list of possible blocks. You can also
  50. // provide initial settings for block weight, status, etc.
  51. // Many options are defined in hook_block_info():
  52. $blocks['example_configurable_text'] = array(
  53. // info: The name of the block.
  54. 'info' => t('Example: configurable text string'),
  55. // Block caching options (per role, per user, etc.)
  56. 'cache' => DRUPAL_CACHE_PER_ROLE, // default
  57. );
  58. // This sample shows how to provide default settings. In this case we'll
  59. // enable the block in the first sidebar and make it visible only on
  60. // 'node/*' pages. See the hook_block_info() documentation for these.
  61. $blocks['example_empty'] = array(
  62. 'info' => t('Example: empty block'),
  63. 'status' => TRUE,
  64. 'region' => 'sidebar_first', // Not usually provided.
  65. 'visibility' => BLOCK_VISIBILITY_LISTED, // Not usually provided.
  66. 'pages' => 'node/*', // Not usually provided here.
  67. );
  68. return $blocks;
  69. }
  70. /**
  71. * Implements hook_block_configure().
  72. *
  73. * This hook declares configuration options for blocks provided by this module.
  74. */
  75. function block_example_block_configure($delta = '') {
  76. // The $delta parameter tells us which block is being configured.
  77. // In this example, we'll allow the administrator to customize
  78. // the text of the 'configurable text string' block defined in this module.
  79. $form = array();
  80. if ($delta == 'example_configurable_text') {
  81. // All we need to provide is the specific configuration options for our
  82. // block. Drupal will take care of the standard block configuration options
  83. // (block title, page visibility, etc.) and the save button.
  84. $form['block_example_string'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Block contents'),
  87. '#size' => 60,
  88. '#description' => t('This text will appear in the example block.'),
  89. '#default_value' => variable_get('block_example_string', t('Some example content.')),
  90. );
  91. }
  92. return $form;
  93. }
  94. /**
  95. * Implements hook_block_save().
  96. *
  97. * This hook declares how the configured options for a block
  98. * provided by this module are saved.
  99. */
  100. function block_example_block_save($delta = '', $edit = array()) {
  101. // We need to save settings from the configuration form.
  102. // We need to check $delta to make sure we are saving the right block.
  103. if ($delta == 'example_configurable_text') {
  104. // Have Drupal save the string to the database.
  105. variable_set('block_example_string', $edit['block_example_string']);
  106. }
  107. return;
  108. }
  109. /**
  110. * Implements hook_block_view().
  111. *
  112. * This hook generates the contents of the blocks themselves.
  113. */
  114. function block_example_block_view($delta = '') {
  115. //The $delta parameter tells us which block is being requested.
  116. switch ($delta) {
  117. case 'example_configurable_text':
  118. // The subject is displayed at the top of the block. Note that it
  119. // should be passed through t() for translation. The title configured
  120. // for the block using Drupal UI supercedes this one.
  121. $block['subject'] = t('Title of first block (example_configurable_text)');
  122. // The content of the block is typically generated by calling a custom
  123. // function.
  124. $block['content'] = block_example_contents($delta);
  125. break;
  126. case 'example_empty':
  127. $block['subject'] = t('Title of second block (example_empty)');
  128. $block['content'] = block_example_contents($delta);
  129. break;
  130. }
  131. return $block;
  132. }
  133. /**
  134. * A module-defined block content function.
  135. */
  136. function block_example_contents($which_block) {
  137. switch ($which_block) {
  138. case 'example_configurable_text':
  139. // Modules would typically perform some database queries to fetch the
  140. // content for their blocks. Here, we'll just use the variable set in the
  141. // block configuration or, if none has set, a default value.
  142. // Block content can be returned in two formats: renderable arrays
  143. // (as here) are preferred though a simple string will work as well.
  144. return array('#markup' => variable_get('block_example_string', t('A default value. This block was created at %time', array('%time' => date('c')))));
  145. case 'example_empty':
  146. // It is possible that a block not have any content, since it is
  147. // probably dynamically constructed. In this case, Drupal will not display
  148. // the block at all. This block will not be displayed.
  149. return;
  150. }
  151. }
  152. /*
  153. * The following hooks can be used to alter blocks
  154. * provided by your own or other modules.
  155. */
  156. /**
  157. * Implements hook_block_list_alter().
  158. *
  159. * This hook allows you to add, remove or modify blocks in the block list. The
  160. * block list contains the block definitions. This example requires
  161. * search module and the search block enabled
  162. * to see how this hook implementation works.
  163. *
  164. * You may also be interested in hook_block_info_alter(), which allows changes
  165. * to the behavior of blocks.
  166. */
  167. function block_example_block_list_alter(&$blocks) {
  168. // We are going to make the search block sticky on bottom of regions. For
  169. // this example, we will modify the block list and append the search block at
  170. // the end of the list, so even if the administrator configures the block to
  171. // be on the top of the region, it will demote to bottom again.
  172. foreach ($blocks as $bid => $block) {
  173. if (($block->module == 'search') && ($block->delta == 'form')) {
  174. // Remove the block from the list and append to the end.
  175. unset($blocks[$bid]);
  176. $blocks[$bid] = $block;
  177. break;
  178. }
  179. }
  180. }
  181. /**
  182. * Implements hook_block_view_alter().
  183. *
  184. * This hook allows you to modify the output of any block in the system.
  185. *
  186. * In addition, instead of hook_block_view_alter(), which is called for all
  187. * blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a
  188. * specific block.
  189. *
  190. * We are going to uppercase the title of any block if the string "magic string"
  191. * is encountered in the content. If we were changing only our block using
  192. * hook_block_view_MODULE_DELTA_alter to do this, we would have used the
  193. * function:
  194. * block_example_block_view_block_example_example_configurable_text_alter()
  195. *
  196. * To demonstrate the effect of this hook, you can use the
  197. * 'configurable_text_string' block created by this module and add the
  198. * text 'magic string' into the configuration.
  199. */
  200. function block_example_block_view_alter(&$data, $block) {
  201. // Verify the we have raw text content
  202. if (!isset($data['content']) || !is_string($data['content'])) {
  203. return;
  204. }
  205. // If the content contains the string: 'magic string', uppercase the title.
  206. if (strstr($data['content'], 'magic string')) {
  207. $data['subject'] = isset($data['subject']) ? drupal_strtoupper($data['subject']) : '';
  208. }
  209. }
  210. /**
  211. * @} End of "defgroup block_example".
  212. */
Login or register to post comments