block.module

  1. 7.x drupal-7.x/modules/block/block.module
  2. 6.x drupal-6.x/modules/block/block.module

Controls the boxes that are displayed around the main content.

File

drupal-6.x/modules/block/block.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Controls the boxes that are displayed around the main content.
  5. */
  6. /**
  7. * Denotes that a block is not enabled in any region and should not
  8. * be shown.
  9. */
  10. define('BLOCK_REGION_NONE', -1);
  11. /**
  12. * Constants defining cache granularity for blocks.
  13. *
  14. * Modules specify the caching patterns for their blocks using binary
  15. * combinations of these constants in their hook_block(op 'list'):
  16. * $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE;
  17. * BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is
  18. * specified.
  19. *
  20. * The block cache is cleared in cache_clear_all(), and uses the same clearing
  21. * policy than page cache (node, comment, user, taxonomy added or updated...).
  22. * Blocks requiring more fine-grained clearing might consider disabling the
  23. * built-in block cache (BLOCK_NO_CACHE) and roll their own.
  24. *
  25. * Note that user 1 is excluded from block caching.
  26. */
  27. /**
  28. * The block should not get cached. This setting should be used:
  29. * - for simple blocks (notably those that do not perform any db query),
  30. * where querying the db cache would be more expensive than directly generating
  31. * the content.
  32. * - for blocks that change too frequently.
  33. */
  34. define('BLOCK_NO_CACHE', -1);
  35. /**
  36. * The block can change depending on the roles the user viewing the page belongs to.
  37. * This is the default setting, used when the block does not specify anything.
  38. */
  39. define('BLOCK_CACHE_PER_ROLE', 0x0001);
  40. /**
  41. * The block can change depending on the user viewing the page.
  42. * This setting can be resource-consuming for sites with large number of users,
  43. * and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient.
  44. */
  45. define('BLOCK_CACHE_PER_USER', 0x0002);
  46. /**
  47. * The block can change depending on the page being viewed.
  48. */
  49. define('BLOCK_CACHE_PER_PAGE', 0x0004);
  50. /**
  51. * The block is the same for every user on every page where it is visible.
  52. */
  53. define('BLOCK_CACHE_GLOBAL', 0x0008);
  54. /**
  55. * Implementation of hook_help().
  56. */
  57. function block_help($path, $arg) {
  58. switch ($path) {
  59. case 'admin/help#block':
  60. $output = '<p>'. t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Garland, for example, implements the regions "left sidebar", "right sidebar", "content", "header", and "footer", and a block may appear in any one of these areas. The <a href="@blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/build/block'))) .'</p>';
  61. $output .= '<p>'. t('Although blocks are usually generated automatically by modules (like the <em>User login</em> block, for example), administrators can also define custom blocks. Custom blocks have a title, description, and body. The body of the block can be as long as necessary, and can contain content supported by any available <a href="@input-format">input format</a>.', array('@input-format' => url('admin/settings/filters'))) .'</p>';
  62. $output .= '<p>'. t('When working with blocks, remember that:') .'</p>';
  63. $output .= '<ul><li>'. t('since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis.') .'</li>';
  64. $output .= '<li>'. t('disabled blocks, or blocks not in a region, are never shown.') .'</li>';
  65. $output .= '<li>'. t('when throttle module is enabled, throttled blocks (blocks with the <em>Throttle</em> checkbox selected) are hidden during high server loads.') .'</li>';
  66. $output .= '<li>'. t('blocks can be configured to be visible only on certain pages.') .'</li>';
  67. $output .= '<li>'. t('blocks can be configured to be visible only when specific conditions are true.') .'</li>';
  68. $output .= '<li>'. t('blocks can be configured to be visible only for certain user roles.') .'</li>';
  69. $output .= '<li>'. t('when allowed by an administrator, specific blocks may be enabled or disabled on a per-user basis using the <em>My account</em> page.') .'</li>';
  70. $output .= '<li>'. t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') .'</li></ul>';
  71. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
  72. return $output;
  73. case 'admin/build/block':
  74. $throttle = module_exists('throttle');
  75. $output = '<p>'. t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. To change the region or order of a block, grab a drag-and-drop handle under the <em>Block</em> column and drag the block to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page.') .'</p>';
  76. if ($throttle) {
  77. $output .= '<p>'. t('To reduce CPU usage, database traffic or bandwidth, blocks may be automatically disabled during high server loads by selecting their <em>Throttle</em> checkbox. Adjust throttle thresholds on the <a href="@throttleconfig">throttle configuration page</a>.', array('@throttleconfig' => url('admin/settings/throttle'))) .'</p>';
  78. }
  79. $output .= '<p>'. t('Click the <em>configure</em> link next to each block to configure its specific title and visibility settings. Use the <a href="@add-block">add block page</a> to create a custom block.', array('@add-block' => url('admin/build/block/add'))) .'</p>';
  80. return $output;
  81. case 'admin/build/block/add':
  82. return '<p>'. t('Use this page to create a new custom block. New blocks are disabled by default, and must be moved to a region on the <a href="@blocks">blocks administration page</a> to be visible.', array('@blocks' => url('admin/build/block'))) .'</p>';
  83. }
  84. }
  85. /**
  86. * Implementation of hook_theme()
  87. */
  88. function block_theme() {
  89. return array(
  90. 'block_admin_display_form' => array(
  91. 'template' => 'block-admin-display-form',
  92. 'file' => 'block.admin.inc',
  93. 'arguments' => array('form' => NULL),
  94. ),
  95. );
  96. }
  97. /**
  98. * Implementation of hook_perm().
  99. */
  100. function block_perm() {
  101. return array('administer blocks', 'use PHP for block visibility');
  102. }
  103. /**
  104. * Implementation of hook_menu().
  105. */
  106. function block_menu() {
  107. $items['admin/build/block'] = array(
  108. 'title' => 'Blocks',
  109. 'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
  110. 'page callback' => 'block_admin_display',
  111. 'access arguments' => array('administer blocks'),
  112. 'file' => 'block.admin.inc',
  113. );
  114. $items['admin/build/block/list'] = array(
  115. 'title' => 'List',
  116. 'type' => MENU_DEFAULT_LOCAL_TASK,
  117. 'weight' => -10,
  118. );
  119. $items['admin/build/block/list/js'] = array(
  120. 'title' => 'JavaScript List Form',
  121. 'page callback' => 'block_admin_display_js',
  122. 'access arguments' => array('administer blocks'),
  123. 'type' => MENU_CALLBACK,
  124. 'file' => 'block.admin.inc',
  125. );
  126. $items['admin/build/block/configure'] = array(
  127. 'title' => 'Configure block',
  128. 'page callback' => 'drupal_get_form',
  129. 'page arguments' => array('block_admin_configure'),
  130. 'access arguments' => array('administer blocks'),
  131. 'type' => MENU_CALLBACK,
  132. 'file' => 'block.admin.inc',
  133. );
  134. $items['admin/build/block/delete'] = array(
  135. 'title' => 'Delete block',
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('block_box_delete'),
  138. 'access arguments' => array('administer blocks'),
  139. 'type' => MENU_CALLBACK,
  140. 'file' => 'block.admin.inc',
  141. );
  142. $items['admin/build/block/add'] = array(
  143. 'title' => 'Add block',
  144. 'page callback' => 'drupal_get_form',
  145. 'page arguments' => array('block_add_block_form'),
  146. 'access arguments' => array('administer blocks'),
  147. 'type' => MENU_LOCAL_TASK,
  148. 'file' => 'block.admin.inc',
  149. );
  150. $default = variable_get('theme_default', 'garland');
  151. foreach (list_themes() as $key => $theme) {
  152. $items['admin/build/block/list/'. $key] = array(
  153. 'title' => check_plain($theme->info['name']),
  154. 'page arguments' => array($key),
  155. 'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  156. 'weight' => $key == $default ? -10 : 0,
  157. 'file' => 'block.admin.inc',
  158. 'access callback' => '_block_themes_access',
  159. 'access arguments' => array($theme),
  160. );
  161. }
  162. return $items;
  163. }
  164. /**
  165. * Menu item access callback - only admin or enabled themes can be accessed
  166. */
  167. function _block_themes_access($theme) {
  168. return user_access('administer blocks') && ($theme->status || $theme->name == variable_get('admin_theme', '0'));
  169. }
  170. /**
  171. * Implementation of hook_block().
  172. *
  173. * Generates the administrator-defined blocks for display.
  174. */
  175. function block_block($op = 'list', $delta = 0, $edit = array()) {
  176. switch ($op) {
  177. case 'list':
  178. $blocks = array();
  179. $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info');
  180. while ($block = db_fetch_object($result)) {
  181. $blocks[$block->bid]['info'] = $block->info;
  182. // Not worth caching.
  183. $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE;
  184. }
  185. return $blocks;
  186. case 'configure':
  187. $box = array('format' => FILTER_FORMAT_DEFAULT);
  188. if ($delta) {
  189. $box = block_box_get($delta);
  190. }
  191. if (filter_access($box['format'])) {
  192. return block_box_form($box);
  193. }
  194. break;
  195. case 'save':
  196. block_box_save($edit, $delta);
  197. break;
  198. case 'view':
  199. $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta));
  200. $data['content'] = check_markup($block->body, $block->format, FALSE);
  201. return $data;
  202. }
  203. }
  204. /**
  205. * Update the 'blocks' DB table with the blocks currently exported by modules.
  206. *
  207. * @param $theme
  208. * The theme to rehash blocks for. If not provided, defaults to the currently
  209. * used theme.
  210. *
  211. * @return
  212. * Blocks currently exported by modules.
  213. */
  214. function _block_rehash($theme = NULL) {
  215. global $theme_key;
  216. init_theme();
  217. if (!isset($theme)) {
  218. $theme = $theme_key;
  219. }
  220. $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme);
  221. $old_blocks = array();
  222. while ($old_block = db_fetch_array($result)) {
  223. $old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
  224. }
  225. $blocks = array();
  226. // Valid region names for the theme.
  227. $regions = system_region_list($theme);
  228. foreach (module_list() as $module) {
  229. $module_blocks = module_invoke($module, 'block', 'list');
  230. if ($module_blocks) {
  231. foreach ($module_blocks as $delta => $block) {
  232. if (empty($old_blocks[$module][$delta])) {
  233. // If it's a new block, add identifiers.
  234. $block['module'] = $module;
  235. $block['delta'] = $delta;
  236. $block['theme'] = $theme;
  237. if (!isset($block['pages'])) {
  238. // {block}.pages is type 'text', so it cannot have a
  239. // default value, and not null, so we need to provide
  240. // value if the module did not.
  241. $block['pages'] = '';
  242. }
  243. // Add defaults and save it into the database.
  244. drupal_write_record('blocks', $block);
  245. // Set region to none if not enabled.
  246. $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
  247. // Add to the list of blocks we return.
  248. $blocks[] = $block;
  249. }
  250. else {
  251. // If it's an existing block, database settings should overwrite
  252. // the code. The only exceptions are 'cache' which is only definable
  253. // and updatable in the code, and 'info' which is not stored in
  254. // the database.
  255. // Update the cache mode only; the other values don't need to change.
  256. if (isset($block['cache']) && $block['cache'] != $old_blocks[$module][$delta]['cache']) {
  257. db_query("UPDATE {blocks} SET cache = %d WHERE bid = %d", $block['cache'], $old_blocks[$module][$delta]['bid']);
  258. }
  259. // Add 'info' to this block.
  260. $old_blocks[$module][$delta]['info'] = $block['info'];
  261. // If the region name does not exist, disable the block and assign it to none.
  262. if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
  263. drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning');
  264. $old_blocks[$module][$delta]['status'] = 0;
  265. $old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
  266. }
  267. else {
  268. $old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
  269. }
  270. // Add this block to the list of blocks we return.
  271. $blocks[] = $old_blocks[$module][$delta];
  272. // Remove this block from the list of blocks to be deleted.
  273. unset($old_blocks[$module][$delta]);
  274. }
  275. }
  276. }
  277. }
  278. // Remove blocks that are no longer defined by the code from the database.
  279. foreach ($old_blocks as $module => $old_module_blocks) {
  280. // This cleanup does not apply to disabled modules, to avoid configuration
  281. // being lost when modules are disabled.
  282. if (module_exists($module)) {
  283. foreach ($old_module_blocks as $delta => $block) {
  284. db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme);
  285. }
  286. }
  287. }
  288. return $blocks;
  289. }
  290. /**
  291. * Implementation of hook_flush_caches().
  292. */
  293. function block_flush_caches() {
  294. // Rehash blocks for active themes. We don't use list_themes() here,
  295. // because if MAINTENANCE_MODE is defined it skips reading the database,
  296. // and we can't tell which themes are active.
  297. $result = db_query("SELECT name FROM {system} WHERE type = 'theme' AND status = 1");
  298. while ($theme = db_result($result)) {
  299. _block_rehash($theme);
  300. }
  301. }
  302. /**
  303. * Returns information from database about a user-created (custom) block.
  304. *
  305. * @param $bid
  306. * ID of the block to get information for.
  307. * @return
  308. * Associative array of information stored in the database for this block.
  309. * Array keys:
  310. * - bid: Block ID.
  311. * - info: Block description.
  312. * - body: Block contents.
  313. * - format: Filter ID of the filter format for the body.
  314. */
  315. function block_box_get($bid) {
  316. return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
  317. }
  318. /**
  319. * Define the custom block form.
  320. */
  321. function block_box_form($edit = array()) {
  322. $edit += array(
  323. 'info' => '',
  324. 'body' => '',
  325. );
  326. $form['info'] = array(
  327. '#type' => 'textfield',
  328. '#title' => t('Block description'),
  329. '#default_value' => $edit['info'],
  330. '#maxlength' => 64,
  331. '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))),
  332. '#required' => TRUE,
  333. '#weight' => -19,
  334. );
  335. $form['body_field']['#weight'] = -17;
  336. $form['body_field']['body'] = array(
  337. '#type' => 'textarea',
  338. '#title' => t('Block body'),
  339. '#default_value' => $edit['body'],
  340. '#rows' => 15,
  341. '#description' => t('The content of the block as shown to the user.'),
  342. '#weight' => -17,
  343. );
  344. if (!isset($edit['format'])) {
  345. $edit['format'] = FILTER_FORMAT_DEFAULT;
  346. }
  347. $form['body_field']['format'] = filter_form($edit['format'], -16);
  348. return $form;
  349. }
  350. /**
  351. * Saves a user-created block in the database.
  352. *
  353. * @param $edit
  354. * Associative array of fields to save. Array keys:
  355. * - info: Block description.
  356. * - body: Block contents.
  357. * - format: Filter ID of the filter format for the body.
  358. * @param $delta
  359. * Block ID of the block to save.
  360. * @return
  361. * Always returns TRUE.
  362. */
  363. function block_box_save($edit, $delta) {
  364. if (!filter_access($edit['format'])) {
  365. $edit['format'] = FILTER_FORMAT_DEFAULT;
  366. }
  367. db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
  368. return TRUE;
  369. }
  370. /**
  371. * Implementation of hook_user().
  372. *
  373. * Allow users to decide which custom blocks to display when they visit
  374. * the site.
  375. */
  376. function block_user($type, $edit, &$account, $category = NULL) {
  377. switch ($type) {
  378. case 'form':
  379. if ($category == 'account') {
  380. $rids = array_keys($account->roles);
  381. $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids);
  382. $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
  383. while ($block = db_fetch_object($result)) {
  384. $data = module_invoke($block->module, 'block', 'list');
  385. if ($data[$block->delta]['info']) {
  386. $return = TRUE;
  387. $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1));
  388. }
  389. }
  390. if (!empty($return)) {
  391. return $form;
  392. }
  393. }
  394. break;
  395. case 'validate':
  396. if (empty($edit['block'])) {
  397. $edit['block'] = array();
  398. }
  399. return $edit;
  400. }
  401. }
  402. /**
  403. * Return all blocks in the specified region for the current user.
  404. *
  405. * @param $region
  406. * The name of a region.
  407. *
  408. * @return
  409. * An array of block objects, indexed with module name and block delta
  410. * concatenated with an underscore, thus: MODULE_DELTA. If you are displaying
  411. * your blocks in one or two sidebars, you may check whether this array is
  412. * empty to see how many columns are going to be displayed.
  413. *
  414. * @todo
  415. * Now that the blocks table has a primary key, we should use that as the
  416. * array key instead of MODULE_DELTA.
  417. */
  418. function block_list($region) {
  419. global $user, $theme_key;
  420. static $blocks = array();
  421. if (!count($blocks)) {
  422. $rids = array_keys($user->roles);
  423. $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array($theme_key), $rids));
  424. while ($block = db_fetch_object($result)) {
  425. if (!isset($blocks[$block->region])) {
  426. $blocks[$block->region] = array();
  427. }
  428. // Use the user's block visibility setting, if necessary
  429. if ($block->custom != 0) {
  430. if ($user->uid && isset($user->block[$block->module][$block->delta])) {
  431. $enabled = $user->block[$block->module][$block->delta];
  432. }
  433. else {
  434. $enabled = ($block->custom == 1);
  435. }
  436. }
  437. else {
  438. $enabled = TRUE;
  439. }
  440. // Match path if necessary
  441. if ($block->pages) {
  442. if ($block->visibility < 2) {
  443. $path = drupal_get_path_alias($_GET['q']);
  444. // Compare with the internal and path alias (if any).
  445. $page_match = drupal_match_path($path, $block->pages);
  446. if ($path != $_GET['q']) {
  447. $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
  448. }
  449. // When $block->visibility has a value of 0, the block is displayed on
  450. // all pages except those listed in $block->pages. When set to 1, it
  451. // is displayed only on those pages listed in $block->pages.
  452. $page_match = !($block->visibility xor $page_match);
  453. }
  454. else {
  455. $page_match = drupal_eval($block->pages);
  456. }
  457. }
  458. else {
  459. $page_match = TRUE;
  460. }
  461. $block->enabled = $enabled;
  462. $block->page_match = $page_match;
  463. $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
  464. }
  465. }
  466. // Create an empty array if there were no entries
  467. if (!isset($blocks[$region])) {
  468. $blocks[$region] = array();
  469. }
  470. foreach ($blocks[$region] as $key => $block) {
  471. // Render the block content if it has not been created already.
  472. if (!isset($block->content)) {
  473. // Erase the block from the static array - we'll put it back if it has content.
  474. unset($blocks[$region][$key]);
  475. if ($block->enabled && $block->page_match) {
  476. // Check the current throttle status and see if block should be displayed
  477. // based on server load.
  478. if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
  479. // Try fetching the block from cache. Block caching is not compatible with
  480. // node_access modules. We also preserve the submission of forms in blocks,
  481. // by fetching from cache only if the request method is 'GET'.
  482. if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
  483. $array = $cache->data;
  484. }
  485. else {
  486. $array = module_invoke($block->module, 'block', 'view', $block->delta);
  487. if (isset($cid)) {
  488. cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
  489. }
  490. }
  491. if (isset($array) && is_array($array)) {
  492. foreach ($array as $k => $v) {
  493. $block->$k = $v;
  494. }
  495. }
  496. }
  497. if (isset($block->content) && $block->content) {
  498. // Override default block title if a custom display title is present.
  499. if ($block->title) {
  500. // Check plain here to allow module generated titles to keep any markup.
  501. $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
  502. }
  503. if (!isset($block->subject)) {
  504. $block->subject = '';
  505. }
  506. $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
  507. }
  508. }
  509. }
  510. }
  511. return $blocks[$region];
  512. }
  513. /**
  514. * Assemble the cache_id to use for a given block.
  515. *
  516. * The cache_id string reflects the viewing context for the current block
  517. * instance, obtained by concatenating the relevant context information
  518. * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_*
  519. * constants). Two block instances can use the same cached content when
  520. * they share the same cache_id.
  521. *
  522. * Theme and language contexts are automatically differenciated.
  523. *
  524. * @param $block
  525. * @return
  526. * The string used as cache_id for the block.
  527. */
  528. function _block_get_cache_id($block) {
  529. global $theme, $base_root, $user;
  530. // User 1 being out of the regular 'roles define permissions' schema,
  531. // it brings too many chances of having unwanted output get in the cache
  532. // and later be served to other users. We therefore exclude user 1 from
  533. // block caching.
  534. if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) {
  535. $cid_parts = array();
  536. // Start with common sub-patterns: block identification, theme, language.
  537. $cid_parts[] = $block->module;
  538. $cid_parts[] = $block->delta;
  539. $cid_parts[] = $theme;
  540. if (module_exists('locale')) {
  541. global $language;
  542. $cid_parts[] = $language->language;
  543. }
  544. // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a
  545. // resource drag for sites with many users, so when a module is being
  546. // equivocal, we favor the less expensive 'PER_ROLE' pattern.
  547. if ($block->cache & BLOCK_CACHE_PER_ROLE) {
  548. $cid_parts[] = 'r.'. implode(',', array_keys($user->roles));
  549. }
  550. elseif ($block->cache & BLOCK_CACHE_PER_USER) {
  551. $cid_parts[] = "u.$user->uid";
  552. }
  553. if ($block->cache & BLOCK_CACHE_PER_PAGE) {
  554. $cid_parts[] = $base_root . request_uri();
  555. }
  556. return implode(':', $cid_parts);
  557. }
  558. }