2ad947c80b86694b4ab7b34ba81f14523ae41864
[yaffs-website] / web / core / themes / seven / seven.theme
1 <?php
2
3 /**
4  * @file
5  * Functions to support theming in the Seven theme.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Implements hook_preprocess_HOOK() for HTML document templates.
12  */
13 function seven_preprocess_html(&$variables) {
14   // If on a node add or edit page, add a node-layout class.
15   $path_args = explode('/', \Drupal::request()->getPathInfo());
16   if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
17     foreach ($suggestions as $suggestion) {
18       if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
19         $variables['attributes']['class'][] = 'node-form-layout';
20       }
21     }
22   }
23 }
24
25 /**
26  * Implements hook_preprocess_HOOK() for menu-local-tasks templates.
27  *
28  * Use preprocess hook to set #attached to child elements
29  * because they will be processed by Twig and drupal_render will
30  * be invoked.
31  */
32 function seven_preprocess_menu_local_tasks(&$variables) {
33   if (!empty($variables['primary'])) {
34     $variables['primary']['#attached'] = [
35       'library' => [
36         'seven/drupal.nav-tabs',
37       ],
38     ];
39   }
40   elseif (!empty($variables['secondary'])) {
41     $variables['secondary']['#attached'] = [
42       'library' => [
43         'seven/drupal.nav-tabs',
44       ],
45     ];
46   }
47 }
48
49 /**
50  * Implements hook_preprocess_HOOK() for menu-local-task templates.
51  */
52 function seven_preprocess_menu_local_task(&$variables) {
53   $variables['attributes']['class'][] = 'tabs__tab';
54 }
55
56 /**
57  * Implements hook_preprocess_HOOK() for list of available node type templates.
58  */
59 function seven_preprocess_node_add_list(&$variables) {
60   if (!empty($variables['content'])) {
61     /** @var \Drupal\node\NodeTypeInterface $type */
62     foreach ($variables['content'] as $type) {
63       $variables['types'][$type->id()]['label'] = $type->label();
64       $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
65     }
66   }
67 }
68
69 /**
70  * Implements hook_preprocess_HOOK() for block content add list templates.
71  *
72  * Displays the list of available custom block types for creation, adding
73  * separate variables for the label and url.
74  */
75 function seven_preprocess_block_content_add_list(&$variables) {
76   if (!empty($variables['content'])) {
77     foreach ($variables['content'] as $type) {
78       $variables['types'][$type->id()]['label'] = $type->label();
79       $options = ['query' => \Drupal::request()->query->all()];
80       $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', ['block_content_type' => $type->id()], $options);
81     }
82   }
83 }
84
85 /**
86  * Implements hook_preprocess_block() for block content.
87  *
88  * Disables contextual links for all blocks.
89  */
90 function seven_preprocess_block(&$variables) {
91   if (isset($variables['title_suffix']['contextual_links'])) {
92     unset($variables['title_suffix']['contextual_links']);
93     unset($variables['elements']['#contextual_links']);
94
95     $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
96   }
97 }
98
99 /**
100  * Implements hook_preprocess_HOOK() for block admin page templates.
101  */
102 function seven_preprocess_admin_block_content(&$variables) {
103   if (!empty($variables['content'])) {
104     foreach ($variables['content'] as $key => $item) {
105       $variables['content'][$key]['url'] = $item['url']->toString();
106     }
107   }
108 }
109
110 /**
111  * Implements hook_preprocess_HOOK() for menu-local-action templates.
112  */
113 function seven_preprocess_menu_local_action(array &$variables) {
114   $variables['link']['#options']['attributes']['class'][] = 'button--primary';
115   $variables['link']['#options']['attributes']['class'][] = 'button--small';
116
117   // We require Modernizr's touch test for button styling.
118   $variables['#attached']['library'][] = 'core/modernizr';
119 }
120
121 /**
122  * Implements hook_element_info_alter().
123  */
124 function seven_element_info_alter(&$type) {
125   // We require Modernizr for button styling.
126   if (isset($type['button'])) {
127     $type['button']['#attached']['library'][] = 'core/modernizr';
128   }
129 }
130
131 /**
132  * Implements hook_preprocess_install_page().
133  */
134 function seven_preprocess_install_page(&$variables) {
135   // Seven has custom styling for the install page.
136   $variables['#attached']['library'][] = 'seven/install-page';
137 }
138
139 /**
140  * Implements hook_preprocess_maintenance_page().
141  */
142 function seven_preprocess_maintenance_page(&$variables) {
143   // Seven has custom styling for the maintenance page.
144   $variables['#attached']['library'][] = 'seven/maintenance-page';
145 }
146
147 /**
148  * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
149  *
150  * Changes vertical tabs to container.
151  */
152 function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
153   $form['#theme'] = ['node_edit_form'];
154   $form['#attached']['library'][] = 'seven/node-form';
155
156   $form['advanced']['#type'] = 'container';
157   $form['meta']['#type'] = 'container';
158   $form['meta']['#access'] = TRUE;
159   $form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
160   $form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
161
162   $form['revision_information']['#type'] = 'container';
163   $form['revision_information']['#group'] = 'meta';
164 }
165
166 /**
167  * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\media\MediaForm.
168  */
169 function seven_form_media_form_alter(&$form, FormStateInterface $form_state) {
170   // @todo Revisit after https://www.drupal.org/node/2892304 is in. It
171   // introduces a footer region to these forms which will allow for us to
172   // display a top border over the published checkbox by defining a
173   // media-edit-form.html.twig template the same way node does.
174   $form['#attached']['library'][] = 'seven/media-form';
175 }