file registry.inc

List of functions used to alter the theme registry in Bootstrap based themes.

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

Functions

Name Description
bootstrap_theme_registry_alter Implements hook_theme_registry_alter().
_bootstrap_process_theme_registry Processes registered hooks in the theme registry against list of themes.
_bootstrap_process_theme_registry_suggestions Processes registered hooks in the theme registry against list of themes.
_bootstrap_registry_sort_phase_functions Ensures the phase functions are invoked in the correct order.
_bootstrap_theme Stub implementation for bootstrap_theme().