Pull merge.
[yaffs-website] / web / core / themes / bartik / bartik.theme
1 <?php
2
3 /**
4  * @file
5  * Functions to support theming in the Bartik theme.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Template\Attribute;
10
11 /**
12  * Implements hook_preprocess_HOOK() for HTML document templates.
13  *
14  * Adds body classes if certain regions have content.
15  */
16 function bartik_preprocess_html(&$variables) {
17   // Add information about the number of sidebars.
18   if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
19     $variables['attributes']['class'][] = 'layout-two-sidebars';
20   }
21   elseif (!empty($variables['page']['sidebar_first'])) {
22     $variables['attributes']['class'][] = 'layout-one-sidebar';
23     $variables['attributes']['class'][] = 'layout-sidebar-first';
24   }
25   elseif (!empty($variables['page']['sidebar_second'])) {
26     $variables['attributes']['class'][] = 'layout-one-sidebar';
27     $variables['attributes']['class'][] = 'layout-sidebar-second';
28   }
29   else {
30     $variables['attributes']['class'][] = 'layout-no-sidebars';
31   }
32
33   if (!empty($variables['page']['featured_top'])) {
34     $variables['attributes']['class'][] = 'has-featured-top';
35   }
36
37 }
38
39 /**
40  * Implements hook_preprocess_HOOK() for page title templates.
41  */
42 function bartik_preprocess_page_title(&$variables) {
43   // Since the title and the shortcut link are both block level elements,
44   // positioning them next to each other is much simpler with a wrapper div.
45   if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
46     // Add a wrapper div using the title_prefix and title_suffix render
47     // elements.
48     $variables['title_prefix']['shortcut_wrapper'] = [
49       '#markup' => '<div class="shortcut-wrapper clearfix">',
50       '#weight' => 100,
51     ];
52     $variables['title_suffix']['shortcut_wrapper'] = [
53       '#markup' => '</div>',
54       '#weight' => -99,
55     ];
56     // Make sure the shortcut link is the first item in title_suffix.
57     $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
58   }
59 }
60
61 /**
62  * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
63  */
64 function bartik_preprocess_maintenance_page(&$variables) {
65   // By default, site_name is set to Drupal if no db connection is available
66   // or during site installation. Setting site_name to an empty string makes
67   // the site and update pages look cleaner.
68   // @see template_preprocess_maintenance_page
69   if (!$variables['db_is_active']) {
70     $variables['site_name'] = '';
71   }
72
73   // Bartik has custom styling for the maintenance page.
74   $variables['#attached']['library'][] = 'bartik/maintenance_page';
75 }
76
77 /**
78  * Implements hook_preprocess_HOOK() for node.html.twig.
79  */
80 function bartik_preprocess_node(&$variables) {
81   // Remove the "Add new comment" link on teasers or when the comment form is
82   // displayed on the page.
83   if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
84     unset($variables['content']['links']['comment']['#links']['comment-add']);
85   }
86 }
87
88 /**
89  * Implements hook_preprocess_HOOK() for block.html.twig.
90  */
91 function bartik_preprocess_block(&$variables) {
92   // Add a clearfix class to system branding blocks.
93   if ($variables['plugin_id'] == 'system_branding_block') {
94     $variables['attributes']['class'][] = 'clearfix';
95   }
96 }
97
98 /**
99  * Implements hook_preprocess_HOOK() for menu.html.twig.
100  */
101 function bartik_preprocess_menu(&$variables) {
102   $variables['attributes']['class'][] = 'clearfix';
103 }
104
105 /**
106  * Implements hook_theme_suggestions_HOOK_alter() for form templates.
107  */
108 function bartik_theme_suggestions_form_alter(array &$suggestions, array $variables) {
109   if ($variables['element']['#form_id'] == 'search_block_form') {
110     $suggestions[] = 'form__search_block_form';
111   }
112 }
113
114 /**
115  * Implements hook_form_alter() to add classes to the search form.
116  */
117 function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) {
118   if (in_array($form_id, ['search_block_form', 'search_form'])) {
119     $key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
120     if (!isset($form[$key]['submit']['#attributes'])) {
121       $form[$key]['submit']['#attributes'] = new Attribute();
122     }
123     $form[$key]['submit']['#attributes']->addClass('search-form__submit');
124   }
125 }