-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutenboot.theme
307 lines (266 loc) · 9.38 KB
/
gutenboot.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
<?php
/**
* @file
* Theme file for gutenboot base theme.
*/
use Drupal\block\Entity\Block;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_form_alter().
*/
function gutenboot_form_alter(&$form, $form_state, $form_id) {
// Remove preview button from contact form.
if ($form_id == 'contact_message_feedback_form') {
$form['actions']['preview']['#access'] = FALSE;
}
}
/**
* Implements template_preprocess_block()
*/
function gutenboot_preprocess_block(&$variables) {
$block = Block::load($variables['elements']['#id']);
if ($variables['base_plugin_id'] == 'block_content') {
// Get the machine name of the custom block type.
$connection = \Drupal::service('database');
$custom_block_type = $connection->query("SELECT type FROM block_content WHERE uuid = :uuid", [
':uuid' => $variables['derivative_plugin_id'],
])->fetchField();
// Call the block-type-specific preprocessor.
$block_type_preprocessor = __FUNCTION__ . '_' . $custom_block_type;
if (function_exists($block_type_preprocessor)) {
$block_type_preprocessor($variables);
}
}
// If this is a views block and it has a more link
// then add the link to the block as a button.
if (isset($variables['content']['#type']) && $variables['content']['#type'] == 'view') {
gutenboot_preprocess_block_views($variables);
}
if ($variables['plugin_id'] == 'system_main_block') {
// If this is a 404 block then wrap the system block content.
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name == 'system.404') {
$variables['content']['#prefix'] = '</article><div class="container">';
$variables['content']['#suffix'] = '</div></article>';
}
}
}
/**
* Preprocessor for page banner blocks.
*/
function gutenboot_preprocess_block_page_banner(&$variables) {
$uuid = $variables['derivative_plugin_id'];
$block_content = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
if ($block_content) {
if ($file = $block_content->field_image->entity) {
$uri = $file->getFileUri();
$htmlId = $variables['attributes']['id'];
// Add style element to vars.
$variables['backdrop_css'] = _gutenboot_responsive_background_css('banner', "#$htmlId .backdrop", $uri);
}
}
}
/**
* Preprocessor for views blocks.
*/
function gutenboot_preprocess_block_views(&$variables) {
$view = $variables['content']['#view'];
// @todo Handle setting "use_more_always: false"
$display = $view->getDisplay('default');
if ($display->isMoreEnabled() && $display->usesLinkDisplay()) {
$link_display_option = $display->getOption('link_display');
// @todo Handle link provided by other view displays (e.g. page)
if ($link_display_option == 'custom_url') {
$title = $display->getOption('use_more_text');
$url = $display->getOption('link_url');
}
// Prepare the link element.
$variables['more_link'] = [
'#type' => 'link',
'#title' => $title,
'#url' => Url::fromUserInput('/' . $url),
'#attributes' => [
'class' => 'btn btn-outline-primary',
],
];
}
}
/**
* Implements template_preprocess_html()
*/
function gutenboot_preprocess_html(&$variables) {
global $base_url;
$theme_path = \Drupal::service('theme.manager')->getActiveTheme()->getPath();
$variables['favicon_dir'] = "$base_url/$theme_path/images/favicon";
$variables['theme_color'] = '#15326c';
}
/**
* Implements template_preprocess_page()
*/
function gutenboot_preprocess_page(&$variables) {
global $base_url;
$theme_path = \Drupal::service('theme.manager')->getActiveTheme()->getPath();
$variables['site_name'] = \Drupal::config('system.site')->get('name');
$variables['site_logo'] = "$base_url/$theme_path/logo.svg";
$variables['current_year'] = date('Y');
}
/**
* Implements template_preprocess_field()
*/
function gutenboot_preprocess_field(&$variables) {
// Call the type-specific preprocessor.
$field_type_preprocessor = __FUNCTION__ . '_type_' . $variables['field_type'];
if (function_exists($field_type_preprocessor)) {
$field_type_preprocessor($variables);
}
// Call the name-specific preprocessor.
$field_name_preprocessor = __FUNCTION__ . '_' . str_replace("field_", "", $variables['field_name']);
if (function_exists($field_name_preprocessor)) {
$field_name_preprocessor($variables);
}
}
/**
* Implements template_preprocess_node()
*/
function gutenboot_preprocess_menu(&$variables, $hook) {
if ($variables['menu_name'] == 'social-media') {
foreach ($variables['items'] as &$item) {
try {
$uri = $item['url']->getUri();
$icons = [
'/facebook\.com/' => 'facebook-square',
'/instagram\.com/' => 'instagram',
];
foreach ($icons as $uri_pattern => $icon_name) {
if (preg_match("$uri_pattern", $uri)) {
$item['icon'] = $icon_name;
}
}
}
catch (UnexpectedValueException $ex) {}
}
}
}
/**
* Implements template_preprocess_node()
*/
function gutenboot_preprocess_node(&$variables) {
// Change the submission date format.
$variables['date'] = \Drupal::service('date.formatter')
->format($variables['node']->getCreatedTime(), 'custom', 'j M Y');
// Call the type-specific preprocessor.
$node_type_preprocessor = __FUNCTION__ . '_' . $variables['node']->getType();
if (function_exists($node_type_preprocessor)) {
$node_type_preprocessor($variables);
}
}
/**
* Preprocessor for portfolio item nodes.
*/
function gutenboot_preprocess_node_portfolio_item(&$variables) {
$node = $variables['node'];
if ($variables['view_mode'] == 'teaser') {
$image_uri = $node->get('field_image')->entity->getFileUri();
$image_style = ImageStyle::load('project_thumbnail');
$variables['image_url'] = file_url_transform_relative($image_style->buildUrl($image_uri));
}
}
/**
* Implements template_preprocess_paragraph()
*/
function gutenboot_preprocess_paragraph(&$variables) {
$paragraph = $variables['elements']['#paragraph'];
// Call the type-specific preprocessor.
$paragraph_type_preprocessor = __FUNCTION__ . '_' . $paragraph->getType();
if (function_exists($paragraph_type_preprocessor)) {
$paragraph_type_preprocessor($variables);
}
}
/**
* Implements template_preprocess_swiftmailer()
*/
function gutenboot_preprocess_swiftmailer(&$vars) {
// Add PNG logo url
$theme_path = \Drupal::theme()->getActiveTheme()->getPath();
$vars['logo'] = file_create_url("$theme_path/logo.png");
}
/**
* Implements hook_theme_suggestions_block_alter().
*/
function gutenboot_theme_suggestions_block_alter(&$suggestions, $variables) {
// Add template suggestions for custom block types.
if ($variables['elements']['#base_plugin_id'] == 'block_content') {
// Get the block type from the database.
$uuid = $variables['elements']['#derivative_plugin_id'];
$block_type = \Drupal::database()
->select('block_content', 'bc')
->fields('bc', ['type'])
->condition('uuid', $uuid)
->execute()
->fetchField();
// Add a template suggestion for the block type.
$suggestions[] = 'block__' . $block_type;
}
}
/**
* Implements hook_theme_suggestions_views_view_alter().
*/
function gutenboot_theme_suggestions_views_view_alter(&$suggestions, $variables) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
$suggestions[] = 'views_view__' . $view->id();
}
/**
* Implements hook_theme_suggestions_views_view_table_alter().
*/
function gutenboot_theme_suggestions_views_view_table_alter(&$suggestions, $variables) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
$suggestions[] = 'views_view_table__' . $view->id();
}
/**
* Adds background CSS to variables.
*
* @param string $image_style_basename
* The base name of the image style. i.e. <basename>_<breakpoint_name>
* @param string $selector
* The CSS selector to be used for this background image CSS
* @param string $image_uri
* The URI for the source image.
* @return array
* The render array for the style element.
*/
function _gutenboot_responsive_background_css(string $image_style_basename, string $selector, string $image_uri) {
// Fetch this theme's breakpoints.
$breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('gutenboot');
// Prepare an array of CSS media queries and URLs to be
// assembled into background-image rules.
$cssRules = [];
foreach ($breakpoints as $breakpoint_id => $breakpoint) {
// Derive the image style name from the breakpoint name.
$image_style_name = str_replace('gutenboot.', "{$image_style_basename}_", $breakpoint_id);
// Try to load the image style.
if ($image_style = ImageStyle::load($image_style_name)) {
// Save the cssRule components.
$cssRules[] = [
'mediaQuery' => $breakpoint->getMediaQuery(),
'url' => file_url_transform_relative($image_style->buildUrl($image_uri)),
];
}
}
// Generate CSS to provide a background-image rule for each
// of this theme's breakpoints that have a matching image style.
// Use block's HTML ID as the selector.
$css = '';
foreach ($cssRules as $rule) {
$mediaQuery = $rule['mediaQuery'];
$url = $rule['url'];
$css .= "@media $mediaQuery { $selector::before, $selector::after { background-image: url('$url'); }}\n";
}
return [
'#type' => 'html_tag',
'#tag' => 'style',
'#value' => $css,
];
}