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. $blocks['example_uppercase'] = array(
  69. // info: The name of the block.
  70. 'info' => t('Example: uppercase this please'),
  71. 'status' => TRUE,
  72. 'region' => 'sidebar_first', // Not usually provided.
  73. );
  74. return $blocks;
  75. }
  76. /**
  77. * Implements hook_block_configure().
  78. *
  79. * This hook declares configuration options for blocks provided by this module.
  80. */
  81. function block_example_block_configure($delta = '') {
  82. // The $delta parameter tells us which block is being configured.
  83. // In this example, we'll allow the administrator to customize
  84. // the text of the 'configurable text string' block defined in this module.
  85. $form = array();
  86. if ($delta == 'example_configurable_text') {
  87. // All we need to provide is the specific configuration options for our
  88. // block. Drupal will take care of the standard block configuration options
  89. // (block title, page visibility, etc.) and the save button.
  90. $form['block_example_string'] = array(
  91. '#type' => 'textfield',
  92. '#title' => t('Block contents'),
  93. '#size' => 60,
  94. '#description' => t('This text will appear in the example block.'),
  95. '#default_value' => variable_get('block_example_string', t('Some example content.')),
  96. );
  97. }
  98. return $form;
  99. }
  100. /**
  101. * Implements hook_block_save().
  102. *
  103. * This hook declares how the configured options for a block
  104. * provided by this module are saved.
  105. */
  106. function block_example_block_save($delta = '', $edit = array()) {
  107. // We need to save settings from the configuration form.
  108. // We need to check $delta to make sure we are saving the right block.
  109. if ($delta == 'example_configurable_text') {
  110. // Have Drupal save the string to the database.
  111. variable_set('block_example_string', $edit['block_example_string']);
  112. }
  113. return;
  114. }
  115. /**
  116. * Implements hook_block_view().
  117. *
  118. * This hook generates the contents of the blocks themselves.
  119. */
  120. function block_example_block_view($delta = '') {
  121. //The $delta parameter tells us which block is being requested.
  122. switch ($delta) {
  123. case 'example_configurable_text':
  124. // The subject is displayed at the top of the block. Note that it
  125. // should be passed through t() for translation. The title configured
  126. // for the block using Drupal UI supercedes this one.
  127. $block['subject'] = t('Title of first block (example_configurable_text)');
  128. // The content of the block is typically generated by calling a custom
  129. // function.
  130. $block['content'] = block_example_contents($delta);
  131. break;
  132. case 'example_empty':
  133. $block['subject'] = t('Title of second block (example_empty)');
  134. $block['content'] = block_example_contents($delta);
  135. break;
  136. case 'example_uppercase':
  137. $block['subject'] = t("uppercase this please");
  138. $block['content'] = t("This block's title will be changed to uppercase. Any other block with 'uppercase' in the subject or title will also be altered. If you change this block's title through the UI to omit the word 'uppercase', it will still be altered to uppercase as the subject key has not been changed.");
  139. break;
  140. }
  141. return $block;
  142. }
  143. /**
  144. * A module-defined block content function.
  145. */
  146. function block_example_contents($which_block) {
  147. switch ($which_block) {
  148. case 'example_configurable_text':
  149. // Modules would typically perform some database queries to fetch the
  150. // content for their blocks. Here, we'll just use the variable set in the
  151. // block configuration or, if none has set, a default value.
  152. // Block content can be returned in two formats: renderable arrays
  153. // (as here) are preferred though a simple string will work as well.
  154. // Block content created through the UI defaults to a string.
  155. return array('#markup' => variable_get('block_example_string', t('A default value. This block was created at %time', array('%time' => date('c')))));
  156. case 'example_empty':
  157. // It is possible that a block not have any content, since it is
  158. // probably dynamically constructed. In this case, Drupal will not display
  159. // the block at all. This block will not be displayed.
  160. return;
  161. }
  162. }
  163. /*
  164. * The following hooks can be used to alter blocks
  165. * provided by your own or other modules.
  166. */
  167. /**
  168. * Implements hook_block_list_alter().
  169. *
  170. * This hook allows you to add, remove or modify blocks in the block list. The
  171. * block list contains the block definitions. This example requires
  172. * search module and the search block enabled
  173. * to see how this hook implementation works.
  174. *
  175. * You may also be interested in hook_block_info_alter(), which allows changes
  176. * to the behavior of blocks.
  177. */
  178. function block_example_block_list_alter(&$blocks) {
  179. // We are going to make the search block sticky on bottom of regions. For
  180. // this example, we will modify the block list and append the search block at
  181. // the end of the list, so even if the administrator configures the block to
  182. // be on the top of the region, it will demote to bottom again.
  183. foreach ($blocks as $bid => $block) {
  184. if (($block->module == 'search') && ($block->delta == 'form')) {
  185. // Remove the block from the list and append to the end.
  186. unset($blocks[$bid]);
  187. $blocks[$bid] = $block;
  188. break;
  189. }
  190. }
  191. }
  192. /**
  193. * Implements hook_block_view_alter().
  194. *
  195. * This hook allows you to modify the output of any block in the system.
  196. *
  197. * In addition, instead of hook_block_view_alter(), which is called for all
  198. * blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a
  199. * specific block. To change only our block using
  200. * hook_block_view_MODULE_DELTA_alter, we would use the function:
  201. * block_example_block_view_block_example_example_configurable_text_alter()
  202. *
  203. * We are going to uppercase the subject (the title of the block as shown to the
  204. * user) of any block if the string "uppercase" appears in the block title or
  205. * subject. Default block titles are set programmatically in the subject key;
  206. * titles created through the UI are saved in the title key. This module creates
  207. * an example block to demonstrate this effect (default title set
  208. * programmatically as subject). You can also demonstrate the effect of this
  209. * hook by creating a new block whose title has the string 'uppercase' in it
  210. * (set as title through the UI).
  211. */
  212. function block_example_block_view_alter(&$data, $block) {
  213. // We'll search for the string 'uppercase'.
  214. if ((!empty($block->title) && stristr($block->title, 'uppercase')) || (!empty($data['subject']) && stristr($data['subject'], 'uppercase'))) {
  215. // This will uppercase the default title.
  216. $data['subject'] = isset($data['subject']) ? drupal_strtoupper($data['subject']) : '';
  217. // This will uppercase a title set in the UI.
  218. $block->title = isset($block->title) ? drupal_strtoupper($block->title) : '';
  219. }
  220. }
  221. /**
  222. * @} End of "defgroup block_example".
  223. */
Login or register to post comments