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