Newer
Older
<?php
/**
* Contains theme override functions and preprocess functions for the Zen theme.
* IMPORTANT WARNING: DO NOT MODIFY THIS FILE.
*
* The base Zen theme is designed to be easily extended by its sub-themes. You
* shouldn't modify this or any of the CSS or PHP files in the root zen/ folder.
* See the online documentation for more information:
* http://drupal.org/node/193318
Ted Serbinski
committed
*/
John Albin Wilkins
committed
// Auto-rebuild the theme registry during theme development.
John Albin Wilkins
committed
if (theme_get_setting('zen_rebuild_registry') && !defined('MAINTENANCE_MODE')) {
// Rebuild .info data.
John Albin Wilkins
committed
system_rebuild_theme_data();
// Rebuild theme registry.
drupal_theme_rebuild();
John Albin Wilkins
committed
}
John Albin Wilkins
committed
/**
* Implements HOOK_theme().
John Albin Wilkins
committed
function zen_theme(&$existing, $type, $theme, $path) {
John Albin Wilkins
committed
include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.theme-registry.inc';
John Albin Wilkins
committed
return _zen_theme($existing, $type, $theme, $path);
}
/**
* Return a themed breadcrumb trail.
*
* @param $variables
* - title: An optional string to be used as a navigational heading to give
* context for breadcrumb links to screen-reader users.
* - title_attributes_array: Array of HTML attributes for the title. It is
* flattened into a string within the theme function.
* - breadcrumb: An array containing the breadcrumb links.
* @return
* A string containing the breadcrumb output.
*/
John Albin Wilkins
committed
function zen_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
John Albin Wilkins
committed
// Determine if we are to display the breadcrumb.
$show_breadcrumb = theme_get_setting('zen_breadcrumb');
if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
John Albin Wilkins
committed
// Optionally get rid of the homepage link.
$show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
if (!$show_breadcrumb_home) {
array_shift($breadcrumb);
}
John Albin Wilkins
committed
// Return the breadcrumb with separators.
$breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
John Albin Wilkins
committed
$trailing_separator = $title = '';
if (theme_get_setting('zen_breadcrumb_title')) {
John Albin Wilkins
committed
$item = menu_get_item();
if (!empty($item['tab_parent'])) {
// If we are on a non-default tab, use the tab's title.
$breadcrumb[] = check_plain($item['title']);
John Albin Wilkins
committed
}
else {
$breadcrumb[] = drupal_get_title();
John Albin Wilkins
committed
}
John Albin Wilkins
committed
}
elseif (theme_get_setting('zen_breadcrumb_trailing')) {
$trailing_separator = $breadcrumb_separator;
}
John Albin Wilkins
committed
// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users.
if (empty($variables['title'])) {
$variables['title'] = t('You are here');
}
// Unless overridden by a preprocess function, make the heading invisible.
if (!isset($variables['title_attributes_array']['class'])) {
$variables['title_attributes_array']['class'][] = 'element-invisible';
}
John Albin Wilkins
committed
// Build the breadcrumb trail.
$output = '<nav class="breadcrumb" role="navigation">';
$output .= '<h2' . drupal_attributes($variables['title_attributes_array']) . '>' . $variables['title'] . '</h2>';
maartenverbaarschot
committed
$output .= '<ol><li>' . implode($breadcrumb_separator . '</li><li>', $breadcrumb) . $trailing_separator . '</li></ol>';
$output .= '</nav>';
return $output;
* Override or insert variables into the html template.
John Albin Wilkins
committed
* @param $variables
* An array of variables to pass to the theme template.
* The name of the template being rendered. This is usually "html", but can
* also be "maintenance_page" since zen_preprocess_maintenance_page() calls
* this function to have consistent variables.
John Albin Wilkins
committed
function zen_preprocess_html(&$variables, $hook) {
John Albin Wilkins
committed
// Add variables and paths needed for HTML5 and responsive support.
$variables['base_path'] = base_path();
$variables['path_to_zen'] = drupal_get_path('theme', 'zen');
John Albin Wilkins
committed
$html5_respond_meta = theme_get_setting('zen_html5_respond_meta');
$variables['add_respond_js'] = in_array('respond', $html5_respond_meta);
$variables['add_html5_shim'] = in_array('html5', $html5_respond_meta);
$variables['add_responsive_meta'] = in_array('meta', $html5_respond_meta);
// If the user is silly and enables Zen as the theme, add some styles.
if ($GLOBALS['theme'] == 'zen') {
include_once './' . $variables['path_to_zen'] . '/zen-internals/template.zen.inc';
John Albin Wilkins
committed
_zen_preprocess_html($variables, $hook);
John Albin Wilkins
committed
// Attributes for html element.
$variables['html_attributes_array'] = array(
'lang' => $variables['language']->language,
'dir' => $variables['language']->dir,
);
// Classes for body element. Allows advanced theming based on context
// (home page, node of certain type, etc.)
if (!$variables['is_front'] && $hook == 'html') {
// Add unique class for each page.
$path = drupal_get_path_alias($_GET['q']);
// Add unique class for each website section.
John Albin Wilkins
committed
$arg = explode('/', $_GET['q']);
if ($arg[0] == 'node' && isset($arg[1])) {
if ($arg[1] == 'add') {
elseif (isset($arg[2]) && is_numeric($arg[1]) && ($arg[2] == 'edit' || $arg[2] == 'delete')) {
John Albin Wilkins
committed
$section = 'node-' . $arg[2];
John Albin Wilkins
committed
$variables['classes_array'][] = drupal_html_class('section-' . $section);
Ted Serbinski
committed
}
if (theme_get_setting('zen_wireframes')) {
John Albin Wilkins
committed
$variables['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
// Store the menu item since it has some useful information.
if ($hook == 'html') {
$variables['menu_item'] = menu_get_item();
if ($variables['menu_item']) {
switch ($variables['menu_item']['page_callback']) {
case 'views_page':
// Is this a Views page?
$variables['classes_array'][] = 'page-views';
break;
case 'page_manager_page_execute':
case 'page_manager_node_view':
case 'page_manager_contact_site':
// Is this a Panels page?
$variables['classes_array'][] = 'page-panels';
break;
}
}
John Albin Wilkins
committed
$variables['skip_link_anchor'] = theme_get_setting('zen_skip_link_anchor');
$variables['skip_link_text'] = theme_get_setting('zen_skip_link_text');
John Albin Wilkins
committed
/**
* Override or insert variables into the html templates.
*
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("html" in this case.)
*/
function zen_process_html(&$variables, $hook) {
// Flatten out html_attributes.
$variables['html_attributes'] = drupal_attributes($variables['html_attributes_array']);
}
/**
* Override or insert variables in the html_tag theme function.
*/
function zen_process_html_tag(&$variables) {
$tag = &$variables['element'];
if ($tag['#tag'] == 'style' || $tag['#tag'] == 'script') {
// Remove redundant type attribute and CDATA comments.
unset($tag['#attributes']['type'], $tag['#value_prefix'], $tag['#value_suffix']);
// Remove media="all" but leave others unaffected.
if (isset($tag['#attributes']['media']) && $tag['#attributes']['media'] === 'all') {
unset($tag['#attributes']['media']);
}
}
}
/**
* Implement hook_html_head_alter().
*/
function zen_html_head_alter(&$head) {
// Simplify the meta tag for character encoding.
$head['system_meta_content_type']['#attributes'] = array('charset' => str_replace('text/html; charset=', '', $head['system_meta_content_type']['#attributes']['content']));
}
John Albin Wilkins
committed
/**
* Override or insert variables into the page template.
*
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("page" in this case.)
*/
function zen_preprocess_page(&$variables, $hook) {
// Find the title of the menu used by the secondary links.
$secondary_links = variable_get('menu_secondary_links_source', 'user-menu');
John Albin Wilkins
committed
if ($secondary_links) {
$menus = function_exists('menu_get_menus') ? menu_get_menus() : menu_list_system_menus();
$variables['secondary_menu_heading'] = $menus[$secondary_links];
}
else {
$variables['secondary_menu_heading'] = '';
}
John Albin Wilkins
committed
}
John Albin Wilkins
committed
/**
* Override or insert variables into the maintenance page template.
*
John Albin Wilkins
committed
* @param $variables
John Albin Wilkins
committed
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("maintenance_page" in this case.)
John Albin Wilkins
committed
*/
John Albin Wilkins
committed
function zen_preprocess_maintenance_page(&$variables, $hook) {
zen_preprocess_html($variables, $hook);
John Albin Wilkins
committed
}
/**
* Override or insert variables into the maintenance page template.
*
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("maintenance_page" in this case.)
*/
function zen_process_maintenance_page(&$variables, $hook) {
zen_process_html($variables, $hook);
John Albin Wilkins
committed
// Ensure default regions get a variable. Theme authors often forget to remove
// a deleted region's variable in maintenance-page.tpl.
foreach (array('header', 'navigation', 'highlighted', 'help', 'content', 'sidebar_first', 'sidebar_second', 'footer', 'bottom') as $region) {
if (!isset($variables[$region])) {
$variables[$region] = '';
}
}
John Albin Wilkins
committed
}
* Override or insert variables into the node templates.
John Albin Wilkins
committed
* @param $variables
* An array of variables to pass to the theme template.
* The name of the template being rendered ("node" in this case.)
John Albin Wilkins
committed
function zen_preprocess_node(&$variables, $hook) {
John Albin Wilkins
committed
// Add $unpublished variable.
John Albin Wilkins
committed
$variables['unpublished'] = (!$variables['status']) ? TRUE : FALSE;
John Albin Wilkins
committed
John Albin Wilkins
committed
// Add pubdate to submitted variable.
$variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['node']->created, 'custom', 'c') . '">' . $variables['date'] . '</time>';
if ($variables['display_submitted']) {
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['pubdate']));
}
// Add a class for the view mode.
if (!$variables['teaser']) {
$variables['classes_array'][] = 'view-mode-' . $variables['view_mode'];
}
John Albin Wilkins
committed
// Add a class to show node is authored by current user.
John Albin Wilkins
committed
if ($variables['uid'] && $variables['uid'] == $GLOBALS['user']->uid) {
$variables['classes_array'][] = 'node-by-viewer';
John Albin Wilkins
committed
$variables['title_attributes_array']['class'][] = 'node-title';
* Override or insert variables into the comment templates.
John Albin Wilkins
committed
* @param $variables
* An array of variables to pass to the theme template.
* The name of the template being rendered ("comment" in this case.)
John Albin Wilkins
committed
function zen_preprocess_comment(&$variables, $hook) {
// If comment subjects are disabled, don't display them.
John Albin Wilkins
committed
if (variable_get('comment_subject_field_' . $variables['node']->type, 1) == 0) {
$variables['title'] = '';
John Albin Wilkins
committed
// Add pubdate to submitted variable.
$variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['comment']->created, 'custom', 'c') . '">' . $variables['created'] . '</time>';
$variables['submitted'] = t('!username replied on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['pubdate']));
John Albin Wilkins
committed
if ($variables['id'] == 1) {
$variables['classes_array'][] = 'first';
John Albin Wilkins
committed
if ($variables['id'] == $variables['node']->comment_count) {
$variables['classes_array'][] = 'last';
John Albin Wilkins
committed
$variables['classes_array'][] = $variables['zebra'];
John Albin Wilkins
committed
$variables['title_attributes_array']['class'][] = 'comment-title';
Ted Serbinski
committed
}
Jeff Robbins
committed
/**
* Preprocess variables for region.tpl.php
*
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("region" in this case.)
John Albin Wilkins
committed
function zen_preprocess_region(&$variables, $hook) {
John Albin Wilkins
committed
// Sidebar regions get some extra classes and a common template suggestion.
John Albin Wilkins
committed
if (strpos($variables['region'], 'sidebar_') === 0) {
$variables['classes_array'][] = 'column';
$variables['classes_array'][] = 'sidebar';
John Albin Wilkins
committed
// Allow a region-specific template to override Zen's region--sidebar.
John Albin Wilkins
committed
array_unshift($variables['theme_hook_suggestions'], 'region__sidebar');
John Albin Wilkins
committed
// Use a template with no wrapper for the content region.
John Albin Wilkins
committed
elseif ($variables['region'] == 'content') {
John Albin Wilkins
committed
// Allow a region-specific template to override Zen's region--no-wrapper.
array_unshift($variables['theme_hook_suggestions'], 'region__no_wrapper');
John Albin Wilkins
committed
}
* Override or insert variables into the block templates.
John Albin Wilkins
committed
* @param $variables
* An array of variables to pass to the theme template.
* The name of the template being rendered ("block" in this case.)
John Albin Wilkins
committed
function zen_preprocess_block(&$variables, $hook) {
John Albin Wilkins
committed
// Use a template with no wrapper for the page's main content.
John Albin Wilkins
committed
if ($variables['block_html_id'] == 'block-system-main') {
John Albin Wilkins
committed
$variables['theme_hook_suggestions'][] = 'block__no_wrapper';
John Albin Wilkins
committed
}
// Classes describing the position of the block within the region.
John Albin Wilkins
committed
if ($variables['block_id'] == 1) {
$variables['classes_array'][] = 'first';
}
// The last_in_region property is set in zen_page_alter().
John Albin Wilkins
committed
if (isset($variables['block']->last_in_region)) {
$variables['classes_array'][] = 'last';
}
John Albin Wilkins
committed
$variables['classes_array'][] = $variables['block_zebra'];
John Albin Wilkins
committed
$variables['title_attributes_array']['class'][] = 'block-title';
John Albin Wilkins
committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Add Aria Roles via attributes.
switch ($variables['block']->module) {
case 'system':
switch ($variables['block']->delta) {
case 'main':
// Note: the "main" role goes in the page.tpl, not here.
break;
case 'help':
case 'powered-by':
$variables['attributes_array']['role'] = 'complementary';
break;
default:
// Any other "system" block is a menu block.
$variables['attributes_array']['role'] = 'navigation';
break;
}
break;
case 'menu':
case 'menu_block':
case 'blog':
case 'book':
case 'comment':
case 'forum':
case 'shortcut':
case 'statistics':
$variables['attributes_array']['role'] = 'navigation';
break;
case 'search':
$variables['attributes_array']['role'] = 'search';
break;
case 'help':
case 'aggregator':
case 'locale':
case 'poll':
case 'profile':
$variables['attributes_array']['role'] = 'complementary';
break;
case 'node':
switch ($variables['block']->delta) {
case 'syndicate':
$variables['attributes_array']['role'] = 'complementary';
break;
case 'recent':
$variables['attributes_array']['role'] = 'navigation';
break;
}
break;
case 'user':
switch ($variables['block']->delta) {
case 'login':
$variables['attributes_array']['role'] = 'form';
break;
case 'new':
case 'online':
$variables['attributes_array']['role'] = 'complementary';
break;
}
break;
}
John Albin Wilkins
committed
}
/**
* Override or insert variables into the block templates.
*
John Albin Wilkins
committed
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("block" in this case.)
*/
John Albin Wilkins
committed
function zen_process_block(&$variables, $hook) {
// Drupal 7 should use a $title variable instead of $block->subject.
John Albin Wilkins
committed
$variables['title'] = $variables['block']->subject;
/**
* Implements hook_page_alter().
*
* Look for the last block in the region. This is impossible to determine from
* within a preprocess_block function.
*
* @param $page
* Nested array of renderable elements that make up the page.
*/
function zen_page_alter(&$page) {
// Look in each visible region for blocks.
foreach (system_region_list($GLOBALS['theme'], REGIONS_VISIBLE) as $region => $name) {
if (!empty($page[$region])) {
// Find the last block in the region.
$blocks = array_reverse(element_children($page[$region]));
while ($blocks && !isset($page[$region][$blocks[0]]['#block'])) {
array_shift($blocks);
}
if ($blocks) {
$page[$region][$blocks[0]]['#block']->last_in_region = TRUE;
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Prevent user-facing field styling from screwing up node edit forms by
* renaming the classes on the node edit form's field wrappers.
*/
function zen_form_node_form_alter(&$form, &$form_state, $form_id) {
// Remove if #1245218 is backported to D7 core.
foreach (array_keys($form) as $item) {
if (strpos($item, 'field_') === 0) {
if (!empty($form[$item]['#attributes']['class'])) {
foreach ($form[$item]['#attributes']['class'] as &$class) {
if (strpos($class, 'field-type-') === 0 || strpos($class, 'field-name-') === 0) {
// Make the class different from that used in theme_field().
$class = 'form-' . $class;
}
}
}
}
}
}