Skip to content

Instantly share code, notes, and snippets.

@paulhhowells
Last active December 19, 2015 16:19
Show Gist options
  • Select an option

  • Save paulhhowells/5982709 to your computer and use it in GitHub Desktop.

Select an option

Save paulhhowells/5982709 to your computer and use it in GitHub Desktop.
inhouse theme template.php - (work in progress)
<?php
/* C U S T O M F U N C T I O N S */
/**
* search an array recursively to find
* the first instance of a key and return its value
*
* useful when you do not know the location of a key nested (only once) in a multidimensional array
*
*/
function array_find_first_value ($needle_key, array $haystack) {
if (array_key_exists($needle_key, $haystack)) {
return $haystack[$needle_key];
}
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$result = array_find_first_value($needle_key, $value);
if ($result) {
return $result;
}
}
}
return false;
}
/* D R U P A L F U N C T I O N S */
/**
* Implements theme_field()
* simplifies field mark-up on front_page node
*/
function inhouse_field__front_page($variables) {
$output = '';
// Render the label, if it's not hidden.
//if (!$variables['label_hidden']) {
//$output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
//}
// Render the items.
// $output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
foreach ($variables['items'] as $delta => $item) {
// $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
// $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
$output .= drupal_render($item);
}
//$output .= '</div>';
// Render the top-level DIV.
// $output = '<div class="xxx ' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
return $output;
}
/**
* Implements hook_preprocess_html().
*
* http://lin-clark.com/blog/theming-html5-and-rdfa-drupal-7
* pinched from Omega
*/
function inhouse_preprocess_html(&$vars) {
// use values from rdf module if it's enabled
if (module_exists('rdf')) {
$vars['doctype'] = '<!doctype html PUBLIC "-//W3C//DTD HTML+RDFa 1.1//EN">' . "\n";
$vars['rdf_version'] = ' version="HTML+RDFa 1.1"';
$vars['rdf_profile'] = ' profile="' . $vars['grddl_profile'] . '"';
}
else {
$vars['doctype'] = '<!doctype html>' . "\n";
$vars['rdf_version'] = '';
$vars['rdf_profile'] = '';
}
/*
* use with this markup:
<?php print $doctype; ?>
<html lang="<?php print $language->language; ?>" dir="<?php print $language->dir; ?>"<?php print $rdf_version . $rdf_namespaces; ?>>
<head<?php print $rdf_profile; ?>>
*/
// Setup IE meta tag to force IE rendering mode
$meta_ie_render_engine = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'content' => 'IE=edge,chrome=1',
'http-equiv' => 'X-UA-Compatible',
)
);
// Add header meta tag for IE to head
drupal_add_html_head($meta_ie_render_engine, 'meta_ie_render_engine');
}
/**
* implements sonspring_process_html_tag
* Purge XHTML stuff unneeded by HTML5
*/
function inhouse_process_html_tag(&$vars) {
$el = &$vars['element'];
// Remove type="..." and CDATA prefix/suffix.
unset($el['#attributes']['type'], $el['#value_prefix'], $el['#value_suffix']);
// Remove media="all" but leave others unaffected.
if (isset($el['#attributes']['media']) && $el['#attributes']['media'] === 'all') {
unset($el['#attributes']['media']);
}
}
/**
* Implements hook_html_head_alter().
* This will overwrite the default meta character type tag with HTML5 version.
*/
function inhouse_html_head_alter(&$head_elements) {
$head_elements['system_meta_content_type']['#attributes'] = array(
'charset' => 'utf-8'
);
// Add viewport settings
// <meta name="viewport" content="width=device-width, initial-scale=1" />
$head_elements['viewport'] = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'width=device-width, initial-scale=1.0',
),
);
}
/**
* Implements theme_preprocess_page
*/
function inhouse_preprocess_page(&$vars, $hook) {
$render_tabs = render($vars['tabs']);
$vars['render_tabs'] = $render_tabs;
$vars['show_render_tabs'] = strlen($render_tabs) ? TRUE : FALSE; // being explicit about the truthyness of an integer greater than zero
// $vars['primary_local_tasks'] = menu_primary_local_tasks();
// $vars['secondary_local_tasks'] = menu_secondary_local_tasks();
// truthyness: empty is inverse of if
$vars['path_to_theme'] = base_path() . path_to_theme(); // $base_path . $directory may not be adequate if tpl is not in theme
// if it's a panel-page then one will want to print the panel-page title as an H1 in html.tpl and not within the panel as an H2
// create a variable to identify panel pages:
$vars['is_panel_page'] = FALSE;
$vars['is_panel_page_with_h1'] = FALSE;
if (module_exists('panels') && function_exists('panels_get_current_page_display')) {
$panels_display = panels_get_current_page_display();
// update $is_panel_page variable
$vars['is_panel_page'] = !empty($panels_display);
// update $is_panel_page_with_h1 variable
// if content exists, need to loop through each in array, and if panel-type exists then check it for page_title
if (!empty($panels_display->content)) {
foreach($panels_display->content as $panel) {
if (!empty($panel->type) && ($panel->type == 'page_title')) {
$vars['is_panel_page_with_h1'] = TRUE;
// break out of loop as soon as the page_title is found
break 1;
}
}
}
}
// add strapline variable to page.tpl
$vars['strapline'] = FALSE;
if (!empty($vars['node'])) {
$field_strapline_array = field_get_items('node', $vars['node'], 'field_strapline');
if ($field_strapline_array) {
$vars['strapline'] = $field_strapline_array[0]['value'];
}
}
// $variables['my_new_variable'] = 'some value';
// $variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;
// $example = $variables['node']->field_example // get at cck fields on node
}
/**
* Implements template_preprocess_block
*
* enable bean blocks to be themed by bean-type:
* i.e. block__bean__BEAN-TYPE.tpl
*
* add classes to block according to region & module
*
*/
function inhouse_preprocess_block(&$variables) {
// In the header region visually hide block titles.
/*
if ($variables['block']->region == 'header') {
// $variables['title_attributes_array']['class'][] = 'element-invisible';
}
*/
$block = $variables['block'];
$block_region = $block->region;
$block_module = $block->module;
$elements = $variables['elements'];
// select Bean Blocks and ignore other Blocks
if (!empty($block_module) && ($block_module == 'bean')) {
// the location of the Bean array within $variables is a moving target
// e.g. use of the Context Module changes it's location
if (array_key_exists('bean', $elements)) {
$bean_array = $elements['bean'];
} else if (array_key_exists('bean', $elements['content'])) {
$bean_array = $elements['content']['bean'];
} else {
$bean_array = array_find_first_value('bean', $elements);
}
// test that the Bean array has been found
if ($bean_array) {
$mystery_key_array = element_children($bean_array);
if ($mystery_key_array) {
$bean = $bean_array[$mystery_key_array[0]];
if (!empty($bean['#bundle'])) {
$variables['theme_hook_suggestions'][] = 'block__' . $block_module . '__' . $bean['#bundle'];
}
}
}
}
// create a hook for the block identifying the region it is in
/*
if(!empty($block_region)) {
// $variables['classes_array'][] = "block--" . str_replace(array('_'), array('-'), $block_region); // refactor with strtr()
$variables['classes_array'][] = strtr($block_region, '_', '-');
// perhaps a class identifying the region & module is unnecessary, but here it is:
$class = "block--" . $block_region;
if(!empty($block_module)) {
$class .= "--" . $block_module;
}
// $class = str_replace(array('_'), array('-'), $class); // refactor with strtr()
$class = strtr($class, '_', '-');
$variables['classes_array'][] = $class;
}
*/
// choose classes to be added to a block, based on the region it is in, by module type
/*
$regions = array(
"main" => array(
"system" => "block--system--main",
),
"content" => array(
"system" => "block--system--content",
),
"sidebar_first" => array(
"system" => "block--system--sidebar-first",
),
"footer" => array(
"user" => "block--user--footer",
"system" => "block--system--footer",
),
);
// check that we have a classes string for this region
if (!empty($block_region) && array_key_exists($block_region, $regions)) {
$modules = $regions[$block_region];
// check that we have a classes string for this module
if (!empty($modules) && array_key_exists($block_module, $modules)) {
$variables['classes_array'][] = $modules[$block_module];
}
}
*/
// choose classes to be added to a block, based on the region it is in, by module type
// this would be a lot prettier if it was php5.4 or json []
// doesn't pick up on blocks inside panels - panels IS the block
$classes = array(
'regions' => array(
'main' => array( // main region
'modules' => array(
'system' => array( // system module
'classes' => array(
'block--system--main',
'p-main-system',
),
),
),
),
'content' => array(
'modules' => array(
'system' => array( // system module
'classes' => array(
'block--system--content',
'p-content-system',
),
),
),
),
'sidebar_first' => array(
'modules' => array(
'system' => array( // system module
'classes' => array(
'block--system--sidebar-first',
),
),
),
),
'footer' => array(
'modules' => array(
'system' => array( // system module
'classes' => array(
'block--system--footer',
),
),
'user' => array( // system module
'classes' => array(
'block--user--footer',
),
),
'menu' => array( // system module
'classes' => array(
'block--menu--footer',
'u-3',
),
// 'inner_classes' => 'inner-menu-class-test', // may be string or array of strings
'inner_classes' => array(
'inner-menu-class-test',
),
),
),
/*
'classes' => array(
'',
),
*/
),
),
);
// use to track down the conditions you want to test for with block_classes(), turn on Devel module and use:
// kpr('$is_front: ' . $variables['is_front'] . '$has_nodes: ' . (!empty($variables['elements']['nodes'])) . ' $block_region: ' . $block->region . ' $block_module: ' . $block->module . ' $block_delta: ' . $block->delta);
// kpr('$block_region: ' . $block->region . ' $block_module: ' . $block->module . ' $block_delta: ' . $block->delta);
// $variables['inner_classes'] = '';
block_classes($variables, $classes);
}
/**
* Implements theme_breadcrumb
*/
function inhouse_breadcrumb($variables) {
// › » |
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users. Make the heading invisible with .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
$output .= '<div class="breadcrumbs">' . implode(' › ', $breadcrumb) . '</div>';
return $output;
}
}
/**
* Set of Block Class functions
*/
/*
* supply an array with integer and string indexes
* and get an associative array divided into #string & #integer
* or if a key_type argument is supplied then get an array with only the keys that match it
*
* for use by process_tested_class()
*/
function array_key_split($array, $key_type = NULL) {
$return_array = FALSE;
$integer_keys = array();
$string_keys = array();
foreach($array as $key => $value) {
if (is_numeric($key)) { // perhaps a test for integers is slower than a numeric test
$integer_keys[] = $value;
} else {
$string_keys[$key] = $value;
}
}
if (empty($key_type)) {
$return_array = array ();
$return_array['integer'] = $integer_keys;
$return_array['string'] = $string_keys;
} else {
if ($key_type == 'integer') {
$return_array = $integer_keys;
} elseif ($key_type == 'string') {
$return_array = $string_keys;
}
}
return $return_array;
};
/**
* process_tested_class()
*
* used by: block_classes
*
* arguments:
* $variables is passed by reference from gramophone_preprocess_block
* $class is an array made up of classes
* each class may be a string (index keyed)
* or an array containing class strings and an array of tests
* the hash key #tests with it's value being and array of tests
* the key of each test determines the type of test
* the value of each test determines the pass or fail criteria
* failing any of the tests will prevent the array of class strings being added
*
*/
function process_tested_class(&$variables, $class) {
$is_front = $variables['is_front']; // is this the front page of the site
$has_nodes = (!empty($variables['elements']['nodes'])); // ? TRUE : FALSE;
//$bundle = $variables['elements']['#bundle']; // user
// if (isset($variables['elements']) && isset($variables['elements']['#bundle']))
$bundle = (isset($variables['elements']['#bundle'])) ? $variables['elements']['#bundle'] : FALSE;
$block = $variables['block'];
$block_region = $block->region;
$block_module = $block->module;
$block_delta = $block->delta;
if (isset($class['#tests'])) {
$tested_classes = array_key_split($class, 'integer');
$tests = $class['#tests'];
$pass = TRUE;
// failing any of the tests will prevent the classes being added
// each test may be a string value (with by inference an index key)
// or may be a key, with the value containing the success criteria
foreach ($tests as $key => $value) {
if (is_numeric($key)) {
$test = $value;
$criteria = TRUE;
} else {
$test = $key;
$criteria = $value;
}
// find reasons to fail, it will only take one reason to set $pass to FALSE
// if case is found, then break will prevent subsequent cases being tested
switch ($test) {
case "is_front":
if ($criteria != $is_front) {
$pass = FALSE;
}
break;
case "is_not_module":
if (!empty($block_module) && ($criteria == $block_module)) {
$pass = FALSE;
}
break;
case "is_module":
if (!empty($block_module) && ($criteria != $block_module)) {
$pass = FALSE;
}
break;
case "is_not_delta":
if (!empty($block_delta) && ($criteria == $block_delta)) {
$pass = FALSE;
}
break;
case "is_delta":
if (!empty($block_delta) && ($criteria != $block_delta)) {
$pass = FALSE;
}
break;
case "is_node":
if ($criteria != $has_nodes) {
$pass = FALSE;
}
break;
case "is_bundle":
// if $bundle does not exist, then there can be no match, and pass must fail
if (empty($bundle) || ($criteria != $bundle)) {
$pass = FALSE;
}
break;
}
}
if ($pass) {
$variables['classes_array'] = array_merge($variables['classes_array'], $tested_classes);
}
}
};
/**
* block_classes()
* used by: themename_preprocess_block
*
* each class may be:
* a string
* (or)
* an array of
* class strings
* (which may also include an optional)
* tests array
* each test may be:
* a string
* key & value pair
*
* could use array_unique to sanitise
*
* add a test for delta // array_key_split($test);
*/
function block_classes(&$variables, $classes_array) {
$block = $variables['block'];
$block_region = $block->region;
$block_module = $block->module;
$regions = $classes_array['regions'];
$inner_classes_array = array();
// check for a match with current block's region
if (!empty($block_region) && !empty($regions) && array_key_exists($block_region, $regions)) {
// region found, so extract data
$region_data = $regions[$block_region];
if (!empty($region_data)) {
// data may contain a classes array, and a modules array
// check for classes array - key exists and value exists
if (isset($region_data['classes'])) {
// assume $classes is an array - would be nice to handle a string here too
$classes = $region_data['classes'];
foreach ($classes as $class) {
if (is_string($class)) {
// add classes (if not already added)
if (!in_array($class, $variables['classes_array'])) {
$variables['classes_array'][] = $class;
}
} elseif (is_array($class)) {
// then apply tests before adding classes
process_tested_class($variables, $class);
}
}
}
// check for modules array
if (isset($region_data['modules'])) {
$modules = $region_data['modules'];
// check for a match with current block's module
if (array_key_exists($block_module, $modules)) {
// module found, so extract data
$module_data = $modules[$block_module];
if (!empty($module_data)) {
if (isset($module_data['classes'])) {
$classes = $module_data['classes'];
foreach ($classes as $class) {
if (is_string($class)) {
$variables['classes_array'][] = $class;
} elseif (is_array($class)) {
process_tested_class($variables, $class);
}
}
}
if (isset($module_data['inner_classes'])) {
// inner_classes may be a string or an array of strings
if (is_array($module_data['inner_classes'])) {
$inner_classes_array = array_merge($inner_classes_array, $module_data['inner_classes']);
} else {
$inner_classes_array[] = $module_data['inner_classes'];
}
}
}
}
}
}
}
$variables['inner_classes'] = implode(" ", $inner_classes_array);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment