1. 6.x modules/block/block.admin.inc
  2. 7.x modules/block/block.admin.inc

Admin page callbacks for the block module.

File

modules/block/block.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the block module.
  5. */
  6. /**
  7. * Menu callback for admin/build/block.
  8. */
  9. function block_admin_display($theme = NULL) {
  10. global $custom_theme;
  11. // If non-default theme configuration has been selected, set the custom theme.
  12. $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
  13. // Fetch and sort blocks
  14. $blocks = _block_rehash();
  15. usort($blocks, '_block_compare');
  16. return drupal_get_form('block_admin_display_form', $blocks, $theme);
  17. }
  18. /**
  19. * Generate main blocks administration form.
  20. */
  21. function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
  22. global $theme_key, $custom_theme;
  23. // Add CSS
  24. drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
  25. // If non-default theme configuration has been selected, set the custom theme.
  26. $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
  27. init_theme();
  28. $throttle = module_exists('throttle');
  29. $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<'. t('none') .'>');
  30. // Weights range from -delta to +delta, so delta should be at least half
  31. // of the amount of blocks present. This makes sure all blocks in the same
  32. // region get an unique weight.
  33. $weight_delta = round(count($blocks) / 2);
  34. // Build form tree
  35. $form = array(
  36. '#action' => arg(4) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block'),
  37. '#tree' => TRUE,
  38. );
  39. foreach ($blocks as $i => $block) {
  40. $key = $block['module'] .'_'. $block['delta'];
  41. $form[$key]['module'] = array(
  42. '#type' => 'value',
  43. '#value' => $block['module'],
  44. );
  45. $form[$key]['delta'] = array(
  46. '#type' => 'value',
  47. '#value' => $block['delta'],
  48. );
  49. $form[$key]['info'] = array(
  50. '#value' => check_plain($block['info'])
  51. );
  52. $form[$key]['theme'] = array(
  53. '#type' => 'hidden',
  54. '#value' => $theme_key
  55. );
  56. $form[$key]['weight'] = array(
  57. '#type' => 'weight',
  58. '#default_value' => $block['weight'],
  59. '#delta' => $weight_delta,
  60. );
  61. $form[$key]['region'] = array(
  62. '#type' => 'select',
  63. '#default_value' => $block['region'],
  64. '#options' => $block_regions,
  65. );
  66. if ($throttle) {
  67. $form[$key]['throttle'] = array('#type' => 'checkbox', '#default_value' => isset($block['throttle']) ? $block['throttle'] : FALSE);
  68. }
  69. $form[$key]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
  70. if ($block['module'] == 'block') {
  71. $form[$key]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
  72. }
  73. }
  74. $form['submit'] = array(
  75. '#type' => 'submit',
  76. '#value' => t('Save blocks'),
  77. );
  78. return $form;
  79. }
  80. /**
  81. * Process main blocks administration form submission.
  82. */
  83. function block_admin_display_form_submit($form, &$form_state) {
  84. foreach ($form_state['values'] as $block) {
  85. $block['status'] = $block['region'] != BLOCK_REGION_NONE;
  86. $block['region'] = $block['status'] ? $block['region'] : '';
  87. 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']);
  88. }
  89. drupal_set_message(t('The block settings have been updated.'));
  90. cache_clear_all();
  91. }
  92. /**
  93. * Helper function for sorting blocks on admin/build/block.
  94. *
  95. * Active blocks are sorted by region, then by weight.
  96. * Disabled blocks are sorted by name.
  97. */
  98. function _block_compare($a, $b) {
  99. global $theme_key;
  100. static $regions;
  101. // We need the region list to correctly order by region.
  102. if (!isset($regions)) {
  103. $regions = array_flip(array_keys(system_region_list($theme_key)));
  104. $regions[BLOCK_REGION_NONE] = count($regions);
  105. }
  106. // Separate enabled from disabled.
  107. $status = $b['status'] - $a['status'];
  108. if ($status) {
  109. return $status;
  110. }
  111. // Sort by region (in the order defined by theme .info file).
  112. if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
  113. return $place;
  114. }
  115. // Sort by weight.
  116. $weight = $a['weight'] - $b['weight'];
  117. if ($weight) {
  118. return $weight;
  119. }
  120. // Sort by title.
  121. return strcmp($a['info'], $b['info']);
  122. }
  123. /**
  124. * Menu callback; displays the block configuration form.
  125. */
  126. function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
  127. $form['module'] = array('#type' => 'value', '#value' => $module);
  128. $form['delta'] = array('#type' => 'value', '#value' => $delta);
  129. $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
  130. $form['block_settings'] = array(
  131. '#type' => 'fieldset',
  132. '#title' => t('Block specific settings'),
  133. '#collapsible' => TRUE,
  134. );
  135. $form['block_settings']['title'] = array(
  136. '#type' => 'textfield',
  137. '#title' => t('Block title'),
  138. '#maxlength' => 64,
  139. '#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.'),
  140. '#default_value' => $edit['title'],
  141. '#weight' => -18,
  142. );
  143. // Module-specific block configurations.
  144. if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
  145. foreach ($settings as $k => $v) {
  146. $form['block_settings'][$k] = $v;
  147. }
  148. }
  149. // Get the block subject for the page title.
  150. $info = module_invoke($module, 'block', 'list');
  151. if (isset($info[$delta])) {
  152. drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
  153. }
  154. // Standard block configurations.
  155. $form['user_vis_settings'] = array(
  156. '#type' => 'fieldset',
  157. '#title' => t('User specific visibility settings'),
  158. '#collapsible' => TRUE,
  159. );
  160. $form['user_vis_settings']['custom'] = array(
  161. '#type' => 'radios',
  162. '#title' => t('Custom visibility settings'),
  163. '#options' => array(
  164. t('Users cannot control whether or not they see this block.'),
  165. t('Show this block by default, but let individual users hide it.'),
  166. t('Hide this block by default but let individual users show it.')
  167. ),
  168. '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
  169. '#default_value' => $edit['custom'],
  170. );
  171. // Role-based visibility settings
  172. $default_role_options = array();
  173. $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
  174. while ($role = db_fetch_object($result)) {
  175. $default_role_options[] = $role->rid;
  176. }
  177. $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
  178. $role_options = array();
  179. while ($role = db_fetch_object($result)) {
  180. $role_options[$role->rid] = $role->name;
  181. }
  182. $form['role_vis_settings'] = array(
  183. '#type' => 'fieldset',
  184. '#title' => t('Role specific visibility settings'),
  185. '#collapsible' => TRUE,
  186. );
  187. $form['role_vis_settings']['roles'] = array(
  188. '#type' => 'checkboxes',
  189. '#title' => t('Show block for specific roles'),
  190. '#default_value' => $default_role_options,
  191. '#options' => $role_options,
  192. '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
  193. );
  194. $form['page_vis_settings'] = array(
  195. '#type' => 'fieldset',
  196. '#title' => t('Page specific visibility settings'),
  197. '#collapsible' => TRUE,
  198. );
  199. $access = user_access('use PHP for block visibility');
  200. if ($edit['visibility'] == 2 && !$access) {
  201. $form['page_vis_settings'] = array();
  202. $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
  203. $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
  204. }
  205. else {
  206. $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
  207. $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>'));
  208. if ($access) {
  209. $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
  210. $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 ?>'));
  211. }
  212. $form['page_vis_settings']['visibility'] = array(
  213. '#type' => 'radios',
  214. '#title' => t('Show block on specific pages'),
  215. '#options' => $options,
  216. '#default_value' => $edit['visibility'],
  217. );
  218. $form['page_vis_settings']['pages'] = array(
  219. '#type' => 'textarea',
  220. '#title' => t('Pages'),
  221. '#default_value' => $edit['pages'],
  222. '#description' => $description,
  223. );
  224. }
  225. $form['submit'] = array(
  226. '#type' => 'submit',
  227. '#value' => t('Save block'),
  228. );
  229. return $form;
  230. }
  231. function block_admin_configure_validate($form, &$form_state) {
  232. if ($form_state['values']['module'] == 'block') {
  233. if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE bid != %d AND info = '%s'", $form_state['values']['delta'], $form_state['values']['info']))) {
  234. form_set_error('info', t('Please ensure that each block description is unique.'));
  235. }
  236. }
  237. }
  238. function block_admin_configure_submit($form, &$form_state) {
  239. if (!form_get_errors()) {
  240. db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $form_state['values']['delta']);
  241. db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
  242. foreach (array_filter($form_state['values']['roles']) as $rid) {
  243. db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
  244. }
  245. module_invoke($form_state['values']['module'], 'block', 'save', $form_state['values']['delta'], $form_state['values']);
  246. drupal_set_message(t('The block configuration has been saved.'));
  247. cache_clear_all();
  248. $form_state['redirect'] = 'admin/build/block';
  249. return;
  250. }
  251. }
  252. /**
  253. * Menu callback: display the custom block addition form.
  254. */
  255. function block_add_block_form(&$form_state) {
  256. return block_admin_configure($form_state, 'block', NULL);
  257. }
  258. function block_add_block_form_validate($form, &$form_state) {
  259. if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info']))) {
  260. form_set_error('info', t('Please ensure that each block description is unique.'));
  261. }
  262. }
  263. /**
  264. * Save the new custom block.
  265. */
  266. function block_add_block_form_submit($form, &$form_state) {
  267. db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['format']);
  268. $delta = db_last_insert_id('boxes', 'bid');
  269. foreach (list_themes() as $key => $theme) {
  270. if ($theme->status) {
  271. db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, '%s', %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_NO_CACHE);
  272. }
  273. }
  274. foreach (array_filter($form_state['values']['roles']) as $rid) {
  275. db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
  276. }
  277. drupal_set_message(t('The block has been created.'));
  278. cache_clear_all();
  279. $form_state['redirect'] = 'admin/build/block';
  280. return;
  281. }
  282. /**
  283. * Menu callback; confirm deletion of custom blocks.
  284. */
  285. function block_box_delete(&$form_state, $bid = 0) {
  286. $box = block_box_get($bid);
  287. $form['info'] = array('#type' => 'hidden', '#value' => $box['info']);
  288. $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
  289. 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'));
  290. }
  291. /**
  292. * Deletion of custom blocks.
  293. */
  294. function block_box_delete_submit($form, &$form_state) {
  295. db_query('DELETE FROM {boxes} WHERE bid = %d', $form_state['values']['bid']);
  296. db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = '%s'", $form_state['values']['bid']);
  297. drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
  298. cache_clear_all();
  299. $form_state['redirect'] = 'admin/build/block';
  300. return;
  301. }
  302. /**
  303. * Process variables for block-admin-display.tpl.php.
  304. *
  305. * The $variables array contains the following arguments:
  306. * - $form
  307. *
  308. * @see block-admin-display.tpl.php
  309. * @see theme_block_admin_display()
  310. */
  311. function template_preprocess_block_admin_display_form(&$variables) {
  312. global $theme_key;
  313. $block_regions = system_region_list($theme_key);
  314. $variables['throttle'] = module_exists('throttle');
  315. $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
  316. foreach ($block_regions as $key => $value) {
  317. // Highlight regions on page to provide visual reference.
  318. drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
  319. // Initialize an empty array for the region.
  320. $variables['block_listing'][$key] = array();
  321. }
  322. // Initialize disabled blocks array.
  323. $variables['block_listing'][BLOCK_REGION_NONE] = array();
  324. // Set up to track previous region in loop.
  325. $last_region = '';
  326. foreach (element_children($variables['form']) as $i) {
  327. $block = &$variables['form'][$i];
  328. // Only take form elements that are blocks.
  329. if (isset($block['info'])) {
  330. // Fetch region for current block.
  331. $region = $block['region']['#default_value'];
  332. // Set special classes needed for table drag and drop.
  333. $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-'. $region;
  334. $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $region;
  335. $variables['block_listing'][$region][$i] = new stdClass();
  336. $variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
  337. $variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
  338. $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
  339. $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
  340. $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
  341. $variables['block_listing'][$region][$i]->throttle_check = $variables['throttle'] ? drupal_render($block['throttle']) : '';
  342. $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
  343. $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
  344. $variables['block_listing'][$region][$i]->printed = FALSE;
  345. $last_region = $region;
  346. }
  347. }
  348. $variables['form_submit'] = drupal_render($variables['form']);
  349. }

Functions

Namesort descending Description
block_add_block_form Menu callback: display the custom block addition form.
block_add_block_form_submit Save the new custom block.
block_add_block_form_validate
block_admin_configure Menu callback; displays the block configuration form.
block_admin_configure_submit
block_admin_configure_validate
block_admin_display Menu callback for admin/build/block.
block_admin_display_form Generate main blocks administration form.
block_admin_display_form_submit Process main blocks administration form submission.
block_box_delete Menu callback; confirm deletion of custom blocks.
block_box_delete_submit Deletion of custom blocks.
template_preprocess_block_admin_display_form Process variables for block-admin-display.tpl.php.
_block_compare Helper function for sorting blocks on admin/build/block.