1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* List of functions used to alter the theme registry in Bootstrap based themes.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* @addtogroup registry
|
10
|
* @{
|
11
|
*/
|
12
|
|
13
|
// Define additional sub-groups for creating lists for all the theme files.
|
14
|
/**
|
15
|
* @defgroup theme_functions Theme Functions (.func.php)
|
16
|
*
|
17
|
* List of theme functions used in the Drupal Bootstrap base theme.
|
18
|
*
|
19
|
* View the parent topic for additional documentation.
|
20
|
*/
|
21
|
/**
|
22
|
* @defgroup theme_preprocess Theme Preprocess Functions (.vars.php)
|
23
|
*
|
24
|
* List of theme preprocess functions used in the Drupal Bootstrap base theme.
|
25
|
*
|
26
|
* View the parent topic for additional documentation.
|
27
|
*/
|
28
|
/**
|
29
|
* @defgroup theme_process Theme Process Functions (.vars.php)
|
30
|
*
|
31
|
* List of theme process functions used in the Drupal Bootstrap base theme.
|
32
|
*
|
33
|
* View the parent topic for additional documentation.
|
34
|
*/
|
35
|
/**
|
36
|
* @defgroup templates Theme Templates (.tpl.php)
|
37
|
*
|
38
|
* List of theme templates used in the Drupal Bootstrap base theme.
|
39
|
*
|
40
|
* View the parent topic for additional documentation.
|
41
|
*/
|
42
|
|
43
|
/**
|
44
|
* Stub implementation for bootstrap_theme().
|
45
|
*
|
46
|
* This base-theme's custom theme hook implementations. Never define "path"
|
47
|
* or "template" as these are detected and automatically added.
|
48
|
*
|
49
|
* @see bootstrap_theme_registry_alter()
|
50
|
* @see bootstrap_theme()
|
51
|
* @see hook_theme()
|
52
|
*/
|
53
|
function _bootstrap_theme(&$existing, $type, $theme, $path) {
|
54
|
// Bootstrap Carousels.
|
55
|
$hooks['bootstrap_carousel'] = array(
|
56
|
'variables' => array(
|
57
|
'attributes' => array(),
|
58
|
'items' => array(),
|
59
|
'start_index' => 0,
|
60
|
'controls' => TRUE,
|
61
|
'indicators' => TRUE,
|
62
|
'interval' => 5000,
|
63
|
'pause' => 'hover',
|
64
|
'wrap' => TRUE,
|
65
|
),
|
66
|
);
|
67
|
|
68
|
// Bootstrap Dropdowns.
|
69
|
$hooks['bootstrap_dropdown'] = array(
|
70
|
'render element' => 'element',
|
71
|
);
|
72
|
|
73
|
// Bootstrap Modals.
|
74
|
$hooks['bootstrap_modal'] = array(
|
75
|
'variables' => array(
|
76
|
'heading' => '',
|
77
|
'body' => '',
|
78
|
'footer' => '',
|
79
|
'dialog_attributes' => array(),
|
80
|
'attributes' => array(),
|
81
|
'size' => '',
|
82
|
'html_heading' => FALSE,
|
83
|
),
|
84
|
);
|
85
|
|
86
|
// Bootstrap Panels.
|
87
|
$hooks['bootstrap_panel'] = array(
|
88
|
'render element' => 'element',
|
89
|
);
|
90
|
|
91
|
// Bootstrap search form wrapper.
|
92
|
// @todo Remove this as it's not really needed and should use suggestions.
|
93
|
$hooks['bootstrap_search_form_wrapper'] = array(
|
94
|
'render element' => 'element',
|
95
|
);
|
96
|
return $hooks;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Implements hook_theme_registry_alter().
|
101
|
*/
|
102
|
function bootstrap_theme_registry_alter(&$registry) {
|
103
|
// Retrieve the active theme names.
|
104
|
$themes = _bootstrap_get_base_themes(NULL, TRUE);
|
105
|
|
106
|
// Return the theme registry unaltered if it is not Bootstrap based.
|
107
|
if (!in_array('bootstrap', $themes)) {
|
108
|
return;
|
109
|
}
|
110
|
|
111
|
// Inject the "footer" variable default in the existing "table" hook.
|
112
|
// @see https://www.drupal.org/node/806982
|
113
|
// @todo Make this discoverable in some way instead of a manual injection.
|
114
|
$registry['table']['variables']['footer'] = NULL;
|
115
|
|
116
|
// Process registered hooks in the theme registry.
|
117
|
_bootstrap_process_theme_registry($registry, $themes);
|
118
|
|
119
|
// Process registered hooks in the theme registry to add necessary theme hook
|
120
|
// suggestion phased function invocations. This must be run after separately
|
121
|
// and after all includes have been loaded.
|
122
|
_bootstrap_process_theme_registry_suggestions($registry, $themes);
|
123
|
|
124
|
// Post-process theme registry. This happens after all altering has occurred.
|
125
|
foreach ($registry as $hook => $info) {
|
126
|
// Ensure uniqueness.
|
127
|
if (!empty($registry[$hook]['includes'])) {
|
128
|
$registry[$hook]['includes'] = array_unique($info['includes']);
|
129
|
}
|
130
|
if (!empty($registry[$hook]['preprocess functions'])) {
|
131
|
$registry[$hook]['preprocess functions'] = array_unique($info['preprocess functions']);
|
132
|
}
|
133
|
if (!empty($registry[$hook]['process functions'])) {
|
134
|
$registry[$hook]['process functions'] = array_unique($info['process functions']);
|
135
|
}
|
136
|
|
137
|
// Ensure "theme path" is set.
|
138
|
if (!isset($registry[$hook]['theme path'])) {
|
139
|
$registry[$hook]['theme path'] = $GLOBALS['theme_path'];
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Processes registered hooks in the theme registry against list of themes.
|
146
|
*
|
147
|
* Discovers and fills missing elements in the theme registry. This is similar
|
148
|
* to _theme_process_registry(), however severely modified for Bootstrap based
|
149
|
* themes.
|
150
|
*
|
151
|
* All additions or modifications must live in `./templates`, relative to the
|
152
|
* base theme or sub-theme's base folder. These files can be organized in any
|
153
|
* order using sub-folders as it searches recursively.
|
154
|
*
|
155
|
* Adds or modifies the following theme hook keys:
|
156
|
* - `includes`: When a variables file `*.vars.php` is found.
|
157
|
* - `includes`: When a function file `*.func.php` is found.
|
158
|
* - `function`: When a specific theme hook function override is found.
|
159
|
* - `template`: When a template file `*.tpl.php` is found in. Note, if both
|
160
|
* a function and a template are defined, a template implementation will
|
161
|
* always be used and the `function` will be unset.
|
162
|
* - `path`: When a template file `*.tpl.php` is found.
|
163
|
* - `preprocess functions`: When a specific theme hook suggestion function
|
164
|
* `hook_preprocess_HOOK__SUGGESTION` is found.
|
165
|
* - `process functions` When a specific theme hook suggestion function
|
166
|
* `hook_process_HOOK__SUGGESTION` is found.
|
167
|
*
|
168
|
* @param array $registry
|
169
|
* The theme registry array, passed by reference.
|
170
|
* @param string|array $themes
|
171
|
* The name of the theme or list of theme names to process.
|
172
|
*
|
173
|
* @see bootstrap_theme_registry_alter()
|
174
|
* @see _theme_process_registry()
|
175
|
* @see _theme_build_registry()
|
176
|
*/
|
177
|
function _bootstrap_process_theme_registry(array &$registry, $themes) {
|
178
|
// Convert to an array if needed.
|
179
|
if (is_string($themes)) {
|
180
|
$themes = array();
|
181
|
}
|
182
|
|
183
|
// Processor functions work in two distinct phases with the process
|
184
|
// functions always being executed after the preprocess functions.
|
185
|
$variable_process_phases = array(
|
186
|
'preprocess functions' => 'preprocess',
|
187
|
'process functions' => 'process',
|
188
|
);
|
189
|
|
190
|
// Iterate over each theme passed.
|
191
|
// Iterate over the [pre]process phases.
|
192
|
foreach ($variable_process_phases as $phase_key => $phase) {
|
193
|
foreach ($themes as $theme) {
|
194
|
// Get the theme's base path.
|
195
|
$path = drupal_get_path('theme', $theme);
|
196
|
|
197
|
// Find theme function overrides.
|
198
|
foreach (drupal_system_listing('/\.(func|vars)\.php$/', $path, 'name', 0) as $name => $file) {
|
199
|
// Strip off the extension.
|
200
|
if (($pos = strpos($name, '.')) !== FALSE) {
|
201
|
$name = substr($name, 0, $pos);
|
202
|
}
|
203
|
|
204
|
// Transform "-" in file names to "_" to match theme hook naming scheme.
|
205
|
$hook = strtr($name, '-', '_');
|
206
|
|
207
|
// File to be included by core's theme function when a theme hook is
|
208
|
// invoked.
|
209
|
if (isset($registry[$hook])) {
|
210
|
if (!isset($registry[$hook][$phase_key])) {
|
211
|
$registry[$hook][$phase_key] = array();
|
212
|
}
|
213
|
if (!isset($registry[$hook]['includes'])) {
|
214
|
$registry[$hook]['includes'] = array();
|
215
|
}
|
216
|
|
217
|
// Include the file now so functions can be discovered below.
|
218
|
include_once DRUPAL_ROOT . '/' . $file->uri;
|
219
|
if (!in_array($file->uri, $registry[$hook]['includes'])) {
|
220
|
$registry[$hook]['includes'][] = $file->uri;
|
221
|
}
|
222
|
}
|
223
|
}
|
224
|
|
225
|
// Process core's normal functionality.
|
226
|
_theme_process_registry($registry, $theme, $GLOBALS['theme_key'] === $theme ? 'theme' : 'base_theme', $theme, $path);
|
227
|
|
228
|
// Find necessary templates in the theme.
|
229
|
$registry = drupal_array_merge_deep($registry, drupal_find_theme_templates($registry, '.tpl.php', $path));
|
230
|
|
231
|
// Iterate over each registered hook.
|
232
|
foreach ($registry as $hook => $info) {
|
233
|
// Remove function callbacks if a template was found.
|
234
|
if (isset($info['function']) && isset($info['template'])) {
|
235
|
unset($registry[$hook]['function']);
|
236
|
}
|
237
|
|
238
|
// Correct template theme paths.
|
239
|
if (!isset($info['theme path'])) {
|
240
|
$registry[$hook]['theme path'] = $path;
|
241
|
}
|
242
|
|
243
|
// Correct the type that is implementing this override.
|
244
|
$registry[$hook]['type'] = $GLOBALS['theme_path'] === $registry[$hook]['theme path'] ? 'theme' : 'base_theme';
|
245
|
|
246
|
// Sort the phase functions.
|
247
|
// @see https://www.drupal.org/node/2098551
|
248
|
_bootstrap_registry_sort_phase_functions($registry[$hook][$phase_key], $hook, $phase, $themes);
|
249
|
|
250
|
// Setup a default "context" variable. This allows #context to be passed
|
251
|
// to every template and theme function.
|
252
|
// @see https://www.drupal.org/node/2035055
|
253
|
if (isset($info['variables']) && !isset($info['variables']['context'])) {
|
254
|
$registry[$hook]['variables']['context'] = array();
|
255
|
}
|
256
|
|
257
|
// Setup a default "icon" variable. This allows #icon to be passed
|
258
|
// to every template and theme function.
|
259
|
// @see https://www.drupal.org/node/2219965
|
260
|
if (isset($info['variables']) && !isset($info['variables']['icon'])) {
|
261
|
$registry[$hook]['variables']['icon'] = NULL;
|
262
|
}
|
263
|
if (isset($info['variables']) && !isset($info['variables']['icon_position'])) {
|
264
|
$registry[$hook]['variables']['icon_position'] = 'before';
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
}
|
269
|
}
|
270
|
|
271
|
/**
|
272
|
* Ensures the phase functions are invoked in the correct order.
|
273
|
*
|
274
|
* @param array $functions
|
275
|
* The phase functions to iterate over.
|
276
|
* @param string $hook
|
277
|
* The current hook being processed.
|
278
|
* @param string $phase
|
279
|
* The current phase being processed.
|
280
|
* @param array $themes
|
281
|
* An indexed array of current themes.
|
282
|
*
|
283
|
* @see https://www.drupal.org/node/2098551
|
284
|
*/
|
285
|
function _bootstrap_registry_sort_phase_functions(array &$functions, $hook, $phase, array $themes) {
|
286
|
// Immediately return if there is nothing to sort.
|
287
|
if (count($functions) < 2) {
|
288
|
return;
|
289
|
}
|
290
|
|
291
|
// Create an associative array of theme functions to ensure sort order.
|
292
|
$theme_functions = array_fill_keys($themes, array());
|
293
|
|
294
|
// Iterate over all the themes.
|
295
|
foreach ($themes as $theme) {
|
296
|
// Only add the function to the array of theme functions if it currently
|
297
|
// exists in the $functions array.
|
298
|
$function = $theme . '_' . $phase . '_' . $hook;
|
299
|
$key = array_search($function, $functions);
|
300
|
if ($key !== FALSE) {
|
301
|
// Save the theme function to be added later, but sorted.
|
302
|
$theme_functions[$theme][] = $function;
|
303
|
|
304
|
// Remove it from the current $functions array.
|
305
|
unset($functions[$key]);
|
306
|
}
|
307
|
}
|
308
|
|
309
|
// Iterate over all the captured theme functions and place them back into
|
310
|
// the phase functions array.
|
311
|
foreach ($theme_functions as $array) {
|
312
|
$functions = array_merge($functions, $array);
|
313
|
}
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* Processes registered hooks in the theme registry against list of themes.
|
318
|
*
|
319
|
* This is used to add the necessary phased functions to theme hook suggestions.
|
320
|
* Because it uses get_defined_functions(), it must be invoked after all
|
321
|
* includes have been detected and loaded. This is similar to
|
322
|
* drupal_find_theme_functions(), however severely modified for Bootstrap based
|
323
|
* themes.
|
324
|
*
|
325
|
* @param array $registry
|
326
|
* The theme registry array, passed by reference.
|
327
|
* @param string|array $themes
|
328
|
* The name of the theme or list of theme names to process.
|
329
|
*
|
330
|
* @see https://www.drupal.org/node/939462
|
331
|
* @see drupal_find_theme_functions()
|
332
|
*/
|
333
|
function _bootstrap_process_theme_registry_suggestions(array &$registry, $themes) {
|
334
|
// Convert to an array if needed.
|
335
|
if (is_string($themes)) {
|
336
|
$themes = array();
|
337
|
}
|
338
|
|
339
|
// Merge in normal core detections first.
|
340
|
$registry = drupal_array_merge_deep($registry, drupal_find_theme_functions($registry, $themes));
|
341
|
|
342
|
// Processor functions work in two distinct phases with the process
|
343
|
// functions always being executed after the preprocess functions.
|
344
|
$variable_process_phases = array(
|
345
|
'preprocess functions' => 'preprocess',
|
346
|
'process functions' => 'process',
|
347
|
);
|
348
|
|
349
|
$grouped_functions = drupal_group_functions_by_prefix();
|
350
|
|
351
|
// Iterate over each theme passed.
|
352
|
foreach ($themes as $theme) {
|
353
|
// Iterate over each registered hook.
|
354
|
foreach ($registry as $hook => $info) {
|
355
|
// The pattern to match.
|
356
|
$pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
|
357
|
|
358
|
// Only process hooks that have not explicitly "turned off" patterns.
|
359
|
if (empty($pattern)) {
|
360
|
continue;
|
361
|
}
|
362
|
|
363
|
// Iterate over the [pre]process phases.
|
364
|
foreach ($variable_process_phases as $phase_key => $phase) {
|
365
|
// Find functions matching the specific theme and phase prefix.
|
366
|
$prefix = $theme . '_' . $phase;
|
367
|
|
368
|
// Grep only the functions which are within the prefix group.
|
369
|
list($first_prefix,) = explode('_', $prefix, 2);
|
370
|
if (isset($grouped_functions[$first_prefix]) && ($matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $grouped_functions[$first_prefix]))) {
|
371
|
foreach ($matches as $match) {
|
372
|
// Determine the current theme implementation.
|
373
|
$hook = substr($match, strlen($prefix) + 1);
|
374
|
$base_hook = $hook;
|
375
|
|
376
|
// If there's no current theme implementation, keep checking for
|
377
|
// more generic base hooks. If there's still no implementation,
|
378
|
// one must be created using the last found implementation
|
379
|
// information.
|
380
|
if (!isset($registry[$base_hook]) || isset($registry[$base_hook]['base hook'])) {
|
381
|
// Iteratively strip everything after the last '__' delimiter,
|
382
|
// until an implementation is found.
|
383
|
while ($pos = strrpos($base_hook, '__')) {
|
384
|
$base_hook = substr($base_hook, 0, $pos);
|
385
|
if (isset($registry[$base_hook])) {
|
386
|
break;
|
387
|
}
|
388
|
}
|
389
|
|
390
|
// No base hook was found, this allows the implementation to be
|
391
|
// ignored in the next steps.
|
392
|
if (!isset($registry[$base_hook])) {
|
393
|
$base_hook = FALSE;
|
394
|
}
|
395
|
}
|
396
|
|
397
|
// Process specific base hook implementations if necessary.
|
398
|
if ($base_hook) {
|
399
|
// The matched theme implementation does not exist in the
|
400
|
// registry, one must be created if base hook information was
|
401
|
// found, otherwise it will be ignored.
|
402
|
if (!isset($registry[$hook])) {
|
403
|
$registry[$base_hook] += array(
|
404
|
'type' => 'theme',
|
405
|
'preprocess functions' => array(),
|
406
|
'process functions' => array(),
|
407
|
);
|
408
|
$hook_type = isset($registry[$base_hook]['function']) ? 'function' : 'template';
|
409
|
$arg_name = isset($registry[$base_hook]['variables']) ? 'variables' : 'render element';
|
410
|
$registry[$hook] = array(
|
411
|
$hook_type => $registry[$base_hook][$hook_type],
|
412
|
$arg_name => $registry[$base_hook][$arg_name],
|
413
|
'base hook' => $base_hook,
|
414
|
'type' => $registry[$base_hook]['type'],
|
415
|
'preprocess functions' => array(),
|
416
|
'process functions' => array(),
|
417
|
);
|
418
|
if (isset($registry[$base_hook]['path'])) {
|
419
|
$registry[$hook]['path'] = $registry[$base_hook]['path'];
|
420
|
}
|
421
|
if (isset($registry[$base_hook]['theme path'])) {
|
422
|
$registry[$hook]['theme path'] = $registry[$base_hook]['theme path'];
|
423
|
}
|
424
|
}
|
425
|
}
|
426
|
|
427
|
// If the hook exists, merge in the functions. Otherwise ignore it
|
428
|
// since there was no base hook found and a new implementation
|
429
|
// could not be created.
|
430
|
if (isset($registry[$hook])) {
|
431
|
$registry[$hook] = drupal_array_merge_deep($registry[$hook], array(
|
432
|
$phase_key => array($match),
|
433
|
));
|
434
|
|
435
|
// Due to how theme() functions, if a base hook implements
|
436
|
// preprocess or process functions, then the base hook info is
|
437
|
// used to invoke the necessary phase functions instead of the
|
438
|
// suggestion hook info. To get around this, a helper function
|
439
|
// must be appended to the base hook info so it can call the
|
440
|
// theme suggestion implementation's phase function.
|
441
|
$function = '_bootstrap_' . $phase . '_theme_suggestion';
|
442
|
if (!in_array($function, $registry[$base_hook][$phase_key])) {
|
443
|
$registry[$base_hook][$phase_key][] = $function;
|
444
|
}
|
445
|
}
|
446
|
}
|
447
|
}
|
448
|
}
|
449
|
}
|
450
|
}
|
451
|
}
|
452
|
|
453
|
/**
|
454
|
* Performance gain.
|
455
|
*
|
456
|
* Do not remove from 7.x. This function is not available in every core version.
|
457
|
*
|
458
|
* @see https://www.drupal.org/node/2339447
|
459
|
*/
|
460
|
if (!function_exists('drupal_group_functions_by_prefix')) {
|
461
|
|
462
|
/**
|
463
|
* Group all user functions by word before first underscore.
|
464
|
*
|
465
|
* @return array
|
466
|
* Functions grouped by the first prefix.
|
467
|
*/
|
468
|
function drupal_group_functions_by_prefix() {
|
469
|
$functions = get_defined_functions();
|
470
|
$grouped_functions = array();
|
471
|
|
472
|
// Splitting user defined functions into groups by the first prefix.
|
473
|
foreach ($functions['user'] as $function) {
|
474
|
list($first_prefix,) = explode('_', $function, 2);
|
475
|
$grouped_functions[$first_prefix][] = $function;
|
476
|
}
|
477
|
|
478
|
return $grouped_functions;
|
479
|
}
|
480
|
|
481
|
}
|
482
|
|
483
|
/**
|
484
|
* @} End of "addtogroup registry".
|
485
|
*/
|