Skip to content

Instantly share code, notes, and snippets.

@mootari
Created May 20, 2015 21:05
Show Gist options
  • Save mootari/e2bab2a75a96f8828d1e to your computer and use it in GitHub Desktop.
Save mootari/e2bab2a75a96f8828d1e to your computer and use it in GitHub Desktop.
Preprocessing for theme hook suggestions. #drupal #theme
<?php
/**
* Provides additional preprocess hooks for theme hook suggestions.
*/
function _THEME_process_hooks(&$vars, $process = 'preprocess', $hooks = null) {
$prefix = '_THEME';
if(is_null($hooks)) {
$hooks = $vars['theme_hook_suggestions'];
}
static $static_fast;
if(is_null($static_fast)) {
$static_fast['functions'] = &drupal_static(__FUNCTION__, array());
}
foreach($hooks as $hook) {
if(!isset($static_fast['functions'][$hook][$process])) {
$function = $prefix . '_' . $process . '__' . $hook;
$static_fast['functions'][$hook][$process] = function_exists($function) ? $function : false;
}
if($static_fast['functions'][$hook][$process]) {
$static_fast['functions'][$hook][$process]($vars);
}
}
}
/**
* Provide additional theme_hook_suggestions for node types based
* on viewmodes
*/
function THEME_preprocess_node(&$vars) {
$type = $vars['type'];
$view_mode = $vars['view_mode'] ? $vars['view_mode'] : 'full';
$vars['theme_hook_suggestions'] = array(
"node__{$type}",
"node__{$type}__{$view_mode}",
);
_THEME_process_hooks($vars, 'preprocess');
}
function _THEME_preprocess_node__page(&$vars) {
// Do stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment