1 |
87dbc3bf
|
Benjamin Luce
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* registry.inc
|
5 |
|
|
*
|
6 |
|
|
* Contains functions specific to the theme registry.
|
7 |
|
|
*
|
8 |
|
|
* Theme templates, functions and [pre]process functions
|
9 |
|
|
*
|
10 |
|
|
* Theme templates `*.tpl.php` files are stored in the `theme` directory along
|
11 |
|
|
* with `*.func.php` and `*.vars.php` files. The latter two are enabled by the
|
12 |
|
|
* processing done below. The three types of files can be grouped into
|
13 |
|
|
* sub-directories. It is recommended that they are grouped by the modules
|
14 |
|
|
* they originate from. Theme specific hooks should be grouped into a folder
|
15 |
|
|
* named after the theme itself.
|
16 |
|
|
*
|
17 |
|
|
* Function files store theme functions directly related to the base file name
|
18 |
|
|
* or "theme hook", e.g., the hook `foo` would be handled by the
|
19 |
|
|
* `bootstrap_foo` function which would be stored in `foo.func.php`.
|
20 |
|
|
*
|
21 |
|
|
* Variable files store [pre]process functions directly related to the base file
|
22 |
|
|
* file name or "theme hook" just like function files, e.g., the hook `html`
|
23 |
|
|
* would be handled by the `bootstrap_preprocess_html` function held in
|
24 |
|
|
* `html.vars.php`.
|
25 |
|
|
*
|
26 |
|
|
* The two types of files mentioned above should also be used to hold support
|
27 |
|
|
* functions specific to the theming hook. When a function grows too large,
|
28 |
|
|
* break it apart so it becomes more readable.
|
29 |
|
|
*
|
30 |
|
|
* Files specific to theme functions and variable preprocess/process functions
|
31 |
|
|
* should be named after the `base hook`. Alternate hooks (or what is commonly
|
32 |
|
|
* referred to as "hook suggestions") should not be used for the file name.
|
33 |
|
|
* The contents of the theme functions file can hold hook suggestions and it
|
34 |
|
|
* should be related to the base hook. [Pre]process functions for hook
|
35 |
|
|
* suggestions are not supported in Drupal 7 core.
|
36 |
|
|
*
|
37 |
|
|
* Using these files will keep the `template.php` file clean and grouping them
|
38 |
|
|
* within the theme directory will make it easier to discover and work with.
|
39 |
|
|
*
|
40 |
|
|
* Warning: Using core's `path_to_theme` function may return the wrong path.
|
41 |
|
|
* It is difficult to predict to begin with and the changes made here will
|
42 |
|
|
* make it even worse. Use `drupal_get_path('theme', 'bootstrap')` instead.
|
43 |
|
|
*/
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* Stub implementation for hook_theme().
|
47 |
|
|
*
|
48 |
|
|
* @see bootstrap_theme()
|
49 |
|
|
* @see hook_theme()
|
50 |
|
|
*/
|
51 |
|
|
function _bootstrap_theme(&$existing, $type, $theme, $path) {
|
52 |
|
|
// If we are auto-rebuilding the theme registry, warn about the feature.
|
53 |
|
|
if (
|
54 |
|
|
// Only display for site config admins.
|
55 |
|
|
isset($GLOBALS['user']) && function_exists('user_access') && user_access('administer site configuration')
|
56 |
|
|
&& theme_get_setting('bootstrap_rebuild_registry')
|
57 |
|
|
// Always display in the admin section, otherwise limit to three per hour.
|
58 |
|
|
&& (arg(0) == 'admin' || flood_is_allowed($GLOBALS['theme'] . '_rebuild_registry_warning', 3))
|
59 |
|
|
) {
|
60 |
|
|
flood_register_event($GLOBALS['theme'] . '_rebuild_registry_warning');
|
61 |
|
|
drupal_set_message(t('For easier theme development, the theme registry is being rebuilt on every page request. It is <em>extremely</em> important to <a href="!link">turn off this feature</a> on production websites.', array('!link' => url('admin/appearance/settings/' . $GLOBALS['theme']))), 'warning', FALSE);
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
// Custom theme hooks:
|
65 |
|
|
// Do not define the `path` or `template`.
|
66 |
|
|
$hook_theme = array(
|
67 |
|
|
'bootstrap_links' => array(
|
68 |
|
|
'variables' => array(
|
69 |
|
|
'links' => array(),
|
70 |
|
|
'attributes' => array(),
|
71 |
|
|
'heading' => NULL,
|
72 |
|
|
),
|
73 |
|
|
),
|
74 |
|
|
'bootstrap_btn_dropdown' => array(
|
75 |
|
|
'variables' => array(
|
76 |
|
|
'links' => array(),
|
77 |
|
|
'attributes' => array(),
|
78 |
|
|
'type' => NULL,
|
79 |
|
|
),
|
80 |
|
|
),
|
81 |
|
|
'bootstrap_modal' => array(
|
82 |
|
|
'variables' => array(
|
83 |
|
|
'heading' => '',
|
84 |
|
|
'body' => '',
|
85 |
|
|
'footer' => '',
|
86 |
|
|
'attributes' => array(),
|
87 |
|
|
'html_heading' => FALSE,
|
88 |
|
|
),
|
89 |
|
|
),
|
90 |
|
|
'bootstrap_accordion' => array(
|
91 |
|
|
'variables' => array(
|
92 |
|
|
'id' => '',
|
93 |
|
|
'elements' => array(),
|
94 |
|
|
),
|
95 |
|
|
),
|
96 |
|
|
'bootstrap_search_form_wrapper' => array(
|
97 |
|
|
'render element' => 'element',
|
98 |
|
|
),
|
99 |
|
|
'bootstrap_panel' => array(
|
100 |
|
|
'render element' => 'element',
|
101 |
|
|
),
|
102 |
|
|
);
|
103 |
|
|
|
104 |
|
|
// Process custom. This should be used again for any sub-themes.
|
105 |
|
|
bootstrap_hook_theme_complete($hook_theme, $theme, $path . '/theme');
|
106 |
|
|
|
107 |
|
|
// Process existing registry. Only invoke once from base theme.
|
108 |
|
|
bootstrap_hook_theme_complete($existing, $theme, $path . '/theme');
|
109 |
|
|
|
110 |
|
|
return $hook_theme;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Discovers and fills missing elements in the theme registry.
|
115 |
|
|
*
|
116 |
|
|
* Adds the following:
|
117 |
|
|
* - `includes` `*.vars.php` if variables file is found.
|
118 |
|
|
* - `includes` `*.func.php` if function file is found.
|
119 |
|
|
* - `function` if the function for $theme is found.
|
120 |
|
|
* - `path` if template file is found.
|
121 |
|
|
* - `template` if template file is found.
|
122 |
|
|
*/
|
123 |
|
|
function bootstrap_hook_theme_complete(&$registry, $theme, $path) {
|
124 |
|
|
$registry = drupal_array_merge_deep(
|
125 |
|
|
$registry,
|
126 |
|
|
bootstrap_find_theme_includes($registry, '.vars.php', $path),
|
127 |
|
|
bootstrap_find_theme_includes($registry, '.func.php', $path),
|
128 |
|
|
drupal_find_theme_functions($registry, array($theme)),
|
129 |
|
|
drupal_find_theme_templates($registry, '.tpl.php', $path)
|
130 |
|
|
);
|
131 |
|
|
// Post-process the theme registry.
|
132 |
|
|
foreach ($registry as $hook => $info) {
|
133 |
|
|
// Core find functions above does not carry over the base `theme path` when
|
134 |
|
|
// finding suggestions. Add it to prevent notices for `theme` calls.
|
135 |
|
|
if (!isset($info['theme path']) && isset($info['base hook'])) {
|
136 |
|
|
$registry[$hook]['theme path'] = $path;
|
137 |
|
|
}
|
138 |
|
|
// Setup a default "context" variable. This allows #context to be passed
|
139 |
|
|
// to every template and theme function.
|
140 |
|
|
// @see https://drupal.org/node/2035055
|
141 |
|
|
if (isset($info['variables']) && !isset($info['variables']['context'])) {
|
142 |
|
|
$registry[$hook]['variables'] += array(
|
143 |
|
|
'context' => array(),
|
144 |
|
|
);
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Discovers and sets the path to each `THEME-HOOK.$extension` file.
|
151 |
|
|
*/
|
152 |
|
|
function bootstrap_find_theme_includes($registry, $extension, $path) {
|
153 |
|
|
$regex = '/' . str_replace('.', '\.', $extension) . '$/';
|
154 |
|
|
$files = drupal_system_listing($regex, $path, 'name', 0);
|
155 |
|
|
|
156 |
|
|
$hook_includes = array();
|
157 |
|
|
foreach ($files as $name => $file) {
|
158 |
|
|
// Chop off the remaining extension.
|
159 |
|
|
if (($pos = strpos($name, '.')) !== FALSE) {
|
160 |
|
|
$name = substr($name, 0, $pos);
|
161 |
|
|
}
|
162 |
|
|
// Transform "-" in filenames to "_" to match theme hook naming scheme.
|
163 |
|
|
$hook = strtr($name, '-', '_');
|
164 |
|
|
// File to be included by core's theme function when the hook is invoked.
|
165 |
|
|
// This only applies to base hooks. When hook derivatives are called
|
166 |
|
|
// (those with a double "__"), it checks for the base hook, calls its
|
167 |
|
|
// variable processors and ignores anything specific to the derivative.
|
168 |
|
|
// Due to the way it works, It becomes redundant to give it a path that is
|
169 |
|
|
// not a base hook.
|
170 |
|
|
// @see https://drupal.org/node/939462
|
171 |
|
|
if (isset($registry[$hook]) && !isset($registry[$hook]['base hook'])) {
|
172 |
|
|
// Include the file so core can discover any containing functions.
|
173 |
|
|
include_once DRUPAL_ROOT . '/' . $file->uri;
|
174 |
|
|
$hook_includes[$hook]['includes'][] = $file->uri;
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
return $hook_includes;
|
179 |
|
|
} |