block.api.php 14 KB

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