bartik.theme 6.32 KB
Newer Older
1 2 3
<?php

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

8
use Drupal\Component\Utility\String;
9
use Drupal\Component\Utility\Xss;
10
use Drupal\Core\Template\RenderWrapper;
11
use Drupal\Core\Template\Attribute;
12

13
/**
14
 * Implements hook_preprocess_HOOK() for page.html.twig.
15 16
 *
 * Adds body classes if certain regions have content.
17
 */
18
function bartik_preprocess_page(&$variables) {
19
  // Add information about the number of sidebars.
20 21 22 23
  /** @var \Drupal\Core\Page\HtmlPage $page_object */
  $page_object = $variables['page']['#page'];
  $attributes = $page_object->getBodyAttributes();
  $classes = $attributes['class'];
24
  if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
25
    $classes[] = 'layout-two-sidebars';
26 27
  }
  elseif (!empty($variables['page']['sidebar_first'])) {
28 29
    $classes[] = 'layout-one-sidebar';
    $classes[] = 'layout-sidebar-first';
30 31
  }
  elseif (!empty($variables['page']['sidebar_second'])) {
32 33
    $classes[] = 'layout-one-sidebar';
    $classes[] = 'layout-sidebar-second';
34 35
  }
  else {
36
    $classes[] = 'layout-no-sidebars';
37 38
  }

39
  if (!empty($variables['page']['featured'])) {
40
    $classes[] = 'featured';
41 42 43 44 45
  }

  if (!empty($variables['page']['triptych_first'])
    || !empty($variables['page']['triptych_middle'])
    || !empty($variables['page']['triptych_last'])) {
46
    $classes[] = 'triptych';
47 48 49 50 51 52
  }

  if (!empty($variables['page']['footer_firstcolumn'])
    || !empty($variables['page']['footer_secondcolumn'])
    || !empty($variables['page']['footer_thirdcolumn'])
    || !empty($variables['page']['footer_fourthcolumn'])) {
53
    $classes[] = 'footer-columns';
54 55
  }

56 57 58
  // Store back the classes to the htmlpage object.
  $attributes['class'] = $classes;

59 60 61 62 63 64 65
  // Pass the main menu and secondary menu to the template as render arrays.
  if (!empty($variables['main_menu'])) {
    $variables['main_menu']['#attributes']['id'] = 'main-menu-links';
    $variables['main_menu']['#attributes']['class'] = array('links', 'clearfix');
  }
  if (!empty($variables['secondary_menu'])) {
    $variables['secondary_menu']['#attributes']['id'] = 'secondary-menu-links';
66 67 68 69 70
    $variables['secondary_menu']['#attributes']['class'] = array(
      'links',
      'inline',
      'clearfix',
    );
71 72
  }

73 74 75
  // Set the options that apply to both page and maintenance page.
  _bartik_process_page($variables);

76 77 78
  // Since the title and the shortcut link are both block level elements,
  // positioning them next to each other is much simpler with a wrapper div.
  if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
79 80
    // Add a wrapper div using the title_prefix and title_suffix render
    // elements.
81 82 83 84 85 86 87 88 89 90 91 92 93
    $variables['title_prefix']['shortcut_wrapper'] = array(
      '#markup' => '<div class="shortcut-wrapper clearfix">',
      '#weight' => 100,
    );
    $variables['title_suffix']['shortcut_wrapper'] = array(
      '#markup' => '</div>',
      '#weight' => -99,
    );
    // Make sure the shortcut link is the first item in title_suffix.
    $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
  }
}

94
/**
95
 * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
96 97
 */
function bartik_preprocess_maintenance_page(&$variables) {
98 99 100 101
  // By default, site_name is set to Drupal if no db connection is available
  // or during site installation. Setting site_name to an empty string makes
  // the site and update pages look cleaner.
  // @see template_preprocess_maintenance_page
102
  if (!$variables['db_is_active']) {
103
    $variables['site_name'] = '';
104
  }
105 106 107 108 109
  // 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(
110
        'bartik/maintenance_page',
111 112 113
      ),
    ),
  );
114
  drupal_render($libraries);
115

116 117
  // Set the options that apply to both page and maintenance page.
  _bartik_process_page($variables);
118 119
}

120
/**
121
 * Implements hook_preprocess_HOOK() for node.html.twig.
122 123 124 125 126 127 128 129 130
 */
function bartik_preprocess_node(&$variables) {
  // Remove the "Add new comment" link on teasers or when the comment form is
  // displayed on the page.
  if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
    unset($variables['content']['links']['comment']['#links']['comment-add']);
  }
}

131
/**
132
 * Implements hook_preprocess_HOOK() for block.html.twig.
133 134 135 136 137 138 139 140
 */
function bartik_preprocess_block(&$variables) {
  // Add a clearfix class to system branding blocks.
  if ($variables['plugin_id'] == 'system_branding_block') {
    $variables['attributes']['class'][] = 'clearfix';
  }
}

141
/**
142
 * Implements THEME_menu_tree().
143 144 145 146
 */
function bartik_menu_tree($variables) {
  return '<ul class="menu clearfix">' . $variables['tree'] . '</ul>';
}
147

148
/**
149
 * Implements THEME_menu_tree__MENUNAME().
150 151 152 153 154
 */
function bartik_menu_tree__shortcut_default($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}

155
/**
156
 * Implements hook_preprocess_HOOK() for field.html.twig.
157 158
 *
 * @see template_preprocess_field()
159
 */
160 161 162 163 164 165 166 167 168 169 170
function bartik_preprocess_field(&$variables) {
  $element = $variables['element'];
  if ($element['#field_type'] == 'taxonomy_term_reference') {
    $label_attributes['class'] = array('field-label');
    if ($variables['label_hidden']) {
      $label_attributes['class'][] = 'visually-hidden';
    }
    if ($variables['element']['#label_display'] == 'inline') {
      $label_attributes['class'][] = 'inline';
    }
    $variables['label_attributes'] = new Attribute($label_attributes);
171 172
  }
}
173 174

/**
175 176
 * Helper function for handling the site name and slogan.
 */
177 178 179 180 181 182 183 184
function _bartik_process_page(&$variables) {
  $site_config = \Drupal::config('system.site');
  // Always print the site name and slogan, but if they are toggled off, we'll
  // just hide them visually.
  $variables['hide_site_name']   = theme_get_setting('features.name') ? FALSE : TRUE;
  $variables['hide_site_slogan'] = theme_get_setting('features.slogan') ? FALSE : TRUE;
  if ($variables['hide_site_name']) {
    // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
185
    $variables['site_name'] = String::checkPlain($site_config->get('name'));
186 187
  }
  if ($variables['hide_site_slogan']) {
188 189
    // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we
    // rebuild it.
190
    $variables['site_slogan'] = Xss::filterAdmin($site_config->get('slogan'));
191 192
  }
}