admin_theme.module 8.6 KB

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