admin_theme.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @file
  4. * Enable the administration theme on more pages,
  5. * then possible with Drupal's default administration page.
  6. */
  7. /**
  8. * Implements hook_permission().
  9. */
  10. function admin_theme_permission() {
  11. return array(
  12. 'access admin theme' => array(
  13. 'title' => t('Access administration theme'),
  14. 'description' => t('View pages using the administration theme'),
  15. )
  16. );
  17. }
  18. /**
  19. * Get the variable name for a certain option.
  20. *
  21. * @param string $module
  22. * Module that defines this option.
  23. * @param string $params
  24. * Name of the option.
  25. *
  26. * @return string
  27. * Variable name for the option.
  28. */
  29. function admin_theme_variable_name($module, $option) {
  30. return 'admin_theme_' . $module . '_' . $option;
  31. }
  32. /**
  33. * Get all module defined options.
  34. *
  35. * @return array
  36. * All options.
  37. */
  38. function admin_theme_list() {
  39. $options = array();
  40. foreach (module_list() as $module) {
  41. $module_options = module_invoke($module, 'admin_theme_info');
  42. if ($module_options && count($module_options) > 0) {
  43. foreach ($module_options as $option => $info) {
  44. $info['option'] = $option;
  45. $info['module'] = $module;
  46. $options[] = $info;
  47. }
  48. }
  49. }
  50. return $options;
  51. }
  52. /**
  53. * Implements hook_form_alter().
  54. */
  55. function admin_theme_form_system_themes_admin_form_alter(&$form, $form_state) {
  56. // Define a fieldset for the page selection.
  57. $form['admin_theme']['pages'] = array(
  58. '#type' => 'fieldset',
  59. '#title' => t('Pages'),
  60. '#collapsible' => TRUE,
  61. '#description' => t('Choose which pages should be displayed with the administration theme.'),
  62. );
  63. // Add the content editing option to the pages fieldset and change the title.
  64. $form['admin_theme']['pages']['node_admin_theme'] = $form['admin_theme']['node_admin_theme'];
  65. $form['admin_theme']['pages']['node_admin_theme']['#title'] = t('Content editing');
  66. unset($form['admin_theme']['node_admin_theme']);
  67. // Add all options as checkboxes to the admin theme settings form.
  68. $list = admin_theme_list();
  69. foreach ($list as $info) {
  70. $var = admin_theme_variable_name($info['module'], $info['option']);
  71. $form['admin_theme']['pages'][$var] = array(
  72. '#type' => 'checkbox',
  73. '#title' => array_key_exists('title', $info) ? $info['title'] : NULL,
  74. '#description' => array_key_exists('description', $info) ? $info['description'] : NULL,
  75. '#default_value' => variable_get($var, '0'),
  76. );
  77. }
  78. // Allow the user to define a set of pages where the admin theme should,
  79. // or should not be applied to.
  80. $form['admin_theme']['pages']['custom'] = array(
  81. '#type' => 'fieldset',
  82. '#title' => t('Custom'),
  83. '#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>')),
  84. '#collapsible' => TRUE,
  85. '#collapsed' => TRUE,
  86. '#weight' => 9,
  87. );
  88. $form['admin_theme']['pages']['custom']['admin_theme_path'] = array(
  89. '#type' => 'textarea',
  90. '#title' => t('Use administration theme on the following pages'),
  91. '#default_value' => variable_get('admin_theme_path', ''),
  92. );
  93. $form['admin_theme']['pages']['custom']['admin_theme_path_disallow'] = array(
  94. '#type' => 'textarea',
  95. '#title' => t('Do not use administration theme on the following pages'),
  96. '#description' => t('If a path appears here, the administration theme is not shown even if all above options apply.'),
  97. '#default_value' => variable_get('admin_theme_path_disallow', ''),
  98. );
  99. $form['#submit'][] = 'admin_theme_form_system_themes_form_alter_submit';
  100. $form['admin_theme']['actions']['#weight'] = 10;
  101. }
  102. /**
  103. * Process system_themes_form additions submissions.
  104. */
  105. function admin_theme_form_system_themes_form_alter_submit($form, &$form_state) {
  106. // Module options.
  107. $list = admin_theme_list();
  108. foreach ($list as $info) {
  109. $var = admin_theme_variable_name($info['module'], $info['option']);
  110. if (isset($form_state['values'][$var])) {
  111. variable_set($var, $form_state['values'][$var]);
  112. }
  113. }
  114. // Custom page options.
  115. variable_set('admin_theme_path', $form_state['values']['admin_theme_path']);
  116. variable_set('admin_theme_path_disallow', $form_state['values']['admin_theme_path_disallow']);
  117. }
  118. /**
  119. * Implements hook_custom_theme().
  120. */
  121. function admin_theme_custom_theme() {
  122. $admin_theme_disallow = FALSE;
  123. $admin_theme = FALSE;
  124. // Check if some paths are disallow to get the theme.
  125. if (trim(variable_get('admin_theme_path_disallow', '')) != '') {
  126. // Pages that are defined by their normal path.
  127. $admin_theme_disallow = drupal_match_path($_GET['q'], variable_get('admin_theme_path_disallow', ''));
  128. // Pages that are defined with their alias.
  129. $alias = drupal_get_path_alias($_GET['q']);
  130. if ($alias != $_GET['q']) {
  131. $admin_theme_disallow = $admin_theme || drupal_match_path($alias, variable_get('admin_theme_path_disallow', ''));
  132. }
  133. }
  134. // We should not show the admin theme if the user has no access,
  135. // or the path is in the disallow list.
  136. if (!user_access('access admin theme') || $admin_theme_disallow) {
  137. return;
  138. }
  139. // Check if an option is enabled and if it results to TRUE.
  140. $list = admin_theme_list();
  141. foreach ($list as $info) {
  142. $var = admin_theme_variable_name($info['module'], $info['option']);
  143. if ((bool)variable_get($var, '0') && module_invoke($info['module'], 'admin_theme', 'check', $info['option'])) {
  144. $admin_theme = TRUE;
  145. }
  146. }
  147. // Some custom defined pages should get admin theme.
  148. if (trim(variable_get('admin_theme_path', '')) != '') {
  149. // Pages that are defined by their normal path.
  150. $admin_theme = $admin_theme || drupal_match_path($_GET['q'], variable_get('admin_theme_path', ''));
  151. // Pages that are defined with their alias.
  152. $alias = drupal_get_path_alias($_GET['q']);
  153. if ($alias != $_GET['q']) {
  154. $admin_theme = $admin_theme || drupal_match_path($alias, variable_get('admin_theme_path', ''));
  155. }
  156. }
  157. // Use the admin theme for the current request (if global admin theme setting is checked).
  158. if ($admin_theme) {
  159. return variable_get('admin_theme');
  160. }
  161. return;
  162. }
  163. /**
  164. * Implements hook_admin_theme_info().
  165. */
  166. function admin_theme_admin_theme_info() {
  167. $options = array();
  168. $options['batch'] = array(
  169. 'title' => t('Batch processing'),
  170. 'description' => t('Use the administration theme when executing batch operations.'),
  171. );
  172. if (module_exists('img_assist')) {
  173. $options['img_assist'] = array(
  174. 'title' => t('Image assist'),
  175. 'description' => t('Use the administration theme when viewing the Image assist popup window.'),
  176. );
  177. }
  178. if (module_exists('coder')) {
  179. $options['coder'] = array(
  180. 'title' => t('Code reviews'),
  181. 'description' => t('Use the administration theme when viewing Coder code reviews.'),
  182. );
  183. }
  184. if (module_exists('devel')) {
  185. $options['devel'] = array(
  186. 'title' => t('Devel pages.'),
  187. 'description' => t('Use the administration theme when viewing pages of the devel module (hook_elements(), Dev render, Dev load, Session viewer, Theme registery, Variable editor, ...).'),
  188. );
  189. }
  190. if (module_exists('service_attachments')) {
  191. $options['service_attachments'] = array(
  192. 'title' => t('Service attachments form on nodes.'),
  193. 'description' => t('Use the administration theme when viewing service attachments on nodes.'),
  194. );
  195. }
  196. if (module_exists('webform')) {
  197. $options['webform_results'] = array(
  198. 'title' => t('Webform submissions.'),
  199. 'description' => t('Use the administration theme when viewing webform submissions.'),
  200. );
  201. }
  202. if (module_exists('statistics')) {
  203. $options['statistics'] = array(
  204. 'title' => t('Pages defined by the statistics module.'),
  205. 'description' => t('Use the administration theme when viewing pages of the statistics module.'),
  206. );
  207. }
  208. return $options;
  209. }
  210. /**
  211. * Implements hook_admin_theme_check().
  212. */
  213. function admin_theme_admin_theme_check($option = NULL) {
  214. switch ($option) {
  215. case 'img_assist':
  216. return arg(0) == 'img_assist';
  217. case 'coder':
  218. return arg(0) == 'coder';
  219. case 'devel':
  220. return arg(0) == 'devel' || (arg(0) == 'node' && arg(2) == 'devel');
  221. case 'batch':
  222. return arg(0) == 'batch';
  223. case 'service_attachments':
  224. return arg(0) == 'node' && arg(2) == 'service_attachments';
  225. case 'webform_results':
  226. return arg(0) == 'node' && arg(2) == 'webform-results';
  227. case 'statistics':
  228. return (arg(0) == 'node' || arg(0) == 'user') && arg(2) == 'track';
  229. }
  230. }