Last active
March 11, 2021 21:43
-
-
Save dvessel/5884501 to your computer and use it in GitHub Desktop.
Drupal theme registry alterations to allow theme functions, preprocess/process function to be organized like templates (.tpl.php).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Theme templates, functions and preprocess/process functions | |
* | |
* Theme templates `*.tpl.php` files are stored in the `theme` directory along | |
* with `*.func.php` and `*.vars.php` files. The latter two are enabled by the | |
* processing done below. The three types of files can be grouped into | |
* sub-directories. It is recommended that they are grouped by the modules | |
* they originate from. Theme specific hooks should be grouped into a folder | |
* named after the theme itself. | |
* | |
* Function files store theme functions directly related to the base file name | |
* or "theme hook", e.g., the hook `foo` would be handled by the | |
* `THEMENAME_foo` function which would be stored in `foo.func.php`. | |
* | |
* Variable files store preprocess and process functions directly related to | |
* the base file name or "theme hook" just like function files, e.g., the hook | |
* `html` would be handled by the `THEMENAME_preprocess_html` function held in | |
* `html.vars.php`. | |
* | |
* The two types of files mentioned above should also be used to hold support | |
* functions specific to the theming hook. When a function grows too large, | |
* break it apart so it becomes more readable. | |
* | |
* Files specific to theme functions and variable preprocess/process functions | |
* should be named after the `base hook`. Alternate hooks (or what is commonly | |
* referred to as "hook suggestions") should not be used for the file name. | |
* The contents of the theme functions file can hold hook suggestions and it | |
* should be related to the base hook. Preprocess/process functions for hook | |
* suggestions are not supported in Drupal core. | |
* | |
* Using these files will keep the `template.php` file clean and grouping them | |
* within the theme directory will make it easier to discover and work with. | |
* | |
* Warning: Using core's `path_to_theme` function may return the wrong path. | |
* It was difficult to predict to begin with and the changes made here will | |
* make it even worse. Use `drupal_get_path('theme', 'THEMENAME')` instead. | |
*/ | |
/** | |
* implements hook_theme(). | |
*/ | |
function THEMENAME_theme(&$existing, $type, $theme, $path) { | |
// Custom theme hooks: | |
// Do not define the `path` or `template`. | |
$hook_theme = array( | |
); | |
// Process custom. This should be used again for any sub-themes. | |
THEMENAME_hook_theme_complete($hook_theme, $theme, "$path/theme"); | |
// Process existing registry. This should be done only once from the base theme. | |
THEMENAME_hook_theme_complete($existing, $theme, "$path/theme"); | |
return $hook_theme; | |
} | |
/** | |
* Discovers and fills missing elements in the theme registry. | |
* | |
* Adds the following: | |
* - `includes` `*.vars.php` if variables file is found. | |
* - `includes` `*.func.php` if function file is found. | |
* - `function` if the function for $theme is found. | |
* - `path` if template file is found. | |
* - `template` if template file is found. | |
*/ | |
function THEMENAME_hook_theme_complete(&$registry, $theme, $path) { | |
$registry = drupal_array_merge_deep( | |
$registry, | |
THEMENAME_find_theme_includes($registry, '.vars.php', $path), | |
THEMENAME_find_theme_includes($registry, '.func.php', $path), | |
drupal_find_theme_functions($registry, array($theme)), | |
drupal_find_theme_templates($registry, '.tpl.php', "$path") | |
); | |
} | |
/** | |
* Discovers and sets the path to each `THEME-HOOK.$extension` file. | |
*/ | |
function THEMENAME_find_theme_includes($registry, $extension, $path) { | |
$regex = '/' . str_replace('.', '\.', $extension) . '$/'; | |
$files = drupal_system_listing($regex, $path, 'name', 0); | |
$hook_includes = array(); | |
foreach ($files as $name => $file) { | |
// Chop off the remaining extension. | |
if (($pos = strpos($name, '.')) !== FALSE) { | |
$name = substr($name, 0, $pos); | |
} | |
// Transform - in filenames to _ to match theme hook naming scheme. | |
$hook = strtr($name, '-', '_'); | |
// File to be included by core's theme function when the hook is invoked. | |
// This only applies to base hooks. When hook derivatives are called | |
// (those with a double '__'), it checkes for the base hook, calls its | |
// variable processors and ignores anything specific to the derivative. | |
// Due to the way it works, It becomes redundant to give it a path that is | |
// not a base hook. | |
if (isset($registry[$hook]) && !isset($registry[$hook]['base hook'])) { | |
// Include the file so core can discover any containing functions. | |
include_once DRUPAL_ROOT . '/' . $file->uri; | |
$hook_includes[$hook]['includes'][] = $file->uri; | |
} | |
} | |
return $hook_includes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment