admin.admin.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * System settings form for admin toolbar.
  4. */
  5. function admin_settings_form($form, &$form_state) {
  6. $form['admin_toolbar'] = array(
  7. '#tree' => TRUE,
  8. '#theme' => 'admin_settings_form',
  9. );
  10. $form['admin_toolbar']['layout'] = array(
  11. '#type' => 'select',
  12. '#title' => t('Layout'),
  13. '#description' => t('Choose a layout for the admin toolbar. Vertical works well with large, wide screens. Horizontal works well on screens with limited real estate.'),
  14. '#options' => array(
  15. 'horizontal' => t('Horizontal'),
  16. 'vertical' => t('Vertical'),
  17. ),
  18. '#default_value' => admin_get_settings('layout'),
  19. );
  20. $form['admin_toolbar']['position'] = array(
  21. '#type' => 'select',
  22. '#title' => t('Position'),
  23. '#description' => t('Choose a position for the admin toolbar that will not collide with other elements in your current theme.'),
  24. '#options' => array(
  25. 'ne' => t('Top right'),
  26. 'nw' => t('Top left'),
  27. 'se' => t('Bottom right'),
  28. 'sw' => t('Bottom left'),
  29. ),
  30. '#default_value' => admin_get_settings('position'),
  31. );
  32. $form['admin_toolbar']['behavior'] = array(
  33. '#type' => 'select',
  34. '#title' => t('State on new pages'),
  35. '#description' => t('Choose toolbar behavior on new page loads.'),
  36. '#options' => array(
  37. 'df' => t('Remember from previous page'),
  38. 'ah' => t('Collapsed'),
  39. ),
  40. '#default_value' => admin_get_settings('behavior'),
  41. );
  42. $block_info = array();
  43. foreach (module_implements('block_info') as $module) {
  44. $module_blocks = module_invoke($module, 'block_info');
  45. if ($module_blocks) {
  46. foreach ($module_blocks as $delta => $info) {
  47. $block_info["{$module}-{$delta}"] = $info;
  48. }
  49. }
  50. }
  51. // Store block info for use in form processing.
  52. $form['#block_info'] = $block_info;
  53. // Get default block options.
  54. $options = array();
  55. $enabled = array_filter(admin_get_settings('blocks')) + admin_get_default_blocks(TRUE);
  56. foreach ($block_info as $bid => $info) {
  57. if (!empty($enabled[$bid])) {
  58. $options[$bid] = $info['info'];
  59. }
  60. }
  61. asort($options);
  62. $form['admin_toolbar']['blocks'] = array(
  63. '#type' => 'checkboxes',
  64. '#options' => $options,
  65. '#default_value' => drupal_map_assoc(array_keys(array_filter(admin_get_settings('blocks')))),
  66. );
  67. // Grab an array of human-readable module names. It hurts that we need to do this.
  68. $result = db_query("SELECT name,type,info FROM {system} WHERE type = :type AND status = :status", array(':type' => 'module', ':status' => 1));
  69. $modules = array();
  70. while ($row = $result->fetchObject()) {
  71. $info = unserialize($row->info);
  72. $modules[$row->name] = isset($info['name']) ? $info['name'] : $row->name;
  73. }
  74. $custom_options = array();
  75. foreach (array_diff_key($block_info, $options) as $bid => $info) {
  76. $block_parts = (explode('-', $bid));
  77. $module = array_shift($block_parts);
  78. $module = isset($modules[$module]) ? $modules[$module] : $module;
  79. $custom_options[$module][$bid] = $info['info'];
  80. }
  81. $custom_options = $custom_options + array(-1 => '<' . t('Choose a block') . '>');
  82. ksort($custom_options);
  83. $form['admin_toolbar']['custom'] = array(
  84. '#type' => 'select',
  85. '#options' => $custom_options,
  86. '#default_value' => -1,
  87. '#element_validate' => array('admin_settings_form_custom_validate'),
  88. '#description' => t('Enable other blocks for use in the admin toolbar.'),
  89. );
  90. $form['admin_toolbar']['add'] = array(
  91. '#type' => 'submit',
  92. '#value' => t('Add block'),
  93. );
  94. $form = system_settings_form($form);
  95. // Add a submit handler for expanding block information.
  96. array_unshift($form['#submit'], 'admin_settings_form_submit');
  97. return $form;
  98. }
  99. /**
  100. * Submit handler for admin_settings_form().
  101. *
  102. * Retrieves cache type for each block and saves that instead of `1` for each
  103. * enabled block.
  104. */
  105. function admin_settings_form_submit($form, &$form_state) {
  106. if (isset($form['#block_info'], $form_state['values']['admin_toolbar']['blocks'])) {
  107. $blocks = array();
  108. foreach (array_keys(array_filter($form_state['values']['admin_toolbar']['blocks'])) as $bid) {
  109. $blocks[$bid] = isset($form['#block_info'][$bid]['cache']) ? $form['#block_info'][$bid]['cache'] : DRUPAL_NO_CACHE;
  110. }
  111. $form_state['values']['admin_toolbar']['blocks'] = $blocks;
  112. }
  113. }
  114. /**
  115. * Element validator for custom block adder. Moves values within
  116. * $form_state['values'] and prevents additional variables from being saved.
  117. */
  118. function admin_settings_form_custom_validate($element, &$form_state) {
  119. if ($form_state['values']['admin_toolbar']['custom'] != -1) {
  120. $bid = $form_state['values']['admin_toolbar']['custom'];
  121. $form_state['values']['admin_toolbar']['blocks'][$bid] = $bid;
  122. }
  123. unset($form_state['values']['admin_toolbar']['custom']);
  124. unset($form_state['values']['admin_toolbar']['add']);
  125. }
  126. /**
  127. * Theme function for the admin settings form.
  128. */
  129. function theme_admin_settings_form($variables) {
  130. $form = $variables['element'];
  131. $rows = array();
  132. // Admin blocks
  133. $header = array(array(
  134. 'data' => t('Administrative blocks'),
  135. 'colspan' => 2,
  136. ));
  137. foreach (array_keys(admin_get_default_blocks(TRUE)) as $block) {
  138. $rows[] = array(array(
  139. 'data' => drupal_render($form['blocks'][$block]),
  140. 'colspan' => 2,
  141. ));
  142. }
  143. // "Custom" blocks
  144. $rows[] = array(array(
  145. 'data' => t('Other blocks'),
  146. 'colspan' => 2,
  147. 'header' => TRUE,
  148. ));
  149. foreach (element_children($form['blocks']) as $block) {
  150. if (empty($form['blocks'][$block]['#printed'])) {
  151. $rows[] = array(array(
  152. 'data' => drupal_render($form['blocks'][$block]),
  153. 'colspan' => 2,
  154. ));
  155. }
  156. }
  157. $rows[] = array(drupal_render($form['custom']), drupal_render($form['add']));
  158. $form['blocks']['#children'] = theme('table', array('header' => $header, 'rows' => $rows));
  159. return drupal_render_children($form);
  160. }
  161. /**
  162. * Rebuild form.
  163. */
  164. function admin_settings_rebuild($form_state) {
  165. $form = array();
  166. $form['rebuild'] = array(
  167. '#type' => 'fieldset',
  168. '#title' => t('Wipe and rebuild'),
  169. '#description' => t('If the administrative menu is not working properly, you may need to wipe and rebuild it from scratch. <strong>This option will reset any customizations made to your administrative menu items.</strong>'),
  170. );
  171. $form['rebuild']['submit'] = array(
  172. '#type' => 'submit',
  173. '#value' => t('Rebuild'),
  174. '#submit' => array('admin_settings_rebuild_submit'),
  175. );
  176. return $form;
  177. }
  178. /**
  179. * Wipe and rebuild the admin menu.
  180. */
  181. function admin_settings_rebuild_submit($form, &$form_state) {
  182. db_delete('menu_links')
  183. ->condition('menu_name', 'management')
  184. ->execute();
  185. menu_rebuild();
  186. if (module_exists('block')) {
  187. cache_clear_all('admin', 'cache_block', TRUE);
  188. }
  189. }