Newer
Older
Dries Buytaert
committed
<?php
Dries Buytaert
committed
/**
Jennifer Hodgdon
committed
* @file
* Functions to support theming in the Seven theme.
*/
Angie Byron
committed
use Drupal\Component\Utility\Xss;
Angie Byron
committed
use Drupal\Core\Template\RenderWrapper;
Angie Byron
committed
use Drupal\Component\Utility\String;
/**
* Implements hook_preprocess_HOOK() for page templates.
*/
function seven_preprocess_page(&$variables) {
/** @var \Drupal\Core\Page\HtmlPage $page_object */
$page_object = $variables['page']['#page'];
$attributes = $page_object->getBodyAttributes();
$classes = $attributes['class'];
// Add information about the number of sidebars.
if (!empty($variables['page']['sidebar_first'])) {
$classes[] = 'one-sidebar';
$classes[] = 'sidebar-first';
}
else {
$classes[] = 'no-sidebars';
}
$attributes['class'] = $classes;
catch
committed
$variables['primary_local_tasks'] = $variables['tabs'];
unset($variables['primary_local_tasks']['#secondary']);
$variables['secondary_local_tasks'] = array(
'#theme' => 'menu_local_tasks',
catch
committed
'#secondary' => isset($variables['tabs']['#secondary']) ? $variables['tabs']['#secondary'] : '',
);
Dries Buytaert
committed
}
Angie Byron
committed
/**
* Overrides theme_menu_local_tasks().
*
* Returns HTML for primary and secondary local tasks.
*
**/
function seven_menu_local_tasks(&$variables) {
$output = '';
if (!empty($variables['primary'])) {
$variables['primary']['#attached'] = array(
'library' => array(
'seven/drupal.nav-tabs',
Angie Byron
committed
),
);
$variables['primary']['#prefix'] = '<h2 id="primary-tabs-title" class="visually-hidden">' . t('Primary tabs') . '</h2>';
$variables['primary']['#prefix'] .= '<nav role="navigation" class="is-horizontal is-collapsible" aria-labelledby="primary-tabs-title" data-drupal-nav-tabs>';
$variables['primary']['#prefix'] .= '<button class="reset-appearance tabs__tab tabs__trigger" aria-label="Primary tabs display toggle" data-drupal-nav-tabs-trigger>•••</button>';
$variables['primary']['#prefix'] .= '<ul class="tabs primary clearfix" data-drupal-nav-tabs-target>';
$variables['primary']['#suffix'] = '</ul>';
$variables['primary']['#suffix'] .= '</nav>';
$output .= drupal_render($variables['primary']);
}
if (!empty($variables['secondary'])) {
$variables['secondary']['#attached'] = array(
'library' => array(
'seven/drupal.nav-tabs',
Angie Byron
committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
),
);
$variables['secondary']['#prefix'] = '<h2 id="secondary-tabs-title" class="visually-hidden">' . t('Secondary tabs') . '</h2>';
$variables['secondary']['#prefix'] .= '<nav role="navigation" class="is-horizontal" aria-labelledby="secondary-tabs-title" data-drupal-nav-tabs>';
$variables['secondary']['#prefix'] .= '<ul class="tabs secondary clearfix">';
$variables['secondary']['#suffix'] = '</ul>';
$variables['secondary']['#suffix'] .= '</nav>';
$output .= drupal_render($variables['secondary']);
}
return $output;
}
/**
* Overrides theme_menu_local_task().
*
* Returns HTML for a local task.
*
**/
function seven_menu_local_task($variables) {
$link = $variables['element']['#link'];
$link += array(
'localized_options' => array(),
);
$link_text = $link['title'];
if (!empty($variables['element']['#active'])) {
// Add text to indicate active tab for non-visual users.
$active = '<span class="visually-hidden">' . t('(active tab)') . '</span>';
// If the link does not contain HTML already, String::checkPlain() it now.
// After we set 'html'=TRUE the link will not be sanitized by l().
if (empty($link['localized_options']['html'])) {
$link['title'] = String::checkPlain($link['title']);
}
$link['localized_options']['html'] = TRUE;
$link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));
}
if (!empty($link['href'])) {
// @todo - remove this once all pages are converted to routes.
$a_tag = l($link_text, $link['href'], $link['localized_options']);
}
else {
$a_tag = \Drupal::l($link_text, $link['route_name'], $link['route_parameters'], $link['localized_options']);
}
return '<li' . (!empty($variables['element']['#active']) ? ' class="tabs__tab active"' : ' class="tabs__tab"') . '>' . $a_tag . '</li>';
}
Dries Buytaert
committed
/**
Jennifer Hodgdon
committed
* Displays the list of available node types for node creation.
Dries Buytaert
committed
*/
Dries Buytaert
committed
function seven_node_add_list($variables) {
$content = $variables['content'];
Dries Buytaert
committed
if ($content) {
Dries Buytaert
committed
$output = '<ul class="admin-list">';
foreach ($content as $type) {
Dries Buytaert
committed
$output .= '<li class="clearfix">';
$content = '<span class="label">' . check_plain($type->name) . '</span>';
Angie Byron
committed
$content .= '<div class="description">' . Xss::filterAdmin($type->description) . '</div>';
$options['html'] = TRUE;
$output .= l($content, 'node/add/' . $type->type, $options);
Dries Buytaert
committed
$output .= '</li>';
}
$output .= '</ul>';
}
Dries Buytaert
committed
else {
$output = '<p>' . t('You have not created any content types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/types/add'))) . '</p>';
}
Dries Buytaert
committed
return $output;
}
Dries Buytaert
committed
/**
* Overrides theme_custom_block_add_list().
*
* Displays the list of available custom block types for creation.
*/
function seven_custom_block_add_list($variables) {
$output = '';
if (!empty($variables['types'])) {
Dries Buytaert
committed
$output = '<ul class="admin-list">';
foreach ($variables['types'] as $id => $type) {
Dries Buytaert
committed
$output .= '<li class="clearfix">';
$content = '<span class="label">' . check_plain($type['title']) . '</span>';
Angie Byron
committed
$content .= '<div class="description">' . Xss::filterAdmin($type['description']) . '</div>';
$options = $type['localized_options'];
Dries Buytaert
committed
$options['html'] = TRUE;
Alex Pott
committed
$output .= \Drupal::l($content, 'custom_block.add_form', array('custom_block_type' => $id), $options);
Dries Buytaert
committed
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
Dries Buytaert
committed
/**
* Overrides theme_admin_block_content().
Dries Buytaert
committed
*
Jennifer Hodgdon
committed
* Uses an unordered list markup in both compact and extended mode.
Dries Buytaert
committed
*/
Dries Buytaert
committed
function seven_admin_block_content($variables) {
$content = $variables['content'];
Dries Buytaert
committed
$output = '';
if (!empty($content)) {
$output = system_admin_compact_mode() ? '<ul class="admin-list compact">' : '<ul class="admin-list">';
foreach ($content as $item) {
$output .= '<li>';
Angie Byron
committed
$content = '<span class="label">' . Xss::filterAdmin($item['title']) . '</span>';
$options = $item['localized_options'];
$options['html'] = TRUE;
if (isset($item['description']) && !system_admin_compact_mode()) {
Angie Byron
committed
$content .= '<div class="description">' . Xss::filterAdmin($item['description']) . '</div>';
Dries Buytaert
committed
}
$output .= l($content, $item['link_path'], $options);
Dries Buytaert
committed
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
/**
Jennifer Hodgdon
committed
* Overrides theme_tablesort_indicator().
Dries Buytaert
committed
*
Jennifer Hodgdon
committed
* Uses Seven's image versions, so the arrows show up as black and not gray on
* gray.
Dries Buytaert
committed
*/
Dries Buytaert
committed
function seven_tablesort_indicator($variables) {
Dries Buytaert
committed
$theme_path = drupal_get_path('theme', 'seven');
Alex Pott
committed
Angie Byron
committed
if ($variables['style'] == 'asc') {
Alex Pott
committed
$image_uri = $theme_path . '/images/arrow-asc.png';
Alex Pott
committed
$text = t('Sort ascending');
Dries Buytaert
committed
}
else {
Alex Pott
committed
$image_uri = $theme_path . '/images/arrow-desc.png';
Alex Pott
committed
$text = t('Sort descending');
Dries Buytaert
committed
}
Alex Pott
committed
$image = array(
'#theme' => 'image',
'#uri' => $image_uri,
'#alt' => $text,
Alex Pott
committed
'#width' => 9,
'#height' => 5,
Alex Pott
committed
'#title' => $text,
);
return drupal_render($image);
Dries Buytaert
committed
}
Dries Buytaert
committed
Angie Byron
committed
/**
* Overrides theme_menu_local_action().
*/
function seven_menu_local_action($variables) {
$link = $variables['element']['#link'];
$link += array(
'href' => '',
'localized_options' => array(),
'route_parameters' => array(),
);
$link['localized_options']['attributes']['class'][] = 'button';
$link['localized_options']['attributes']['class'][] = 'button--primary';
$link['localized_options']['attributes']['class'][] = 'button--small';
// @todo Replace with a generalized solution for icons.
// See http://drupal.org/node/1849712
$link['localized_options']['attributes']['class'][] = 'button-action';
// We require Modernizr's touch test for button styling.
$libraries = array(
'#attached' => array(
'library' => array(
'core/modernizr',
Angie Byron
committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
),
),
);
drupal_render($libraries);
$output = '<li>';
// @todo Remove this check and the call to l() when all pages are converted to
// routes.
// @todo Figure out how to support local actions without a href properly.
if ($link['href'] === '' && !empty($link['route_name'])) {
$output .= Drupal::l($link['title'], $link['route_name'], $link['route_parameters'], $link['localized_options']);
}
else {
$output .= l($link['title'], $link['href'], $link['localized_options']);
}
$output .= "</li>";
return $output;
}
/**
* Implements hook_element_info_alter().
*/
function seven_element_info_alter(&$type) {
// We require Modernizr for button styling.
if (isset($type['button'])) {
$type['button']['#attached']['library'][] = 'core/modernizr';
Angie Byron
committed
}
}
Angie Byron
committed
/**
* Implements hook_preprocess_install_page().
*/
function seven_preprocess_install_page(&$variables) {
Angie Byron
committed
$variables['styles'] = new RenderWrapper('drupal_get_css');
$variables['scripts'] = new RenderWrapper('drupal_get_js');
// Normally we could attach libraries via hook_page_alter(), but when the
// database is inactive it's not called so we add them here.
$libraries = array(
'#attached' => array(
'library' => array(
'seven/maintenance-page',
'seven/install-page',
),
),
);
drupal_render($libraries);
}
/**
* Implements hook_preprocess_maintenance_page().
*/
function seven_preprocess_maintenance_page(&$variables) {
Angie Byron
committed
$variables['styles'] = new RenderWrapper('drupal_get_css');
$variables['scripts'] = new RenderWrapper('drupal_get_js');
// // Normally we could attach libraries via hook_page_alter(), but when the
// // database is inactive it's not called so we add them here.
$libraries = array(
'#attached' => array(
'library' => array(
'seven/maintenance-page',
),
),
);
drupal_render($libraries);
Angie Byron
committed
}
Angie Byron
committed
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Changes vertical tabs to container and adds meta information.
*/
function seven_form_node_form_alter(&$form, &$form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state['controller']->getEntity();
Angie Byron
committed
Dries Buytaert
committed
$form['#theme'] = array('node_edit_form');
$form['#attached'] = array(
Alex Pott
committed
'css' => array(drupal_get_path('module', 'node') . '/css/node.module.css'),
Dries Buytaert
committed
);
Angie Byron
committed
$form['advanced']['#type'] = 'container';
$form['meta'] = array (
'#attributes' => array('class' => array('entity-meta-header')),
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -100,
'published' => array(
'#type' => 'item',
'#wrapper_attributes' => array('class' => array('published')),
'#markup' => $node->isPublished() ? t('Published') : t('Not published'),
'#access' => !$node->isNew(),
Angie Byron
committed
),
'changed' => array(
'#type' => 'item',
'#wrapper_attributes' => array('class' => array('changed', 'container-inline')),
'#title' => t('Last saved'),
'#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet'),
Angie Byron
committed
),
'author' => array(
'#type' => 'item',
'#wrapper_attributes' => array('class' => array('author', 'container-inline')),
'#title' => t('Author'),
'#markup' => $node->getOwner()->getUsername(),
Angie Byron
committed
),
);
$form['revision_information']['#type'] = 'container';
$form['revision_information']['#group'] = 'meta';
}