block.admin.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the block module.
  5. */
  6. /**
  7. * Menu callback for admin/structure/block/demo.
  8. */
  9. function block_admin_demo($theme = NULL) {
  10. drupal_add_css(drupal_get_path('module', 'block') . '/block.css');
  11. return '';
  12. }
  13. /**
  14. * Menu callback for admin/structure/block.
  15. *
  16. * @param $theme
  17. * The theme to display the administration page for. If not provided, defaults
  18. * to the currently used theme.
  19. */
  20. function block_admin_display($theme = NULL) {
  21. global $theme_key;
  22. drupal_theme_initialize();
  23. if (!isset($theme)) {
  24. // If theme is not specifically set, rehash for the current theme.
  25. $theme = $theme_key;
  26. }
  27. // Fetch and sort blocks.
  28. $blocks = block_admin_display_prepare_blocks($theme);
  29. return drupal_get_form('block_admin_display_form', $blocks, $theme);
  30. }
  31. /**
  32. * Prepares a list of blocks for display on the blocks administration page.
  33. *
  34. * @param $theme
  35. * The machine-readable name of the theme whose blocks should be returned.
  36. *
  37. * @return
  38. * An array of blocks, as returned by _block_rehash(), sorted by region in
  39. * preparation for display on the blocks administration page.
  40. *
  41. * @see block_admin_display_form()
  42. */
  43. function block_admin_display_prepare_blocks($theme) {
  44. $blocks = _block_rehash($theme);
  45. $compare_theme = &drupal_static('_block_compare:theme');
  46. $compare_theme = $theme;
  47. usort($blocks, '_block_compare');
  48. return $blocks;
  49. }
  50. /**
  51. * Form constructor for the main block administration form.
  52. *
  53. * @param $blocks
  54. * An array of blocks, as returned by block_admin_display_prepare_blocks().
  55. * @param $theme
  56. * A string representing the name of the theme to edit blocks for.
  57. * @param $block_regions
  58. * (optional) An array of regions in which the blocks will be allowed to be
  59. * placed. Defaults to all visible regions for the theme whose blocks are
  60. * being configured. In all cases, a dummy region for disabled blocks will
  61. * also be displayed.
  62. *
  63. * @return
  64. * An array representing the form definition.
  65. *
  66. * @ingroup forms
  67. * @see block_admin_display_form_submit()
  68. */
  69. function block_admin_display_form($form, &$form_state, $blocks, $theme, $block_regions = NULL) {
  70. $form['#attached']['css'] = array(drupal_get_path('module', 'block') . '/block.css');
  71. // Get a list of block regions if one was not provided.
  72. if (!isset($block_regions)) {
  73. $block_regions = system_region_list($theme, REGIONS_VISIBLE);
  74. }
  75. // Weights range from -delta to +delta, so delta should be at least half
  76. // of the amount of blocks present. This makes sure all blocks in the same
  77. // region get an unique weight.
  78. $weight_delta = round(count($blocks) / 2);
  79. // Build the form tree.
  80. $form['edited_theme'] = array(
  81. '#type' => 'value',
  82. '#value' => $theme,
  83. );
  84. $form['block_regions'] = array(
  85. '#type' => 'value',
  86. // Add a last region for disabled blocks.
  87. '#value' => $block_regions + array(BLOCK_REGION_NONE => BLOCK_REGION_NONE),
  88. );
  89. $form['blocks'] = array();
  90. $form['#tree'] = TRUE;
  91. foreach ($blocks as $i => $block) {
  92. $key = $block['module'] . '_' . $block['delta'];
  93. $form['blocks'][$key]['module'] = array(
  94. '#type' => 'value',
  95. '#value' => $block['module'],
  96. );
  97. $form['blocks'][$key]['delta'] = array(
  98. '#type' => 'value',
  99. '#value' => $block['delta'],
  100. );
  101. $form['blocks'][$key]['info'] = array(
  102. '#markup' => check_plain($block['info']),
  103. );
  104. $form['blocks'][$key]['theme'] = array(
  105. '#type' => 'hidden',
  106. '#value' => $theme,
  107. );
  108. $form['blocks'][$key]['weight'] = array(
  109. '#type' => 'weight',
  110. '#default_value' => $block['weight'],
  111. '#delta' => $weight_delta,
  112. '#title_display' => 'invisible',
  113. '#title' => t('Weight for @block block', array('@block' => $block['info'])),
  114. );
  115. $form['blocks'][$key]['region'] = array(
  116. '#type' => 'select',
  117. '#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL,
  118. '#empty_value' => BLOCK_REGION_NONE,
  119. '#title_display' => 'invisible',
  120. '#title' => t('Region for @block block', array('@block' => $block['info'])),
  121. '#options' => $block_regions,
  122. );
  123. $form['blocks'][$key]['configure'] = array(
  124. '#type' => 'link',
  125. '#title' => t('configure'),
  126. '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure',
  127. );
  128. if ($block['module'] == 'block') {
  129. $form['blocks'][$key]['delete'] = array(
  130. '#type' => 'link',
  131. '#title' => t('delete'),
  132. '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/delete',
  133. );
  134. }
  135. }
  136. // Do not allow disabling the main system content block when it is present.
  137. if (isset($form['blocks']['system_main']['region'])) {
  138. $form['blocks']['system_main']['region']['#required'] = TRUE;
  139. }
  140. $form['actions'] = array(
  141. '#tree' => FALSE,
  142. '#type' => 'actions',
  143. );
  144. $form['actions']['submit'] = array(
  145. '#type' => 'submit',
  146. '#value' => t('Save blocks'),
  147. );
  148. return $form;
  149. }
  150. /**
  151. * Form submission handler for block_admin_display_form().
  152. *
  153. * @see block_admin_display_form()
  154. */
  155. function block_admin_display_form_submit($form, &$form_state) {
  156. $transaction = db_transaction();
  157. try {
  158. foreach ($form_state['values']['blocks'] as $block) {
  159. $block['status'] = (int) ($block['region'] != BLOCK_REGION_NONE);
  160. $block['region'] = $block['status'] ? $block['region'] : '';
  161. db_update('block')
  162. ->fields(array(
  163. 'status' => $block['status'],
  164. 'weight' => $block['weight'],
  165. 'region' => $block['region'],
  166. ))
  167. ->condition('module', $block['module'])
  168. ->condition('delta', $block['delta'])
  169. ->condition('theme', $block['theme'])
  170. ->execute();
  171. }
  172. }
  173. catch (Exception $e) {
  174. $transaction->rollback();
  175. watchdog_exception('block', $e);
  176. throw $e;
  177. }
  178. drupal_set_message(t('The block settings have been updated.'));
  179. cache_clear_all();
  180. }
  181. /**
  182. * Sorts active blocks by region, then by weight; sorts inactive blocks by name.
  183. *
  184. * Callback for usort() in block_admin_display_prepare_blocks().
  185. */
  186. function _block_compare($a, $b) {
  187. global $theme_key;
  188. // Theme should be set before calling this function, or the current theme
  189. // is being used.
  190. $theme = &drupal_static(__FUNCTION__ . ':theme');
  191. if (!isset($theme)) {
  192. $theme = $theme_key;
  193. }
  194. $regions = &drupal_static(__FUNCTION__ . ':regions');
  195. // We need the region list to correctly order by region.
  196. if (!isset($regions)) {
  197. $regions = array_flip(array_keys(system_region_list($theme)));
  198. $regions[BLOCK_REGION_NONE] = count($regions);
  199. }
  200. // Separate enabled from disabled.
  201. $status = $b['status'] - $a['status'];
  202. if ($status) {
  203. return $status;
  204. }
  205. // Sort by region (in the order defined by theme .info file).
  206. if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
  207. return $place;
  208. }
  209. // Sort by weight, unless disabled.
  210. if ($a['region'] != BLOCK_REGION_NONE) {
  211. $weight = $a['weight'] - $b['weight'];
  212. if ($weight) {
  213. return $weight;
  214. }
  215. }
  216. // Sort by title.
  217. return strcmp($a['info'], $b['info']);
  218. }
  219. /**
  220. * Form constructor for the block configuration form.
  221. *
  222. * Also used by block_add_block_form() for adding a new custom block.
  223. *
  224. * @param $module
  225. * Name of the module that implements the block to be configured.
  226. * @param $delta
  227. * Unique ID of the block within the context of $module.
  228. *
  229. * @see block_admin_configure_validate()
  230. * @see block_admin_configure_submit()
  231. * @ingroup forms
  232. */
  233. function block_admin_configure($form, &$form_state, $module, $delta) {
  234. $block = block_load($module, $delta);
  235. $form['module'] = array(
  236. '#type' => 'value',
  237. '#value' => $block->module,
  238. );
  239. $form['delta'] = array(
  240. '#type' => 'value',
  241. '#value' => $block->delta,
  242. );
  243. // Get the block subject for the page title.
  244. $info = module_invoke($block->module, 'block_info');
  245. if (isset($info[$block->delta])) {
  246. drupal_set_title(t("'%name' block", array('%name' => $info[$block->delta]['info'])), PASS_THROUGH);
  247. }
  248. $form['settings']['title'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t('Block title'),
  251. '#maxlength' => 255,
  252. '#description' => $block->module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>!placeholder</em> to display no title, or leave blank to use the default block title.', array('!placeholder' => '&lt;none&gt;')),
  253. '#default_value' => isset($block->title) ? $block->title : '',
  254. '#weight' => -19,
  255. );
  256. // Module-specific block configuration.
  257. if ($settings = module_invoke($block->module, 'block_configure', $block->delta)) {
  258. foreach ($settings as $k => $v) {
  259. $form['settings'][$k] = $v;
  260. }
  261. }
  262. // Region settings.
  263. $form['regions'] = array(
  264. '#type' => 'fieldset',
  265. '#title' => t('Region settings'),
  266. '#collapsible' => FALSE,
  267. '#description' => t('Specify in which themes and regions this block is displayed.'),
  268. '#tree' => TRUE,
  269. );
  270. $theme_default = variable_get('theme_default', 'bartik');
  271. $admin_theme = variable_get('admin_theme');
  272. foreach (list_themes() as $key => $theme) {
  273. // Only display enabled themes
  274. if ($theme->status) {
  275. $region = db_query("SELECT region FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(
  276. ':module' => $block->module,
  277. ':delta' => $block->delta,
  278. ':theme' => $key,
  279. ))->fetchField();
  280. // Use a meaningful title for the main site theme and administrative
  281. // theme.
  282. $theme_title = $theme->info['name'];
  283. if ($key == $theme_default) {
  284. $theme_title = t('!theme (default theme)', array('!theme' => $theme_title));
  285. }
  286. elseif ($admin_theme && $key == $admin_theme) {
  287. $theme_title = t('!theme (administration theme)', array('!theme' => $theme_title));
  288. }
  289. $form['regions'][$key] = array(
  290. '#type' => 'select',
  291. '#title' => $theme_title,
  292. '#default_value' => !empty($region) && $region != -1 ? $region : NULL,
  293. '#empty_value' => BLOCK_REGION_NONE,
  294. '#options' => system_region_list($key, REGIONS_VISIBLE),
  295. '#weight' => ($key == $theme_default ? 9 : 10),
  296. );
  297. }
  298. }
  299. // Visibility settings.
  300. $form['visibility_title'] = array(
  301. '#type' => 'item',
  302. '#title' => t('Visibility settings'),
  303. );
  304. $form['visibility'] = array(
  305. '#type' => 'vertical_tabs',
  306. '#attached' => array(
  307. 'js' => array(drupal_get_path('module', 'block') . '/block.js'),
  308. ),
  309. );
  310. // Per-path visibility.
  311. $form['visibility']['path'] = array(
  312. '#type' => 'fieldset',
  313. '#title' => t('Pages'),
  314. '#collapsible' => TRUE,
  315. '#collapsed' => TRUE,
  316. '#group' => 'visibility',
  317. '#weight' => 0,
  318. );
  319. $access = user_access('use PHP for settings');
  320. if (isset($block->visibility) && $block->visibility == BLOCK_VISIBILITY_PHP && !$access) {
  321. $form['visibility']['path']['visibility'] = array(
  322. '#type' => 'value',
  323. '#value' => BLOCK_VISIBILITY_PHP,
  324. );
  325. $form['visibility']['path']['pages'] = array(
  326. '#type' => 'value',
  327. '#value' => isset($block->pages) ? $block->pages : '',
  328. );
  329. }
  330. else {
  331. $options = array(
  332. BLOCK_VISIBILITY_NOTLISTED => t('All pages except those listed'),
  333. BLOCK_VISIBILITY_LISTED => t('Only the listed pages'),
  334. );
  335. $description = t("Specify pages by using their paths. Enter one path per line. 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>'));
  336. if (module_exists('php') && $access) {
  337. $options += array(BLOCK_VISIBILITY_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
  338. $title = t('Pages or PHP code');
  339. $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
  340. }
  341. else {
  342. $title = t('Pages');
  343. }
  344. $form['visibility']['path']['visibility'] = array(
  345. '#type' => 'radios',
  346. '#title' => t('Show block on specific pages'),
  347. '#options' => $options,
  348. '#default_value' => isset($block->visibility) ? $block->visibility : BLOCK_VISIBILITY_NOTLISTED,
  349. );
  350. $form['visibility']['path']['pages'] = array(
  351. '#type' => 'textarea',
  352. '#title' => '<span class="element-invisible">' . $title . '</span>',
  353. '#default_value' => isset($block->pages) ? $block->pages : '',
  354. '#description' => $description,
  355. );
  356. }
  357. // Per-role visibility.
  358. $default_role_options = db_query("SELECT rid FROM {block_role} WHERE module = :module AND delta = :delta", array(
  359. ':module' => $block->module,
  360. ':delta' => $block->delta,
  361. ))->fetchCol();
  362. $role_options = array_map('check_plain', user_roles());
  363. $form['visibility']['role'] = array(
  364. '#type' => 'fieldset',
  365. '#title' => t('Roles'),
  366. '#collapsible' => TRUE,
  367. '#collapsed' => TRUE,
  368. '#group' => 'visibility',
  369. '#weight' => 10,
  370. );
  371. $form['visibility']['role']['roles'] = array(
  372. '#type' => 'checkboxes',
  373. '#title' => t('Show block for specific roles'),
  374. '#default_value' => $default_role_options,
  375. '#options' => $role_options,
  376. '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
  377. );
  378. // Per-user visibility.
  379. $form['visibility']['user'] = array(
  380. '#type' => 'fieldset',
  381. '#title' => t('Users'),
  382. '#collapsible' => TRUE,
  383. '#collapsed' => TRUE,
  384. '#group' => 'visibility',
  385. '#weight' => 20,
  386. );
  387. $form['visibility']['user']['custom'] = array(
  388. '#type' => 'radios',
  389. '#title' => t('Customizable per user'),
  390. '#options' => array(
  391. BLOCK_CUSTOM_FIXED => t('Not customizable'),
  392. BLOCK_CUSTOM_ENABLED => t('Customizable, visible by default'),
  393. BLOCK_CUSTOM_DISABLED => t('Customizable, hidden by default'),
  394. ),
  395. '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
  396. '#default_value' => isset($block->custom) ? $block->custom : BLOCK_CUSTOM_FIXED,
  397. );
  398. $form['actions'] = array('#type' => 'actions');
  399. $form['actions']['submit'] = array(
  400. '#type' => 'submit',
  401. '#value' => t('Save block'),
  402. );
  403. return $form;
  404. }
  405. /**
  406. * Form validation handler for block_admin_configure().
  407. *
  408. * @see block_admin_configure()
  409. * @see block_admin_configure_submit()
  410. */
  411. function block_admin_configure_validate($form, &$form_state) {
  412. if ($form_state['values']['module'] == 'block') {
  413. $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE bid <> :bid AND info = :info', 0, 1, array(
  414. ':bid' => $form_state['values']['delta'],
  415. ':info' => $form_state['values']['info'],
  416. ))->fetchField();
  417. if (empty($form_state['values']['info']) || $custom_block_exists) {
  418. form_set_error('info', t('Ensure that each block description is unique.'));
  419. }
  420. }
  421. }
  422. /**
  423. * Form submission handler for block_admin_configure().
  424. *
  425. * @see block_admin_configure()
  426. * @see block_admin_configure_validate()
  427. */
  428. function block_admin_configure_submit($form, &$form_state) {
  429. if (!form_get_errors()) {
  430. $transaction = db_transaction();
  431. try {
  432. db_update('block')
  433. ->fields(array(
  434. 'visibility' => (int) $form_state['values']['visibility'],
  435. 'pages' => trim($form_state['values']['pages']),
  436. 'custom' => (int) $form_state['values']['custom'],
  437. 'title' => $form_state['values']['title'],
  438. ))
  439. ->condition('module', $form_state['values']['module'])
  440. ->condition('delta', $form_state['values']['delta'])
  441. ->execute();
  442. db_delete('block_role')
  443. ->condition('module', $form_state['values']['module'])
  444. ->condition('delta', $form_state['values']['delta'])
  445. ->execute();
  446. $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
  447. foreach (array_filter($form_state['values']['roles']) as $rid) {
  448. $query->values(array(
  449. 'rid' => $rid,
  450. 'module' => $form_state['values']['module'],
  451. 'delta' => $form_state['values']['delta'],
  452. ));
  453. }
  454. $query->execute();
  455. // Store regions per theme for this block
  456. foreach ($form_state['values']['regions'] as $theme => $region) {
  457. db_merge('block')
  458. ->key(array('theme' => $theme, 'delta' => $form_state['values']['delta'], 'module' => $form_state['values']['module']))
  459. ->fields(array(
  460. 'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
  461. 'pages' => trim($form_state['values']['pages']),
  462. 'status' => (int) ($region != BLOCK_REGION_NONE),
  463. ))
  464. ->execute();
  465. }
  466. module_invoke($form_state['values']['module'], 'block_save', $form_state['values']['delta'], $form_state['values']);
  467. }
  468. catch (Exception $e) {
  469. $transaction->rollback();
  470. watchdog_exception('block', $e);
  471. throw $e;
  472. }
  473. drupal_set_message(t('The block configuration has been saved.'));
  474. cache_clear_all();
  475. $form_state['redirect'] = 'admin/structure/block';
  476. }
  477. }
  478. /**
  479. * Form constructor for the add block form.
  480. *
  481. * @see block_add_block_form_validate()
  482. * @see block_add_block_form_submit()
  483. * @ingroup forms
  484. */
  485. function block_add_block_form($form, &$form_state) {
  486. return block_admin_configure($form, $form_state, 'block', NULL);
  487. }
  488. /**
  489. * Form validation handler for block_add_block_form().
  490. *
  491. * @see block_add_block_form()
  492. * @see block_add_block_form_submit()
  493. */
  494. function block_add_block_form_validate($form, &$form_state) {
  495. $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE info = :info', 0, 1, array(':info' => $form_state['values']['info']))->fetchField();
  496. if (empty($form_state['values']['info']) || $custom_block_exists) {
  497. form_set_error('info', t('Ensure that each block description is unique.'));
  498. }
  499. }
  500. /**
  501. * Form submission handler for block_add_block_form().
  502. *
  503. * Saves the new custom block.
  504. *
  505. * @see block_add_block_form()
  506. * @see block_add_block_form_validate()
  507. */
  508. function block_add_block_form_submit($form, &$form_state) {
  509. $delta = db_insert('block_custom')
  510. ->fields(array(
  511. 'body' => $form_state['values']['body']['value'],
  512. 'info' => $form_state['values']['info'],
  513. 'format' => $form_state['values']['body']['format'],
  514. ))
  515. ->execute();
  516. // Store block delta to allow other modules to work with new block.
  517. $form_state['values']['delta'] = $delta;
  518. $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
  519. foreach (list_themes() as $key => $theme) {
  520. if ($theme->status) {
  521. $query->values(array(
  522. 'visibility' => (int) $form_state['values']['visibility'],
  523. 'pages' => trim($form_state['values']['pages']),
  524. 'custom' => (int) $form_state['values']['custom'],
  525. 'title' => $form_state['values']['title'],
  526. 'module' => $form_state['values']['module'],
  527. 'theme' => $theme->name,
  528. 'status' => 0,
  529. 'weight' => 0,
  530. 'delta' => $delta,
  531. 'cache' => DRUPAL_NO_CACHE,
  532. ));
  533. }
  534. }
  535. $query->execute();
  536. $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
  537. foreach (array_filter($form_state['values']['roles']) as $rid) {
  538. $query->values(array(
  539. 'rid' => $rid,
  540. 'module' => $form_state['values']['module'],
  541. 'delta' => $delta,
  542. ));
  543. }
  544. $query->execute();
  545. // Store regions per theme for this block
  546. foreach ($form_state['values']['regions'] as $theme => $region) {
  547. db_merge('block')
  548. ->key(array('theme' => $theme, 'delta' => $delta, 'module' => $form_state['values']['module']))
  549. ->fields(array(
  550. 'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
  551. 'pages' => trim($form_state['values']['pages']),
  552. 'status' => (int) ($region != BLOCK_REGION_NONE),
  553. ))
  554. ->execute();
  555. }
  556. drupal_set_message(t('The block has been created.'));
  557. cache_clear_all();
  558. $form_state['redirect'] = 'admin/structure/block';
  559. }
  560. /**
  561. * Form constructor for the custom block deletion form.
  562. *
  563. * @param $module
  564. * The name of the module that implements the block to be deleted. This should
  565. * always equal 'block' since it only allows custom blocks to be deleted.
  566. * @param $delta
  567. * The unique ID of the block within the context of $module.
  568. *
  569. * @see block_custom_block_delete_submit()
  570. */
  571. function block_custom_block_delete($form, &$form_state, $module, $delta) {
  572. $block = block_load($module, $delta);
  573. $custom_block = block_custom_block_get($block->delta);
  574. $form['info'] = array('#type' => 'hidden', '#value' => $custom_block['info'] ? $custom_block['info'] : $custom_block['title']);
  575. $form['bid'] = array('#type' => 'hidden', '#value' => $block->delta);
  576. return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $custom_block['info'])), 'admin/structure/block', '', t('Delete'), t('Cancel'));
  577. }
  578. /**
  579. * Form submission handler for block_custom_block_delete().
  580. *
  581. * @see block_custom_block_delete()
  582. */
  583. function block_custom_block_delete_submit($form, &$form_state) {
  584. db_delete('block_custom')
  585. ->condition('bid', $form_state['values']['bid'])
  586. ->execute();
  587. db_delete('block')
  588. ->condition('module', 'block')
  589. ->condition('delta', $form_state['values']['bid'])
  590. ->execute();
  591. db_delete('block_role')
  592. ->condition('module', 'block')
  593. ->condition('delta', $form_state['values']['bid'])
  594. ->execute();
  595. drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
  596. cache_clear_all();
  597. $form_state['redirect'] = 'admin/structure/block';
  598. return;
  599. }
  600. /**
  601. * Processes variables for block-admin-display-form.tpl.php.
  602. *
  603. * The $variables array contains the following arguments:
  604. * - $form
  605. *
  606. * @see block-admin-display.tpl.php
  607. * @see theme_block_admin_display()
  608. */
  609. function template_preprocess_block_admin_display_form(&$variables) {
  610. $variables['block_regions'] = $variables['form']['block_regions']['#value'];
  611. if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
  612. $variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled');
  613. }
  614. foreach ($variables['block_regions'] as $key => $value) {
  615. // Initialize an empty array for the region.
  616. $variables['block_listing'][$key] = array();
  617. }
  618. // Initialize disabled blocks array.
  619. $variables['block_listing'][BLOCK_REGION_NONE] = array();
  620. // Add each block in the form to the appropriate place in the block listing.
  621. foreach (element_children($variables['form']['blocks']) as $i) {
  622. $block = &$variables['form']['blocks'][$i];
  623. // Fetch the region for the current block.
  624. $region = (isset($block['region']['#default_value']) ? $block['region']['#default_value'] : BLOCK_REGION_NONE);
  625. // Set special classes needed for table drag and drop.
  626. $block['region']['#attributes']['class'] = array('block-region-select', 'block-region-' . $region);
  627. $block['weight']['#attributes']['class'] = array('block-weight', 'block-weight-' . $region);
  628. $variables['block_listing'][$region][$i] = new stdClass();
  629. $variables['block_listing'][$region][$i]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
  630. $variables['block_listing'][$region][$i]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
  631. $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
  632. $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
  633. $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
  634. $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
  635. $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
  636. $variables['block_listing'][$region][$i]->printed = FALSE;
  637. }
  638. $variables['form_submit'] = drupal_render_children($variables['form']);
  639. }