This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathda_vinci.theme
331 lines (301 loc) · 11.4 KB
/
da_vinci.theme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* @file
* Functions to support theming.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\node\Entity\Node;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Adds body classes if certain regions have content.
*/
function da_vinci_preprocess_html(&$variables) {
if (theme_get_setting('responsive-project')) {
$variables['responsive'] = theme_get_setting('responsive-project');
}
if (theme_get_setting('styling')) {
$variables['styling_code'] = theme_get_setting('styling_code');
}
if (theme_get_setting('body-font')) {
$body_font = str_replace('+', ' ', theme_get_setting('body-font'));
$variables['fonts'] = theme_get_setting('body-font');
if (isset($variables['styling_code'])) {
$variables['styling_code'] .= '
body {
font-family: ' . $body_font . ';
}';
}
else {
$variables['styling_code'] = 'body {
font-family: ' . $body_font . ';
}';
}
}
if (theme_get_setting('header-font')) {
$header_font = str_replace('+', ' ', theme_get_setting('header-font'));
if (theme_get_setting('body-font')) {
$variables['fonts'] .= '|' . theme_get_setting('header-font');
}
else {
$variables['fonts'] = theme_get_setting('header-font');
}
if (isset($variables['styling_code'])) {
$variables['styling_code'] .= '
h1, h2, h3, h4, h5, h6 {
font-family: ' . $header_font . ';
}';
}
else {
$variables['styling_code'] = 'h1, h2, h3, h4, h5, h6 {
font-family: ' . $header_font . ';
}';
}
}
if (!empty($variables['page']['featured_top'])) {
$variables['attributes']['class'][] = 'has-featured-top';
}
$path = \Drupal::service('path.current')->getPath();
$path_args = explode('/', $path);
if (isset($path_args[1]) && isset($path_args[2]) && ($path_args[1] == 'node') && (is_numeric($path_args[2]))) {
$variables['attributes']['class'][] = 'page-node-' . $path_args[2];
}
$is_front_page = \Drupal::service('path.matcher')->isFrontPage();
if ($is_front_page) {
$variables['#attached']['library'][] = 'da_vinci/front';
}
// Load Libraries for Path.
if (!empty(isset($path_args[2]))) {
$variables['#attached']['library'][] = 'da_vinci/' . $path_args[1] . '-' . $path_args[2];
}
else {
$variables['#attached']['library'][] = 'da_vinci/' . $path_args[1];
}
// Page specific info.
$variables['path_info']['args'] = FALSE;
$path = \Drupal::service('path.current')->getPath();
$path = ltrim($path, '/');
$path_args = explode('/', $path);
if (isset($path_args[0]) && isset($path_args[1]) && ($path_args[0] == 'node') && (is_numeric($path_args[1]))) {
$variables['attributes']['class'][] = 'page-node-' . $path_args[1];
}
if (isset($path_args[0]) && isset($path_args[1]) && ($path_args[0] == 'user')) {
$variables['attributes']['class'][] = 'page-user-' . $path_args[1];
}
if (isset($path_args[0]) && isset($path_args[1]) && isset($path_args[2]) &&
$path_args[0] == 'taxonomy' && $path_args[1] == 'term') {
$variables['attributes']['class'][] = 'page-taxonomy-term-' . $path_args[2];
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($path_args[2]);
if (!empty($term)) {
$vocabulary_id = $term->bundle();
$variables['attributes']['class'][] = 'page-vocabulary-' . Html::cleanCssIdentifier($vocabulary_id);
}
}
$view_id = \Drupal::routeMatch()->getParameter('view_id');
if ($view_id) {
$variables['attributes']['class'][] = 'page-view-' . Html::cleanCssIdentifier($view_id);
}
// SEO Information.
$http_equiv = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'x-ua-compatible',
'content' => 'ie=edge',
],
];
$variables['page']['#attached']['html_head'][] = [$http_equiv, 'http-equiv'];
}
/**
* Implements hook_preprocess_HOOK() for page templates.
*/
function da_vinci_preprocess_page_title(&$variables) {
// 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']) {
// Add a wrapper div using the title_prefix and title_suffix render
// elements.
$variables['title_prefix']['shortcut_wrapper'] = [
'#markup' => '<div class="shortcut-wrapper clearfix">',
'#weight' => 100,
];
$variables['title_suffix']['shortcut_wrapper'] = [
'#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;
}
}
/**
* Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
*/
function da_vinci_preprocess_maintenance_page(&$variables) {
// 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
if (!$variables['db_is_active']) {
$variables['site_name'] = '';
}
// Maintenance page.
$variables['#attached']['library'][] = 'da_vinci/maintenance_page';
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function da_vinci_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']);
}
}
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*/
function da_vinci_preprocess_block(&$variables) {
if ($variables['base_plugin_id'] == 'system_menu_block' && isset($variables['attributes']['id'])) {
$variables['content']['#attributes']['block'] = $variables['attributes']['id'];
}
// Add a clearfix class to system branding blocks.
if ($variables['plugin_id'] == 'system_branding_block') {
$variables['attributes']['class'][] = 'clearfix';
}
// Add the block type class to the custom blocks.
if (isset($variables['content']['#block_content'])) {
$block_type = $variables['content']['#block_content']->bundle();
$variables['attributes']['class'][] = 'block-content-type-' . $block_type;
}
// Add the block view mode class to the custom blocks.
if (isset($variables['elements']['#configuration']['view_mode'])) {
$block_view_mode = $variables['elements']['#configuration']['view_mode'];
$variables['attributes']['class'][] = 'block--view-mode-' . $block_view_mode;
}
}
/**
* Implements hook_preprocess_HOOK() for menu.html.twig.
*/
function da_vinci_preprocess_menu(&$variables) {
$variables['attributes']['class'][] = 'clearfix';
// Get the current path.
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
// If path is current_path, set active to li.
if ($item['url']->toString() == $current_path) {
// Add active link.
$variables['items'][$key]['attributes']['class'] = 'active';
}
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-task.html.twig.
*/
function da_vinci_preprocess_menu_local_task(&$variables) {
$link = $variables['element']['#link'];
$url = $link['url'];
$options = $url->getOptions();
$url->setOptions($options + $link['localized_options']);
$variables['item'] = Link::fromTextAndUrl($link['title'], $url);
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function da_vinci_theme_suggestions_node_alter(array &$suggestions, array $variables) {
// Add template suggestions based on the current view mode.
$node = $variables['elements']['#node'];
$suggestions[] = 'node__' . $variables['elements']['#view_mode'];
$suggestions[] = 'node__' . $node->bundle() . '__' . $variables['elements']['#view_mode'];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function da_vinci_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
// Remove the block and replace dashes with underscores in the block ID to
// use for the hook name.
if (isset($variables['attributes']['block'])) {
$hook = str_replace(['block-', '-'], ['', '_'], $variables['attributes']['block']);
$suggestions[] = $variables['theme_hook_original'] . '__' . $hook;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function da_vinci_theme_suggestions_user_alter(array &$suggestions, array $variables, $hook) {
// Define the view mode.
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$user_id = \Drupal::service('current_user')->id();
$suggestions[] = 'user__' . $sanitized_view_mode;
$suggestions[] = 'user__' . $user_id;
$suggestions[] = 'user__' . $user_id . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function da_vinci_theme_suggestions_form_alter(array &$suggestions, array $variables) {
if ($variables['element']['#form_id'] == 'search_block_form') {
$suggestions[] = 'form__search_block_form';
}
if ($variables['element']['#form_id'] == 'node_form') {
$suggestions[] = 'node_edit_form';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function da_vinci_theme_suggestions_file_link_alter(array &$suggestions, array $variables) {
// Get the node type.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$type = $node->getType();
// Create the theme hook suggestion.
$suggestions[] = 'file_link__type_' . $type;
}
}
/**
* Implements hook_form_alter().
*/
function da_vinci_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, ['search_block_form', 'search_form'])) {
$key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
if (!isset($form[$key]['submit']['#attributes'])) {
$form[$key]['submit']['#attributes'] = new Attribute();
}
$form[$key]['submit']['#attributes']->addClass('search-form__submit');
if (theme_get_setting('search-placeholder')) {
$form['keys']['#attributes']['placeholder'] = t('Search');
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for get page template suggestions based on the node type.
*/
function da_vinci_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if (!$node instanceof Node) {
$node = Node::load($node);
}
$content_type = $node->bundle();
$suggestions[] = 'page__' . $content_type;
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function da_vinci_preprocess_views_view(&$variables) {
$variables['theme_hook_original'] = $variables['theme_hook_original'] . '__' . $variables['id'];
}
/**
* Implements theme_suggestions_field_alter().
*/
function da_vinci_theme_suggestions_field_alter(&$suggestions, $variables) {
// Add a suggestion field template for all the fields of a viewmode.
$suggestions[] = 'field__' . $variables['element']['#view_mode'];
// Add a suggestion field template for a field depending on entity, on bundle,
// on field and on viewmode.
$suggestions[] = 'field__' . $variables['element']['#field_name'] . '__' . $variables['element']['#entity_type'] . '_' . $variables['element']['#bundle'] . '__' . $variables['element']['#view_mode'];
}