-
Notifications
You must be signed in to change notification settings - Fork 0
/
drab.module
203 lines (164 loc) · 6.06 KB
/
drab.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @file
* Makes the DRAB module do it's stuff
*/
/**
* Returnsa bundle aggregrated files bases on a path
*
* @global string $base_url
* @global string $base_theme_info
* @global string $theme_info
* @param string $current_path
* @return string
*/
function drab_get_files($current_path) {
$load = &drupal_static(__FUNCTION__);
if (!isset($load)) {
// This is the first time this is called.
global $base_url, $base_theme_info, $theme_info;
// Make a list of base themes and the current theme.
$themes = $base_theme_info;
$themes[] = $theme_info;
// generate file info
$generate_files = array();
foreach (array_keys($themes) as $key) {
$theme_path = dirname($themes[$key]->filename) . '/';
if (isset($themes[$key]->info['bundle'])) {
foreach ($themes[$key]->info['bundle'] as $bundle_name => $bundle) {
$has_errors = FALSE;
// Validate we have all the information we need
$type = key($bundle);
$allowed_types = array('css', 'js');
if (!in_array($type, $allowed_types)) {
drupal_set_message(t("DRAB needs a correct type set, either 'css' or 'js'"), 'error');
$has_errors = TRUE;
}
elseif (!isset($bundle[$type]['files']) && empty($bundle[$type]['files'])) {
drupal_set_message(t("DRAB needs a set of files to bundle"), 'error');
$has_errors = TRUE;
}
// add the theme to the bundle to make life simpler later
$bundle['theme'] = $themes[$key]->name;
// if we have no errors then generate the bundle
if (!$has_errors) {
$file_details = drab_generate_bundled_file($bundle);
// add an extra attribute to make tracking files easier from the
// front end
$file_details['#attributes']['data-drab-name'] = $bundle_name;
// check if we are on a path to add on
foreach ($bundle[$type]['path'] as $bundle_path) {
$alias_path = drupal_get_path_alias($current_path);
$page_match = drupal_match_path($alias_path, $bundle_path);
$in_current_theme = ($theme_info->name == $themes[$key]->name) ? TRUE : FALSE;
if ($page_match || $bundle_path == "all") {
$generated_files[] = $file_details;
}
}
}
}
}
}
}
// generate the link html tags
$files = '';
// check we we have any generate files.
if (!empty($generated_files)) {
foreach ($generated_files as $file) {
$files .= drupal_render($file);
}
}
return $files;
}
/**
* Generates a aggregated file based on the bundle
*
* Returns an array that can by used by drupal_render
*
* @param array $bundle
* @return array
*/
function drab_generate_bundled_file($bundle) {
// vars
$all_css_files = drupal_add_css();
$type = key($bundle);
$files = $bundle[$type]['files'];
$file_group = array();
// copy the file information from the theme in to our custom group
foreach ($files as $file) {
// fix path
$theme_path = drupal_get_path('theme', $bundle['theme']);
$file_path = $theme_path . '/' . $file;
// copy
$file_group[$file_path] = $all_css_files[$file_path];
// make a new group
$file_group[$file_path]['group'] = 456;
// makre sure preprocess is set
$file_group[$file_path]['preprocess'] = 1;
}
$rendered_options = array(
'#items' => $file_group,
'#pre_render' => array('drupal_pre_render_styles'),
'#group_callback' => 'drupal_group_css',
'#aggregate_callback' => 'drupal_aggregate_css',
);
// if the less module exists process the stylesheets
if (module_exists('less')) {
$rendered_options = _less_pre_render($rendered_options);
}
// check for CDN
if (module_exists('cdn')) {
$cdn_status = variable_get('cdn_status');
if ($cdn_status > 0) {
$rendered_options['#aggregate_callback'] = '_cdn_aggregate_css';
}
}
$rendered = drupal_pre_render_styles($rendered_options);
return $rendered[0];
}
/**
* Implements hook_theme_registry_alter().
*/
function drab_theme_registry_alter(&$registry) {
if (($index = array_search('template_process_html', $registry['html']['process functions'], TRUE)) !== FALSE) {
array_splice($registry['html']['process functions'], $index, 1, 'drab_template_process_html_override');
}
}
/**
* Overrides template_process_html()
*
* This pattern was re-used from the Modernizer module. Thanks to rupl,
* Yorirou and fubhy for figuring all this out.
*/
function drab_template_process_html_override(&$variables) {
// Render page_top and page_bottom into top level variables.
$variables['page_top'] = drupal_render($variables['page']['page_top']);
$variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
// Place the rendered HTML for the page body into a top level variable.
$variables['page'] = $variables['page']['#children'];
// Only run if DRAB is enabled
$preprocess_drab_enabled = variable_get('preprocess_drab', FALSE);
$styles = drupal_get_css();
if ($preprocess_drab_enabled) {
$path = current_path();
$drab_files = drab_get_files($path);
// make sure we can always fall back to default drupal_get_css()
if ($drab_files) {
$styles = $drab_files;
}
}
$variables['head'] = drupal_get_html_head();
$variables['css'] = drupal_add_css();
$variables['styles'] = $styles;
$variables['scripts'] = drupal_get_js();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function drab_form_system_performance_settings_alter(&$form, &$form_state, $form_id) {
$form['bandwidth_optimization']['preprocess_drab'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the DRAB settings in the theme'),
'#default_value' => variable_get('preprocess_drab', FALSE),
);
}