-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcua_theme.theme
304 lines (269 loc) · 11.1 KB
/
cua_theme.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
<?php
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
/**
* Implements hook_form_FORM_ID_alter() for the search form.
*/
function cua_theme_form_search_block_form_alter(array &$form, FormStateInterface $form_state, $form_id): void {
// Change the label of the submit button to "Submit".
$form['actions']['submit']['#value'] = t('Submit');
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
*
* @param array $suggestions
* An associative array of suggestions for the theme hook.
* @param array $variables
* An associative array of variables passed to the theme hook.
*/
function cua_theme_theme_suggestions_block_alter(array &$suggestions, array $variables): void {
// Add a suggestion based on the field name and view mode if it exists.
if (isset($variables['element']['#field_name'])) {
$suggestions[] = 'field__' .
$variables['element']['#field_name'] . '__' .
$variables['element']['#view_mode'];
}
// Block suggestions for custom block bundles.
if (isset($variables['elements']['content']['#block_content'])) {
array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for field templates.
*
* Adds theme suggestions for field templates based on field name and view mode.
*
* @param array $suggestions
* An array of suggestions for the theme hook.
* @param array $variables
* An array of variables passed to the theme hook.
*/
function cua_theme_theme_suggestions_field_alter(array &$suggestions, array $variables): void {
$suggestions[] = 'field__' .
$variables['element']['#field_name'] . '__' .
$variables['element']['#view_mode'];
}
/**
* Implements hook_form_FORM_ID_alter() for the search block form.
*
* Modifies the search block form to customize its appearance by adding a
* placeholder, changing the submit button value to a Font Awesome icon, and
* applying a custom class to the submit button.
*
* @param array $form
* The form structure array.
* @param array $form_state
* An array representing the form's state.
*/
/**
* Implements hook_form_alter().
*/
function cua_theme_form_alter(&$form, &$form_state, $form_id): void {
// Check if the form is the search form.
if ($form_id == 'search_block_form') {
// Change the text on the search button.
$form['actions']['submit']['#value'] = t('Search');
// Add a placeholder text to the search input field.
$form['keys']['#attributes']['placeholder'] = t('Search');
}
}
/**
* Implements hook_preprocess_HOOK() for page templates.
*
* Prepares variables for page templates.
*
* @param array $variables
* An associative array of variables to pass to the theme template.
*/
function cua_theme_preprocess_page(array &$variables): void {
$site_config = \Drupal::config('system.site');
$variables['site_name'] = $site_config->get('name');
$variables['site_slogan'] = $site_config->get('slogan');
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
*
* @param array $definitions
* An associative array of theme hook definitions.
* @param array $extra
* An array of additional data, typically the current state.
*/
function cua_theme_plugin_filter_block__layout_builder_alter(array &$definitions, array $extra): void {
// Load all custom block types.
$block_types = BlockContentType::loadMultiple();
$block_prefix = "inline_block";
// Sort alphabetically by label.
uasort($block_types, function($a, $b) {
return SortArray::sortByKeyString($a, $b, 'label');
});
// Add suggestions for each block type.
foreach ($block_types as $block_type => $block) {
// Append prefix to the block type.
$definition = &$definitions["$block_prefix:$block_type"];
// Add category 'Add CU Blocks'.
$definition['category'] = t('Add CU Blocks');
// Sync block->label with definition.
$definition['admin_label'] = $block->label();
}
}
/**
* Implements hook_preprocess_HOOK() for block templates.
*
* Prepares variables for block templates. This function is called before
* the block is rendered, allowing you to preprocess variables used in
* the block template. It ensures that necessary data is available and
* properly formatted for use in the block's theme template.
*
* @param array $variables
* An associative array of variables to pass to the theme template. This
* array includes the data that will be available to the block's Twig
* template.
*/
/**
* Implements hook_preprocess_block().
*/
function cua_theme_preprocess_block(array &$variables): void {
// Check if the block content is available in the variables array.
if (!empty($variables['content']['#block_content'])) {
$block_content = $variables['content']['#block_content'];
$renderer = \Drupal::service('renderer');
// Define an array of field names to exclude from preprocessing.
$excluded_fields = [
'field_visibility_arrows',
'field_video_mp4',
'field_video_youtube',
// Add other fields you want to exclude here.
];
// Preprocess all fields dynamically, excluding specified fields.
foreach ($block_content->getFieldDefinitions() as $field_name => $field_definition) {
// Skip excluded fields.
if (in_array($field_name, $excluded_fields)) {
continue;
}
// Check if the block content has the current field.
if ($block_content->hasField($field_name)) {
// Get the field's value as an array.
$field_items = $block_content->get($field_name)->getValue();
if (!empty($field_items) && is_array($field_items)) {
// Retrieve the first item of the field's value if it exists.
$field_value = reset($field_items);
// Check if the field value is an array and contains a 'value' key.
if (is_array($field_value) && isset($field_value['value'])) {
// Assign the value to the variables array without the "field_" prefix.
$variables[str_replace('field_', '', $field_name)] = $field_value['value'];
}
}
}
}
// Special handling for specific fields like links and images.
if ($block_content->hasField('field_link')) {
// Handle the link field.
$link_field_items = $block_content->get('field_link')->getValue();
if (!empty($link_field_items)) {
$links = [];
foreach ($link_field_items as $item) {
$url_object = Url::fromUri($item['uri']);
$url = $url_object->isExternal() ? $item['uri'] : $url_object->toString();
$links[] = ['url' => $url, 'title' => $item['title']];
}
$variables['links'] = $links;
if (count($links) > 1) {
$variables['attributes']['class'][] = 'multiple-links';
}
}
}
if ($block_content->hasField('field_image')) {
// Handle the image field.
$image_field_items = $block_content->get('field_image')->getValue();
if (!empty($image_field_items)) {
$image_field_value = reset($image_field_items);
if (is_array($image_field_value)) {
$image_render_array = $block_content->get('field_image')->view(['type' => 'default']);
$variables['card_image'] = $renderer->render($image_render_array);
$variables['image'] = $image_field_value['image'] ?? NULL;
$variables['image_url'] = $image_field_value['uri'] ?? NULL;
$variables['image_alt'] = $image_field_value['alt'] ?? NULL;
$variables['image_title'] = $image_field_value['title'] ?? NULL;
}
}
}
// Set cache context and tags to optimize caching.
$variables['#cache']['contexts'][] = 'url';
$variables['#cache']['tags'][] = 'block_content:' . $block_content->id();
$variables['#cache']['max-age'] = Cache::PERMANENT;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for HTML templates.
*
* Adds suggestions for HTML templates based on the current route.
*
* @param array &$suggestions
* An associative array of suggestions for the theme hook.
* @param array $variables
* An associative array of variables passed to the theme hook.
*/
function cua_theme_theme_suggestions_html_alter(array &$suggestions, array $variables): void {
// Get the current route match.
$route_match = \Drupal::routeMatch();
// Get the name of the current route.
$route_name = $route_match->getRouteName();
// Add a suggestion based on the route name.
$suggestions[] = 'html__' . str_replace('.', '__', $route_name);
}
/**
* Implements hook_preprocess_HOOK() for HTML templates.
*
* Prepares variables for HTML templates.
*
* @param array $variables
* An associative array of variables to pass to the theme template.
*/
/**
* Implements hook_preprocess_Html().
*/
function cua_theme_preprocess_html(array &$variables): void {
// Get the current user object.
$user = \Drupal::currentUser();
// Check if the user has the 'administrator' role.
if ($user->hasRole('administrator')) {
// Add a class to body_classes.
$variables['attributes']['class'][] = 'user-administrator';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() to provide custom theme
* suggestions for forms.
*/
function cua_theme_theme_suggestions_form_alter(array &$suggestions, array $variables): void {
// Get the form ID.
$form_id = $variables['element']['#form_id'];
// Replace dashes with underscores in the form ID.
$form_id = str_replace('--', '_', $form_id);
// Add a theme suggestion based on the modified form ID.
$suggestions[] = 'form__' . $form_id;
}
function cua_theme_preprocess_spacer(&$variables): void {
// Get the current user object.
$user = \Drupal::currentUser();
// Check if the current user has either the 'administrator' or 'advancement_admin' role.
$variables['is_admin'] = $user->hasRole('administrator') || $user->hasRole('advancement_admin');
}
/**
* Implements hook_preprocess_page_title().
*/
function cua_theme_preprocess_page_title(array &$variables): void {
// Load the node from the current route, if available.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// Add the node to the variables for the page title template.
$variables['node'] = $node;
// If the field exists, add it to the variables.
if ($node->hasField('field_title_visibility')) {
$variables['title_visibility'] = $node->get('field_title_visibility')->value;
}
}
}