1. 8.2.x core/modules/block/block.module
  2. 8.0.x core/modules/block/block.module
  3. 8.1.x core/modules/block/block.module
  4. 8.3.x core/modules/block/block.module
  5. 4.6.x modules/block.module
  6. 4.7.x modules/block.module
  7. 5.x modules/block/block.module
  8. 6.x modules/block/block.module
  9. 7.x modules/block/block.module

Controls the boxes that are displayed around the main content.

File

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. * Implementation of hook_help().
  13. */
  14. function block_help($section) {
  15. switch ($section) {
  16. case 'admin/help#block':
  17. $output = '<p>'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'</p>';
  18. $output .= '<p>'. t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') .'</p>';
  19. $output .= '<p>'. t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region; heavier ones "sink".') .'</p>';
  20. $output .= t("<p>A block's visibility depends on:</p>
  21. <ul>
  22. <li>Its region placement. Blocks with no region assigned to them are never shown.</li>
  23. <li>Its throttle checkbox when throttle module is enabled. Throttled blocks are hidden during high server loads.</li>
  24. <li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
  25. <li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
  26. <li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
  27. <li>Its user-role visibility settings. Administrators can choose to let blocks be visible only for certain user roles.</li>
  28. <li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
  29. </ul>
  30. ");
  31. $output .= '<h3>'. t('Module blocks') .'</h3>';
  32. $output .= '<p>'. t('Some modules generate blocks that become available when the modules are enabled. These blocks can be administered via the <a href="@admin-block">blocks administration page</a>.</p>', array('@admin-block' => url('admin/build/block'))) .'</p>';
  33. $output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
  34. $output .= '<p>'. t('Administrators can also define custom blocks. These blocks consist of a title, a description, and a body which can be as long as you wish. Block content can be in any of the input formats supported for other content.') .'</p>';
  35. $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@block">Block page</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
  36. return $output;
  37. case 'admin/build/block':
  38. return t('<p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
  39. <p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar). Highlighted labels on this page show the regions into which blocks can be rendered. You can specify where within a region a block will appear by adjusting its weight.</p>
  40. <p>If you want certain blocks to disable themselves temporarily during high server loads, check the "Throttle" box. You can configure the auto-throttle on the <a href="@throttle">throttle configuration page</a> after having enabled the throttle module.</p>
  41. <p>You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the "configure" link for each block.</p>', array('@throttle' => url('admin/settings/throttle')));
  42. case 'admin/build/block/add':
  43. return '<p>'. t('Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="@overview">blocks</a>. The description is used in the "block" column on the <a href="@overview">blocks</a> page.', array('@overview' => url('admin/build/block'))) .'</p>';
  44. }
  45. }
  46. /**
  47. * Implementation of hook_perm().
  48. */
  49. function block_perm() {
  50. return array('administer blocks', 'use PHP for block visibility');
  51. }
  52. /**
  53. * Implementation of hook_menu().
  54. */
  55. function block_menu($may_cache) {
  56. $items = array();
  57. if ($may_cache) {
  58. $items[] = array(
  59. 'path' => 'admin/build/block',
  60. 'title' => t('Blocks'),
  61. 'access' => user_access('administer blocks'),
  62. 'description' => t('Configure what block content appears in your site\'s sidebars and other regions.'),
  63. 'callback' => 'drupal_get_form',
  64. 'callback arguments' => array('block_admin_display'));
  65. $items[] = array('path' => 'admin/build/block/list', 'title' => t('List'),
  66. 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
  67. $items[] = array('path' => 'admin/build/block/configure', 'title' => t('Configure block'),
  68. 'access' => user_access('administer blocks'),
  69. 'callback' => 'drupal_get_form',
  70. 'callback arguments' => array('block_admin_configure'),
  71. 'type' => MENU_CALLBACK);
  72. $items[] = array('path' => 'admin/build/block/delete', 'title' => t('Delete block'),
  73. 'access' => user_access('administer blocks'),
  74. 'callback' => 'drupal_get_form',
  75. 'callback arguments' => array('block_box_delete'),
  76. 'type' => MENU_CALLBACK);
  77. $items[] = array('path' => 'admin/build/block/add', 'title' => t('Add block'),
  78. 'access' => user_access('administer blocks'),
  79. 'callback' => 'drupal_get_form',
  80. 'callback arguments' => array('block_box_form'),
  81. 'type' => MENU_LOCAL_TASK);
  82. $default = variable_get('theme_default', 'garland');
  83. foreach (list_themes() as $key => $theme) {
  84. $items[] = array(
  85. 'path' => 'admin/build/block/list/'. $key,
  86. 'title' => t('!key settings', array('!key' => $key)),
  87. 'callback' => 'drupal_get_form',
  88. 'callback arguments' => array('block_admin_display', $key),
  89. 'access' => user_access('administer blocks'),
  90. 'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  91. 'weight' => $key == $default ? -10 : 0,
  92. );
  93. }
  94. }
  95. return $items;
  96. }
  97. /**
  98. * Implementation of hook_block().
  99. *
  100. * Generates the administrator-defined blocks for display.
  101. */
  102. function block_block($op = 'list', $delta = 0, $edit = array()) {
  103. switch ($op) {
  104. case 'list':
  105. $blocks = array();
  106. $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info');
  107. while ($block = db_fetch_object($result)) {
  108. $blocks[$block->bid]['info'] = $block->info;
  109. }
  110. return $blocks;
  111. case 'configure':
  112. $box = block_box_get($delta);
  113. if (filter_access($box['format'])) {
  114. return block_box_form($box);
  115. }
  116. break;
  117. case 'save':
  118. block_box_save($edit, $delta);
  119. break;
  120. case 'view':
  121. $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta));
  122. $data['content'] = check_markup($block->body, $block->format, FALSE);
  123. return $data;
  124. }
  125. }
  126. /**
  127. * Update the 'blocks' DB table with the blocks currently exported by modules.
  128. *
  129. * @return
  130. * Blocks currently exported by modules.
  131. */
  132. function _block_rehash() {
  133. global $theme_key;
  134. init_theme();
  135. $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
  136. while ($old_block = db_fetch_object($result)) {
  137. $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  138. }
  139. $blocks = array();
  140. foreach (module_list() as $module) {
  141. $module_blocks = module_invoke($module, 'block', 'list');
  142. if ($module_blocks) {
  143. foreach ($module_blocks as $delta => $block) {
  144. $block['module'] = $module;
  145. $block['delta'] = $delta;
  146. // If previously written to database, load values.
  147. if ($old_blocks[$module][$delta]) {
  148. $block['status'] = $old_blocks[$module][$delta]->status;
  149. $block['weight'] = $old_blocks[$module][$delta]->weight;
  150. $block['region'] = $old_blocks[$module][$delta]->region;
  151. $block['visibility'] = $old_blocks[$module][$delta]->visibility;
  152. $block['pages'] = $old_blocks[$module][$delta]->pages;
  153. $block['custom'] = $old_blocks[$module][$delta]->custom;
  154. $block['throttle'] = $old_blocks[$module][$delta]->throttle;
  155. $block['title'] = $old_blocks[$module][$delta]->title;
  156. }
  157. // Otherwise, use any set values, or else substitute defaults.
  158. else {
  159. $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0, 'title' => '');
  160. foreach ($properties as $property => $default) {
  161. if (!isset($block[$property])) {
  162. $block[$property] = $default;
  163. }
  164. }
  165. }
  166. $blocks[] = $block;
  167. }
  168. }
  169. }
  170. db_lock_table('blocks');
  171. // Remove all blocks from table.
  172. db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
  173. // Reinsert new set of blocks into table.
  174. foreach ($blocks as $block) {
  175. db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title']);
  176. }
  177. db_unlock_tables();
  178. return $blocks;
  179. }
  180. /**
  181. * Generate main block administration form.
  182. */
  183. function block_admin_display($theme = NULL) {
  184. global $theme_key, $custom_theme;
  185. // Add CSS
  186. drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
  187. // If non-default theme configuration has been selected, set the custom theme.
  188. if ($theme) {
  189. $custom_theme = $theme;
  190. }
  191. else {
  192. $custom_theme = variable_get('theme_default', 'garland');
  193. }
  194. init_theme();
  195. // Fetch and sort blocks
  196. $blocks = _block_rehash();
  197. usort($blocks, '_block_compare');
  198. $throttle = module_exists('throttle');
  199. $block_regions = array(BLOCK_REGION_NONE => '<'. t('none') .'>') + system_region_list($theme_key);
  200. // Build form tree
  201. $form['#action'] = arg(3) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block');
  202. $form['#tree'] = TRUE;
  203. foreach ($blocks as $i => $block) {
  204. $form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
  205. $form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
  206. $form[$i]['info'] = array('#value' => check_plain($block['info']));
  207. $form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
  208. $form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
  209. $form[$i]['region'] = array('#type' => 'select',
  210. '#default_value' => $block['status'] ? (isset($block['region']) ? $block['region'] : system_default_region($theme_key)) : BLOCK_REGION_NONE,
  211. '#options' => $block_regions,
  212. );
  213. if ($throttle) {
  214. $form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
  215. }
  216. $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
  217. if ($block['module'] == 'block') {
  218. $form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
  219. }
  220. }
  221. $form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
  222. return $form;
  223. }
  224. /**
  225. * Helper function for sorting blocks on admin/build/block.
  226. *
  227. * Active blocks are sorted by region, then by weight.
  228. * Disabled blocks are sorted by name.
  229. */
  230. function _block_compare($a, $b) {
  231. $status = $b['status'] - $a['status'];
  232. // Separate enabled from disabled.
  233. if ($status) {
  234. return $status;
  235. }
  236. // Enabled blocks
  237. if ($a['status']) {
  238. $place = strcmp($a['region'], $b['region']);
  239. return $place ? $place : ($a['weight'] - $b['weight']);
  240. }
  241. // Disabled blocks
  242. else {
  243. return strcmp($a['info'], $b['info']);
  244. }
  245. }
  246. /**
  247. * Process main block administration form submission.
  248. */
  249. function block_admin_display_submit($form_id, $form_values) {
  250. foreach ($form_values as $block) {
  251. $block['status'] = $block['region'] != BLOCK_REGION_NONE;
  252. $block['region'] = $block['status'] ? $block['region'] : '';
  253. db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], isset($block['throttle']) ? $block['throttle'] : 0, $block['module'], $block['delta'], $block['theme']);
  254. }
  255. drupal_set_message(t('The block settings have been updated.'));
  256. cache_clear_all();
  257. }
  258. /**
  259. * Theme main block administration form submission.
  260. *
  261. * Note: the blocks are already sorted in the right order,
  262. * grouped by status, region and weight.
  263. */
  264. function theme_block_admin_display($form) {
  265. global $theme_key;
  266. $throttle = module_exists('throttle');
  267. $block_regions = system_region_list($theme_key);
  268. // Highlight regions on page to provide visual reference.
  269. foreach ($block_regions as $key => $value) {
  270. drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
  271. }
  272. // Build rows
  273. $rows = array();
  274. $last_region = '';
  275. $last_status = 1;
  276. foreach (element_children($form) as $i) {
  277. $block = &$form[$i];
  278. // Only take form elements that are blocks.
  279. if (is_array($block['info'])) {
  280. // Fetch values
  281. $region = $block['region']['#default_value'];
  282. $status = $region != BLOCK_REGION_NONE;
  283. // Output region header
  284. if ($status && $region != $last_region) {
  285. $region_title = t('@region', array('@region' => drupal_ucfirst($block_regions[$region])));
  286. $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 6 : 5)));
  287. $last_region = $region;
  288. }
  289. // Output disabled header
  290. elseif ($status != $last_status) {
  291. $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 6 : 5)));
  292. $last_status = $status;
  293. }
  294. // Generate block row
  295. $row = array(
  296. array('data' => drupal_render($block['info']), 'class' => 'block'),
  297. drupal_render($block['region']) . drupal_render($block['theme']),
  298. drupal_render($block['weight']),
  299. );
  300. if ($throttle) {
  301. $row[] = drupal_render($block['throttle']);
  302. }
  303. $row[] = drupal_render($block['configure']);
  304. $row[] = $block['delete'] ? drupal_render($block['delete']) : '';
  305. $rows[] = $row;
  306. }
  307. }
  308. // Finish table
  309. $header = array(t('Block'), t('Region'), t('Weight'));
  310. if ($throttle) {
  311. $header[] = t('Throttle');
  312. }
  313. $header[] = array('data' => t('Operations'), 'colspan' => 2);
  314. $output = theme('table', $header, $rows, array('id' => 'blocks'));
  315. $output .= drupal_render($form);
  316. return $output;
  317. }
  318. function block_box_get($bid) {
  319. return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
  320. }
  321. /**
  322. * Menu callback; displays the block configuration form.
  323. */
  324. function block_admin_configure($module = NULL, $delta = 0) {
  325. $form['module'] = array('#type' => 'value', '#value' => $module);
  326. $form['delta'] = array('#type' => 'value', '#value' => $delta);
  327. $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
  328. $form['block_settings'] = array(
  329. '#type' => 'fieldset',
  330. '#title' => t('Block specific settings'),
  331. '#collapsible' => TRUE,
  332. );
  333. $form['block_settings']['title'] = array(
  334. '#type' => 'textfield',
  335. '#title' => t('Block title'),
  336. '#maxlength' => 64,
  337. '#description' => $module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>&lt;none&gt;</em> to display no title, or leave blank to use the default block title.'),
  338. '#default_value' => $edit['title'],
  339. '#weight' => -18,
  340. );
  341. // Module-specific block configurations.
  342. if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
  343. foreach ($settings as $k => $v) {
  344. $form['block_settings'][$k] = $v;
  345. }
  346. }
  347. // Get the block subject for the page title.
  348. $info = module_invoke($module, 'block', 'list');
  349. drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
  350. // Standard block configurations.
  351. $form['user_vis_settings'] = array(
  352. '#type' => 'fieldset',
  353. '#title' => t('User specific visibility settings'),
  354. '#collapsible' => TRUE,
  355. );
  356. $form['user_vis_settings']['custom'] = array(
  357. '#type' => 'radios',
  358. '#title' => t('Custom visibility settings'),
  359. '#options' => array(
  360. t('Users cannot control whether or not they see this block.'),
  361. t('Show this block by default, but let individual users hide it.'),
  362. t('Hide this block by default but let individual users show it.')
  363. ),
  364. '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
  365. '#default_value' => $edit['custom'],
  366. );
  367. // Role-based visibility settings
  368. $default_role_options = array();
  369. $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
  370. while ($role = db_fetch_object($result)) {
  371. $default_role_options[] = $role->rid;
  372. }
  373. $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
  374. $role_options = array();
  375. while ($role = db_fetch_object($result)) {
  376. $role_options[$role->rid] = $role->name;
  377. }
  378. $form['role_vis_settings'] = array(
  379. '#type' => 'fieldset',
  380. '#title' => t('Role specific visibility settings'),
  381. '#collapsible' => TRUE,
  382. );
  383. $form['role_vis_settings']['roles'] = array(
  384. '#type' => 'checkboxes',
  385. '#title' => t('Show block for specific roles'),
  386. '#default_value' => $default_role_options,
  387. '#options' => $role_options,
  388. '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
  389. );
  390. $form['page_vis_settings'] = array(
  391. '#type' => 'fieldset',
  392. '#title' => t('Page specific visibility settings'),
  393. '#collapsible' => TRUE,
  394. );
  395. $access = user_access('use PHP for block visibility');
  396. if ($edit['visibility'] == 2 && !$access) {
  397. $form['page_vis_settings'] = array();
  398. $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
  399. $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
  400. }
  401. else {
  402. $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
  403. $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
  404. if ($access) {
  405. $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
  406. $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
  407. }
  408. $form['page_vis_settings']['visibility'] = array(
  409. '#type' => 'radios',
  410. '#title' => t('Show block on specific pages'),
  411. '#options' => $options,
  412. '#default_value' => $edit['visibility'],
  413. );
  414. $form['page_vis_settings']['pages'] = array(
  415. '#type' => 'textarea',
  416. '#title' => t('Pages'),
  417. '#default_value' => $edit['pages'],
  418. '#description' => $description,
  419. );
  420. }
  421. $form['submit'] = array(
  422. '#type' => 'submit',
  423. '#value' => t('Save block'),
  424. );
  425. return $form;
  426. }
  427. function block_admin_configure_validate($form_id, $form_values) {
  428. if ($form_values['module'] == 'block') {
  429. if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
  430. form_set_error('info', t('Please ensure that each block description is unique.'));
  431. }
  432. }
  433. }
  434. function block_admin_configure_submit($form_id, $form_values) {
  435. if (!form_get_errors()) {
  436. db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], trim($form_values['pages']), $form_values['custom'], $form_values['title'], $form_values['module'], $form_values['delta']);
  437. db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
  438. foreach (array_filter($form_values['roles']) as $rid) {
  439. db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $form_values['delta']);
  440. }
  441. module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
  442. drupal_set_message(t('The block configuration has been saved.'));
  443. cache_clear_all();
  444. return 'admin/build/block';
  445. }
  446. }
  447. function block_box_form_validate($form_id, $form_values) {
  448. if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
  449. form_set_error('info', t('Please ensure that each block description is unique.'));
  450. }
  451. }
  452. function block_box_form_submit($form_id, $form_values) {
  453. if (!form_get_errors()) {
  454. if (block_box_save($form_values)) {
  455. drupal_set_message(t('The block has been created.'));
  456. return 'admin/build/block';
  457. }
  458. }
  459. }
  460. /**
  461. * Menu callback; confirm deletion of custom blocks.
  462. */
  463. function block_box_delete($bid = 0) {
  464. $box = block_box_get($bid);
  465. $form['info'] = array('#type' => 'hidden', '#value' => $box['info']);
  466. $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
  467. return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $box['info'])), 'admin/build/block', '', t('Delete'), t('Cancel'));
  468. }
  469. /**
  470. * Deletion of custom blocks.
  471. */
  472. function block_box_delete_submit($form_id, $form_values) {
  473. db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
  474. db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = '%s'", $form_values['bid']);
  475. drupal_set_message(t('The block %name has been removed.', array('%name' => $form_values['info'])));
  476. cache_clear_all();
  477. return 'admin/build/block';
  478. };
  479. function block_box_form($edit = array()) {
  480. $form['info'] = array(
  481. '#type' => 'textfield',
  482. '#title' => t('Block description'),
  483. '#default_value' => $edit['info'],
  484. '#maxlength' => 64,
  485. '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))),
  486. '#required' => TRUE,
  487. '#weight' => -19,
  488. );
  489. $form['body_filter']['#weight'] = -17;
  490. $form['body_filter']['body'] = array(
  491. '#type' => 'textarea',
  492. '#title' => t('Block body'),
  493. '#default_value' => $edit['body'],
  494. '#rows' => 15,
  495. '#description' => t('The content of the block as shown to the user.'),
  496. '#weight' => -17,
  497. );
  498. if (!isset($edit['format'])) {
  499. $edit['format'] = FILTER_FORMAT_DEFAULT;
  500. }
  501. $form['body_filter']['format'] = filter_form($edit['format'], -16);
  502. $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
  503. return $form;
  504. }
  505. function block_box_save($edit, $delta = NULL) {
  506. if (!filter_access($edit['format'])) {
  507. $edit['format'] = FILTER_FORMAT_DEFAULT;
  508. }
  509. if (isset($delta)) {
  510. db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
  511. }
  512. else {
  513. db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $edit['body'], $edit['info'], $edit['format']);
  514. }
  515. return TRUE;
  516. }
  517. /**
  518. * Implementation of hook_user().
  519. *
  520. * Allow users to decide which custom blocks to display when they visit
  521. * the site.
  522. */
  523. function block_user($type, $edit, &$account, $category = NULL) {
  524. switch ($type) {
  525. case 'form':
  526. if ($category == 'account') {
  527. $rids = array_keys($account->roles);
  528. $placeholders = implode(',', array_fill(0, count($rids), '%d'));
  529. $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 ($placeholders) OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids);
  530. $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
  531. while ($block = db_fetch_object($result)) {
  532. $data = module_invoke($block->module, 'block', 'list');
  533. if ($data[$block->delta]['info']) {
  534. $return = TRUE;
  535. $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));
  536. }
  537. }
  538. if ($return) {
  539. return $form;
  540. }
  541. }
  542. break;
  543. case 'validate':
  544. if (!$edit['block']) {
  545. $edit['block'] = array();
  546. }
  547. return $edit;
  548. }
  549. }
  550. /**
  551. * Return all blocks in the specified region for the current user.
  552. *
  553. * @param $region
  554. * The name of a region.
  555. *
  556. * @return
  557. * An array of block objects, indexed with <i>module</i>_<i>delta</i>.
  558. * If you are displaying your blocks in one or two sidebars, you may check
  559. * whether this array is empty to see how many columns are going to be
  560. * displayed.
  561. *
  562. * @todo
  563. * Add a proper primary key (bid) to the blocks table so we don't have
  564. * to mess around with this <i>module</i>_<i>delta</i> construct.
  565. * Currently, the blocks table has no primary key defined!
  566. */
  567. function block_list($region) {
  568. global $user, $theme_key;
  569. static $blocks = array();
  570. if (!count($blocks)) {
  571. $rids = array_keys($user->roles);
  572. $placeholders = implode(',', array_fill(0, count($rids), '%d'));
  573. $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.theme = '%s' AND b.status = 1 AND (r.rid IN ($placeholders) OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", array_merge(array($theme_key), $rids));
  574. while ($block = db_fetch_object($result)) {
  575. if (!isset($blocks[$block->region])) {
  576. $blocks[$block->region] = array();
  577. }
  578. // Use the user's block visibility setting, if necessary
  579. if ($block->custom != 0) {
  580. if ($user->uid && isset($user->block[$block->module][$block->delta])) {
  581. $enabled = $user->block[$block->module][$block->delta];
  582. }
  583. else {
  584. $enabled = ($block->custom == 1);
  585. }
  586. }
  587. else {
  588. $enabled = TRUE;
  589. }
  590. // Match path if necessary
  591. if ($block->pages) {
  592. if ($block->visibility < 2) {
  593. $path = drupal_get_path_alias($_GET['q']);
  594. $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
  595. // Compare with the internal and path alias (if any).
  596. $page_match = preg_match($regexp, $path);
  597. if ($path != $_GET['q']) {
  598. $page_match = $page_match || preg_match($regexp, $_GET['q']);
  599. }
  600. // When $block->visibility has a value of 0, the block is displayed on
  601. // all pages except those listed in $block->pages. When set to 1, it
  602. // is displayed only on those pages listed in $block->pages.
  603. $page_match = !($block->visibility xor $page_match);
  604. }
  605. else {
  606. $page_match = drupal_eval($block->pages);
  607. }
  608. }
  609. else {
  610. $page_match = TRUE;
  611. }
  612. $block->enabled = $enabled;
  613. $block->page_match = $page_match;
  614. $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
  615. }
  616. }
  617. // Create an empty array if there were no entries
  618. if (!isset($blocks[$region])) {
  619. $blocks[$region] = array();
  620. }
  621. foreach ($blocks[$region] as $key => $block) {
  622. // Render the block content if it has not been created already.
  623. if (!isset($block->content)) {
  624. // Erase the block from the static array - we'll put it back if it has content.
  625. unset($blocks[$region][$key]);
  626. if ($block->enabled && $block->page_match) {
  627. // Check the current throttle status and see if block should be displayed
  628. // based on server load.
  629. if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
  630. $array = module_invoke($block->module, 'block', 'view', $block->delta);
  631. if (isset($array) && is_array($array)) {
  632. foreach ($array as $k => $v) {
  633. $block->$k = $v;
  634. }
  635. }
  636. }
  637. if (isset($block->content) && $block->content) {
  638. // Override default block title if a custom display title is present.
  639. if ($block->title) {
  640. // Check plain here to allow module generated titles to keep any markup.
  641. $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
  642. }
  643. $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
  644. }
  645. }
  646. }
  647. }
  648. return $blocks[$region];
  649. }

Functions

Namesort descending Description
block_admin_configure Menu callback; displays the block configuration form.
block_admin_configure_submit
block_admin_configure_validate
block_admin_display Generate main block administration form.
block_admin_display_submit Process main block administration form submission.
block_block Implementation of hook_block().
block_box_delete Menu callback; confirm deletion of custom blocks.
block_box_delete_submit Deletion of custom blocks.
block_box_form
block_box_form_submit
block_box_form_validate
block_box_get
block_box_save
block_help Implementation of hook_help().
block_list Return all blocks in the specified region for the current user.
block_menu Implementation of hook_menu().
block_perm Implementation of hook_perm().
block_user Implementation of hook_user().
theme_block_admin_display Theme main block administration form submission.
_block_compare Helper function for sorting blocks on admin/build/block.
_block_rehash Update the 'blocks' DB table with the blocks currently exported by modules.

Constants

Namesort descending Description
BLOCK_REGION_NONE Denotes that a block is not enabled in any region and should not be shown.

Comments

akberhussain’s picture

how can i remove the tag from a block drupal 6