seven.theme 10.5 KB
Newer Older
1 2
<?php

3
/**
4 5 6 7
 * @file
 * Functions to support theming in the Seven theme.
 */

8
use Drupal\Component\Utility\Xss;
9
use Drupal\Component\Utility\String;
10
use Drupal\Core\Form\FormStateInterface;
11

12
/**
13
 * Implements hook_preprocess_HOOK() for page templates.
14
 */
15 16 17 18 19
function seven_preprocess_page(&$variables) {
  /** @var \Drupal\Core\Page\HtmlPage $page_object */
  $page_object = $variables['page']['#page'];
  $attributes = $page_object->getBodyAttributes();
  $classes = $attributes['class'];
20
  // Add information about the number of sidebars.
21

22
  if (!empty($variables['page']['sidebar_first'])) {
23 24
    $classes[] = 'layout-one-sidebar';
    $classes[] = 'layout-sidebar-first';
25 26
  }
  else {
27
    $classes[] = 'layout-no-sidebars';
28
  }
29
  $attributes['class'] = $classes;
30

31 32 33
  $variables['primary_local_tasks'] = $variables['tabs'];
  unset($variables['primary_local_tasks']['#secondary']);
  $variables['secondary_local_tasks'] = array(
34
    '#theme' => 'menu_local_tasks',
35
    '#secondary' => isset($variables['tabs']['#secondary']) ? $variables['tabs']['#secondary'] : '',
36
  );
37 38
}

39 40 41 42
/**
 * Overrides theme_menu_local_tasks().
 *
 * Returns HTML for primary and secondary local tasks.
43
 */
44 45 46 47 48 49
function seven_menu_local_tasks(&$variables) {
  $output = '';

  if (!empty($variables['primary'])) {
    $variables['primary']['#attached'] = array(
      'library' => array(
50
        'seven/drupal.nav-tabs',
51 52 53 54 55 56 57 58 59 60 61 62 63
      ),
    );
    $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>&bull;&bull;&bull;</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(
64
        'seven/drupal.nav-tabs',
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      ),
    );
    $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.
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
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>';
}

113
/**
114
 * Implements hook_preprocess_HOOK() for list of available node type templates.
115
 */
116 117 118 119 120 121
function seven_preprocess_node_add_list(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
      $variables['types'][$type->type]['label'] = String::checkPlain($type->name);
      $variables['types'][$type->type]['description'] = Xss::filterAdmin($type->description);
      $variables['types'][$type->type]['url'] = \Drupal::url('node.add', array('node_type' => $type->type));
122 123 124 125
    }
  }
}

126
/**
127
 * Implements hook_preprocess_HOOK() for block content add list templates.
128
 *
129 130
 * Displays the list of available custom block types for creation, adding
 * separate variables for the label, description, and url.
131
 */
132 133 134 135 136 137 138
function seven_preprocess_block_content_add_list(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
      $variables['types'][$type->id()]['label'] = String::checkPlain($type->label());
      $variables['types'][$type->id()]['description'] = Xss::filterAdmin($type->description);
      $options = array('query' => \Drupal::request()->query->all());
      $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', array('block_content_type' => $type->id()), $options);
139 140 141 142
    }
  }
}

143
/**
144
 * Implements hook_preprocess_HOOK() for block admin page templates.
145
 */
146 147 148
function seven_preprocess_admin_block_content(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $key => $item) {
149
      $variables['content'][$key]['url'] = $item['url']->toString();
150 151 152 153 154
    }
  }
}

/**
155
 * Implements hook_preprocess_HOOK() for tablesort indicator templates.
156
 *
157 158
 * Uses Seven's image versions, so the arrows show up as black and not gray on
 * gray.
159
 */
160
function seven_preprocess_tablesort_indicator(&$variables) {
161
  $theme_path = drupal_get_path('theme', 'seven');
162 163
  $variables['arrow_asc'] = file_create_url($theme_path . '/images/arrow-asc.png');
  $variables['arrow_desc'] = file_create_url($theme_path . '/images/arrow-desc.png');
164
}
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/**
 * 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(
188
        'core/modernizr',
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      ),
    ),
  );
  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'])) {
215
    $type['button']['#attached']['library'][] = 'core/modernizr';
216 217 218
  }
}

219 220 221 222
/**
 * Implements hook_preprocess_install_page().
 */
function seven_preprocess_install_page(&$variables) {
223 224 225 226 227
  $page_object = $variables['page']['#page'];
  $attributes = $page_object->getHtmlAttributes();
  $classes = $attributes['class'];
  $classes[] = 'install-background';
  $attributes['class'] = $classes;
228 229 230

  // Normally we could attach libraries via hook_page_alter(), but when the
  // database is inactive it's not called so we add them here.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  $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) {
246 247 248 249 250 251
  $page_object = $variables['page']['#page'];
  $attributes = $page_object->getHtmlAttributes();
  $classes = $attributes['class'];
  $classes[] = 'maintenance-background';
  $attributes['class'] = $classes;

252 253 254 255 256 257 258 259 260
  // // 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',
      ),
    ),
  );
261
  drupal_render($libraries);
262
}
263 264 265 266 267 268

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Changes vertical tabs to container and adds meta information.
 */
269
function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
270
  /** @var \Drupal\node\NodeInterface $node */
271
  $node = $form_state->get('controller')->getEntity();
272

273
  $form['#theme'] = array('node_edit_form');
274
  $form['#attached']['css'][] = drupal_get_path('module', 'node') . '/css/node.module.css';
275

276
  $form['advanced']['#type'] = 'container';
277
  $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
278
  $form['meta'] = array(
279 280 281 282 283 284 285
    '#attributes' => array('class' => array('entity-meta-header')),
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('published')),
286
      '#markup' => $node->isPublished() ? t('Published') : t('Not published'),
287
      '#access' => !$node->isNew(),
288 289 290 291
    ),
    'changed' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('changed', 'container-inline')),
292
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
293 294 295 296
    ),
    'author' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('author', 'container-inline')),
297
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
298 299 300 301 302
    ),
  );
  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';
}