block.api.php

  1. drupal
    1. 7 modules/block/block.api.php
    2. 8 core/modules/block/block.api.php

Hooks provided by the Block module.

Functions & methods

NameDescription
hook_block_configureDefine a configuration form for a block.
hook_block_infoDefine all blocks provided by the module.
hook_block_info_alterChange block definition before saving to the database.
hook_block_list_alterAct on blocks prior to rendering.
hook_block_saveSave the configuration options from hook_block_configure().
hook_block_viewReturn a rendered or renderable view of a block.
hook_block_view_alterPerform alterations to the content of a block.
hook_block_view_MODULE_DELTA_alterPerform alterations to a specific block.

File

core/modules/block/block.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Block module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Define all blocks provided by the module.
  12. *
  13. * This hook declares to Drupal what blocks are provided by your module and can
  14. * optionally specify initial block configuration settings.
  15. *
  16. * In hook_block_info(), each block your module provides is given a unique
  17. * identifier referred to as "delta" (the array key in the return value). Delta
  18. * values only need to be unique within your module, and they are used in the
  19. * following ways:
  20. * - Passed into the other block hooks in your module as an argument to identify
  21. * the block being configured or viewed.
  22. * - Used to construct the default HTML ID of "block-MODULE-DELTA" applied to
  23. * each block when it is rendered. This ID may then be used for CSS styling or
  24. * JavaScript programming.
  25. * - Used to define a theming template suggestion of block__MODULE__DELTA, for
  26. * advanced theming possibilities.
  27. * - Used by other modules to identify your block in hook_block_info_alter() and
  28. * other alter hooks.
  29. * The values of delta can be strings or numbers, but because of the uses above
  30. * it is preferable to use descriptive strings whenever possible, and only use a
  31. * numeric identifier if you have to (for instance if your module allows users
  32. * to create several similar blocks that you identify within your module code
  33. * with numeric IDs). The maximum length for delta values is 32 bytes.
  34. *
  35. * @return
  36. * An associative array whose keys define the delta for each block and whose
  37. * values contain the block descriptions. Each block description is itself an
  38. * associative array, with the following key-value pairs:
  39. * - info: (required) The human-readable administrative name of the block.
  40. * This is used to identify the block on administration screens, and
  41. * is not displayed to non-administrative users.
  42. * - cache: (optional) A bitmask describing what kind of caching is
  43. * appropriate for the block. Drupal provides the following bitmask
  44. * constants for defining cache granularity:
  45. * - DRUPAL_CACHE_PER_ROLE (default): The block can change depending on the
  46. * roles the user viewing the page belongs to.
  47. * - DRUPAL_CACHE_PER_USER: The block can change depending on the user
  48. * viewing the page. This setting can be resource-consuming for sites
  49. * with large number of users, and should only be used when
  50. * DRUPAL_CACHE_PER_ROLE is not sufficient.
  51. * - DRUPAL_CACHE_PER_PAGE: The block can change depending on the page
  52. * being viewed.
  53. * - DRUPAL_CACHE_GLOBAL: The block is the same for every user on every
  54. * page where it is visible.
  55. * - DRUPAL_NO_CACHE: The block should not get cached.
  56. * - properties: (optional) Array of additional metadata to add to the block.
  57. * Common properties include:
  58. * - administrative: Boolean that categorizes this block as usable in an
  59. * administrative context. This might include blocks that help an
  60. * administrator approve/deny comments, or view recently created user
  61. * accounts.
  62. * - weight: (optional) Initial value for the ordering weight of this block.
  63. * Most modules do not provide an initial value, and any value provided can
  64. * be modified by a user on the block configuration screen.
  65. * - status: (optional) Initial value for block enabled status. (1 = enabled,
  66. * 0 = disabled). An initial value for 'region' is required for 'status' to
  67. * take effect.
  68. * Most modules do not provide an initial value, and any value provided can
  69. * be modified by a user on the block configuration screen.
  70. * - region: (optional) Initial value for theme region within which this block
  71. * is set. If the specified region is not available in a theme, the block
  72. * will be disabled. The initial value for 'status' must be enabled or the
  73. * initial region value is ignored.
  74. * Most modules do not provide an initial value, and any value provided can
  75. * be modified by a user on the block configuration screen.
  76. * - visibility: (optional) Initial value for the visibility flag, which tells
  77. * how to interpret the 'pages' value. Possible values are:
  78. * - BLOCK_VISIBILITY_NOTLISTED: Show on all pages except listed pages.
  79. * 'pages' lists the paths where the block should not be shown.
  80. * - BLOCK_VISIBILITY_LISTED: Show only on listed pages. 'pages' lists the
  81. * paths where the block should be shown.
  82. * - BLOCK_VISIBILITY_PHP: Use custom PHP code to determine visibility.
  83. * 'pages' gives the PHP code to use.
  84. * Most modules do not provide an initial value for 'visibility' or 'pages',
  85. * and any value provided can be modified by a user on the block
  86. * configuration screen.
  87. * - pages: (optional) See 'visibility' above. A string that contains one or
  88. * more page paths separated by '\n', '\r', or '\r\n' when 'visibility' is
  89. * set to BLOCK_VISIBILITY_NOTLISTED or BLOCK_VISIBILITY_LISTED, or custom
  90. * PHP code when 'visibility' is set to BLOCK_VISIBILITY_PHP. Paths may use
  91. * '*' as a wildcard (matching any number of characters); '<front>'
  92. * designates the site's front page. For BLOCK_VISIBILITY_PHP, the PHP
  93. * code's return value should be TRUE if the block is to be made visible or
  94. * FALSE if the block should not be visible.
  95. *
  96. * For a detailed usage example, see block_example.module.
  97. *
  98. * @see hook_block_configure()
  99. * @see hook_block_save()
  100. * @see hook_block_view()
  101. * @see hook_block_info_alter()
  102. */
  103. function hook_block_info() {
  104. // This example comes from node.module.
  105. $blocks['syndicate'] = array(
  106. 'info' => t('Syndicate'),
  107. 'cache' => DRUPAL_NO_CACHE
  108. );
  109. $blocks['recent'] = array(
  110. 'info' => t('Recent content'),
  111. // DRUPAL_CACHE_PER_ROLE will be assumed.
  112. );
  113. return $blocks;
  114. }
  115. /**
  116. * Change block definition before saving to the database.
  117. *
  118. * @param $blocks
  119. * A multidimensional array of blocks keyed by the defining module and delta;
  120. * the values are blocks returned by hook_block_info(). This hook is fired
  121. * after the blocks are collected from hook_block_info() and the database,
  122. * right before saving back to the database.
  123. * @param $theme
  124. * The theme these blocks belong to.
  125. * @param $code_blocks
  126. * The blocks as defined in hook_block_info() before being overwritten by the
  127. * database data.
  128. *
  129. * @see hook_block_info()
  130. */
  131. function hook_block_info_alter(&$blocks, $theme, $code_blocks) {
  132. // Disable the login block.
  133. $blocks['user']['login']['status'] = 0;
  134. }
  135. /**
  136. * Define a configuration form for a block.
  137. *
  138. * @param $delta
  139. * Which block is being configured. This is a unique identifier for the block
  140. * within the module, defined in hook_block_info().
  141. *
  142. * @return
  143. * A configuration form, if one is needed for your block beyond the standard
  144. * elements that the block module provides (block title, visibility, etc.).
  145. *
  146. * For a detailed usage example, see block_example.module.
  147. *
  148. * @see hook_block_info()
  149. * @see hook_block_save()
  150. */
  151. function hook_block_configure($delta = '') {
  152. // This example comes from node.module.
  153. $form = array();
  154. if ($delta == 'recent') {
  155. $form['node_recent_block_count'] = array(
  156. '#type' => 'select',
  157. '#title' => t('Number of recent content items to display'),
  158. '#default_value' => variable_get('node_recent_block_count', 10),
  159. '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
  160. );
  161. }
  162. return $form;
  163. }
  164. /**
  165. * Save the configuration options from hook_block_configure().
  166. *
  167. * This hook allows you to save the block-specific configuration settings
  168. * defined within your hook_block_configure().
  169. *
  170. * @param $delta
  171. * Which block is being configured. This is a unique identifier for the block
  172. * within the module, defined in hook_block_info().
  173. * @param $edit
  174. * The submitted form data from the configuration form.
  175. *
  176. * For a detailed usage example, see block_example.module.
  177. *
  178. * @see hook_block_configure()
  179. * @see hook_block_info()
  180. */
  181. function hook_block_save($delta = '', $edit = array()) {
  182. // This example comes from node.module.
  183. if ($delta == 'recent') {
  184. variable_set('node_recent_block_count', $edit['node_recent_block_count']);
  185. }
  186. }
  187. /**
  188. * Return a rendered or renderable view of a block.
  189. *
  190. * @param $delta
  191. * Which block to render. This is a unique identifier for the block
  192. * within the module, defined in hook_block_info().
  193. *
  194. * @return
  195. * An array containing the following elements:
  196. * - subject: The default localized title of the block. If the block does not
  197. * have a default title, this should be set to NULL.
  198. * - content: The content of the block's body. This may be a renderable array
  199. * (preferable) or a string containing rendered HTML content.
  200. *
  201. * For a detailed usage example, see block_example.module.
  202. *
  203. * @see hook_block_info()
  204. * @see hook_block_view_alter()
  205. * @see hook_block_view_MODULE_DELTA_alter()
  206. */
  207. function hook_block_view($delta = '') {
  208. // This example is adapted from node.module.
  209. $block = array();
  210. switch ($delta) {
  211. case 'syndicate':
  212. $block['subject'] = t('Syndicate');
  213. $block['content'] = array(
  214. '#theme' => 'feed_icon',
  215. '#url' => 'rss.xml',
  216. '#title' => t('Syndicate'),
  217. );
  218. break;
  219. case 'recent':
  220. if (user_access('access content')) {
  221. $block['subject'] = t('Recent content');
  222. if ($nodes = node_get_recent(variable_get('node_recent_block_count', 10))) {
  223. $block['content'] = array(
  224. '#theme' => 'node_recent_block',
  225. '#nodes' => $nodes,
  226. );
  227. } else {
  228. $block['content'] = t('No content available.');
  229. }
  230. }
  231. break;
  232. }
  233. return $block;
  234. }
  235. /**
  236. * Perform alterations to the content of a block.
  237. *
  238. * This hook allows you to modify any data returned by hook_block_view().
  239. *
  240. * Note that instead of hook_block_view_alter(), which is called for all
  241. * blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a
  242. * specific block.
  243. *
  244. * @param $data
  245. * An array of data, as returned from the hook_block_view() implementation of
  246. * the module that defined the block:
  247. * - subject: The default localized title of the block.
  248. * - content: Either a string or a renderable array representing the content
  249. * of the block. You should check that the content is an array before trying
  250. * to modify parts of the renderable structure.
  251. * @param $block
  252. * The block object, as loaded from the database, having the main properties:
  253. * - module: The name of the module that defined the block.
  254. * - delta: The unique identifier for the block within that module, as defined
  255. * in hook_block_info().
  256. *
  257. * @see hook_block_view_MODULE_DELTA_alter()
  258. * @see hook_block_view()
  259. */
  260. function hook_block_view_alter(&$data, $block) {
  261. // Remove the contextual links on all blocks that provide them.
  262. if (is_array($data['content']) && isset($data['content']['#contextual_links'])) {
  263. unset($data['content']['#contextual_links']);
  264. }
  265. // Add a theme wrapper function defined by the current module to all blocks
  266. // provided by the "somemodule" module.
  267. if (is_array($data['content']) && $block->module == 'somemodule') {
  268. $data['content']['#theme_wrappers'][] = 'mymodule_special_block';
  269. }
  270. }
  271. /**
  272. * Perform alterations to a specific block.
  273. *
  274. * Modules can implement hook_block_view_MODULE_DELTA_alter() to modify a
  275. * specific block, rather than implementing hook_block_view_alter().
  276. *
  277. * @param $data
  278. * An array of data, as returned from the hook_block_view() implementation of
  279. * the module that defined the block:
  280. * - subject: The localized title of the block.
  281. * - content: Either a string or a renderable array representing the content
  282. * of the block. You should check that the content is an array before trying
  283. * to modify parts of the renderable structure.
  284. * @param $block
  285. * The block object, as loaded from the database, having the main properties:
  286. * - module: The name of the module that defined the block.
  287. * - delta: The unique identifier for the block within that module, as defined
  288. * in hook_block_info().
  289. *
  290. * @see hook_block_view_alter()
  291. * @see hook_block_view()
  292. */
  293. function hook_block_view_MODULE_DELTA_alter(&$data, $block) {
  294. // This code will only run for a specific block. For example, if MODULE_DELTA
  295. // in the function definition above is set to "mymodule_somedelta", the code
  296. // will only run on the "somedelta" block provided by the "mymodule" module.
  297. // Change the title of the "somedelta" block provided by the "mymodule"
  298. // module.
  299. $data['subject'] = t('New title of the block');
  300. }
  301. /**
  302. * Act on blocks prior to rendering.
  303. *
  304. * This hook allows you to add, remove or modify blocks in the block list. The
  305. * block list contains the block definitions, not the rendered blocks. The
  306. * blocks are rendered after the modules have had a chance to manipulate the
  307. * block list.
  308. *
  309. * You can also set $block->content here, which will override the content of the
  310. * block and prevent hook_block_view() from running.
  311. *
  312. * @param $blocks
  313. * An array of $blocks, keyed by the block ID.
  314. */
  315. function hook_block_list_alter(&$blocks) {
  316. global $language_interface, $theme_key;
  317. // This example shows how to achieve language specific visibility setting for
  318. // blocks.
  319. $result = db_query('SELECT module, delta, language FROM {my_table}');
  320. $block_languages = array();
  321. foreach ($result as $record) {
  322. $block_languages[$record->module][$record->delta][$record->language] = TRUE;
  323. }
  324. foreach ($blocks as $key => $block) {
  325. // Any module using this alter should inspect the data before changing it,
  326. // to ensure it is what they expect.
  327. if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
  328. // This block was added by a contrib module, leave it in the list.
  329. continue;
  330. }
  331. if (!isset($block_languages[$block->module][$block->delta])) {
  332. // No language setting for this block, leave it in the list.
  333. continue;
  334. }
  335. if (!isset($block_languages[$block->module][$block->delta][$language_interface->language])) {
  336. // This block should not be displayed with the active language, remove
  337. // from the list.
  338. unset($blocks[$key]);
  339. }
  340. }
  341. }
  342. /**
  343. * @} End of "addtogroup hooks".
  344. */
Login or register to post comments