Renaming extensions cache key.
[project/omega.git] / theme-settings.php
1 <?php
2
3 /**
4 * @file
5 * Theme settings file for the Omega base theme.
6 */
7
8 require_once dirname(__FILE__) . '/template.php';
9
10 /**
11 * Implements hook_form_FORM_alter().
12 */
13 function omega_form_system_theme_settings_alter(&$form, $form_state, $form_id = NULL) {
14 // General "alters" use a form id. Settings should not be set here. The only
15 // thing useful about this is if you need to alter the form for the running
16 // theme and *not* the the me setting. @see http://drupal.org/node/943212
17 if (isset($form_id)) {
18 return;
19 }
20
21 // The layout extension requires Omega Tools to be installed in order to
22 // function properly.
23 foreach (omega_extensions() as $extension => $info) {
24
25 }
26
27 // Include the template.php and theme-settings.php files for all the themes in
28 // the theme trail.
29 foreach (omega_theme_trail() as $theme => $name) {
30 $path = drupal_get_path('theme', $theme);
31
32 $filename = DRUPAL_ROOT . '/' . $path . '/template.php';
33 if (file_exists($filename)) {
34 require_once $filename;
35 }
36
37 $filename = DRUPAL_ROOT . '/' . $path . '/theme-settings.php';
38 if (file_exists($filename)) {
39 require_once $filename;
40 }
41 }
42
43 // Get the admin theme so we can set a class for styling this form.
44 $admin = drupal_html_class(variable_get('admin_theme', $GLOBALS['theme']));
45 $form['#prefix'] = '<div class="admin-theme-' . $admin . '">';
46 $form['#suffix'] = '</div>';
47
48 // Add some custom styling and functionality to our theme settings form.
49 $form['#attached']['css'][] = drupal_get_path('theme', 'omega') . '/css/omega.admin.css';
50 $form['#attached']['js'][] = drupal_get_path('theme', 'omega') . '/js/omega.admin.js';
51
52 // Collapse all the core theme settings tabs in order to have the form actions
53 // visible all the time without having to scroll.
54 foreach (element_children($form) as $key) {
55 if ($form[$key]['#type'] == 'fieldset') {
56 $form[$key]['#collapsible'] = TRUE;
57 $form[$key]['#collapsed'] = TRUE;
58 }
59 }
60
61 if ($extensions = omega_extensions()) {
62 $form['omega'] = array(
63 '#type' => 'vertical_tabs',
64 '#weight' => -10,
65 );
66
67 // Load the theme settings for all enabled extensions.
68 foreach ($extensions as $extension => $info) {
69 $form['omega'][$extension] = array(
70 '#type' => 'fieldset',
71 '#title' => $info['info']['name'],
72 '#attributes' => array(
73 'class' => array('omega-extension'),
74 ),
75 );
76
77 if (!empty($info['info']['dependencies'])) {
78 $errors = array();
79 foreach ($info['info']['dependencies'] as $dependency) {
80 $dependency = drupal_parse_dependency($dependency);
81
82 // Check if the module exists.
83 if (!$module = system_get_info('module', $dependency['name'])) {
84 $errors[] = t('This extensions requires the @module module.', array(
85 '@module' => drupal_ucfirst($dependency['name']),
86 ));
87 }
88 // Check if the module version is compatible.
89 elseif (($version = drupal_check_incompatibility($dependency, str_replace(DRUPAL_CORE_COMPATIBILITY . '-', '', $module['version']))) !== NULL) {
90 $errors[] = t('This extension requires @module @version. The currently installed version is @installed.', array(
91 '@module' => $module['name'],
92 '@version' => $version,
93 '@installed' => !empty($module['version']) ? $module['version'] : t('undetermined'),
94 ));
95 }
96 }
97
98 if (!empty($errors)) {
99 $form['omega'][$extension]['errors'] = array(
100 '#type' => 'item',
101 '#title' => t('Missing requirements'),
102 '#markup' => '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>',
103 '#weight' => -20,
104 // Abuse the #name attribute to add a class to the form item.
105 '#name' => 'omega-requirements',
106 );
107 }
108
109 // Disable all options if there were any errors.
110 $form['omega'][$extension]['#disabled'] = !empty($errors);
111 }
112
113 $form['omega'][$extension]['omega_toggle_extension_' . $extension] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Enable this extension'),
116 '#description' => $info['info']['description'],
117 '#default_value' => omega_extension_enabled($extension),
118 '#weight' => -10,
119 );
120
121 $element = array();
122
123 // Load the implementation for this extensions and invoke the according
124 // hook.
125 $file = $info['path'] . '/' . $extension . '.settings.inc';
126 if (is_file($file)) {
127 require_once $file;
128 }
129
130 $function = $info['theme'] . '_extension_' . $extension . '_settings_form';
131 if (function_exists($function)) {
132 // By default, each extension resides in a vertical tab
133 $element = $function($element, $form, $form_state) + array(
134 '#type' => 'fieldset',
135 '#title' => t('Settings'),
136 '#states' => array(
137 'disabled' => array(
138 'input[name="omega_toggle_extension_' . $extension . '"]' => array('checked' => FALSE),
139 ),
140 ),
141 );
142 }
143
144 drupal_alter('extension_' . $extension . '_settings_form', $element, $form, $form_state);
145
146 if (element_children($element)) {
147 // Append the extension form to the theme settings form if it has any
148 // children.
149 $form['omega'][$extension]['settings'] = $element;
150 }
151 }
152 }
153
154 // Custom option for toggling the main content blog on the front page.
155 $form['theme_settings']['omega_toggle_front_page_content'] = array(
156 '#type' => 'checkbox',
157 '#title' => t('Front page content'),
158 '#description' => t('Allow the main content block to be rendered on the front page.'),
159 '#default_value' => omega_theme_get_setting('omega_toggle_front_page_content', TRUE),
160 );
161
162 // We need a custom form submit handler for processing some of the values.
163 $form['#submit'][] = 'omega_theme_settings_form_submit';
164 }
165
166 /**
167 * Form submit handler for the theme settings form.
168 */
169 function omega_theme_settings_form_submit($form, &$form_state) {
170 // Clear the theme settings cache.
171 $theme = $form_state['build_info']['args'][0];
172 cache_clear_all('theme_settings:' . $theme, 'cache');
173 cache_clear_all('omega_extensions:' . $form_state['build_info']['args'][0], 'cache');
174
175 // Rebuild the theme registry. This has quite a performance impact but since
176 // this only happens once after we (re-)saved the theme settings this is fine.
177 // Also, this is actually required because we are caching certain things in
178 // the theme registry.
179 drupal_theme_rebuild();
180
181 // This is a relict from the vertical tabs and should be removed so it doesn't
182 // end up in the theme settings array.
183 unset($form_state['values']['omega__active_tab']);
184 }