-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathatomic.theme
More file actions
512 lines (444 loc) · 16.5 KB
/
atomic.theme
File metadata and controls
512 lines (444 loc) · 16.5 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
/**
* @file
* Functions to support theming.
*/
use Drupal\node\Entity\Node;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\media\MediaInterface;
/**
* Implements hook_preprocess_region().
*
* Assigns details from _get_collection_nav_details() to the $variables.
*/
function atomic_preprocess_region__header(&$variables) {
$variables['site_name'] = \Drupal::config('system.site')->get('name');
$variables['header_variant'] = 'normal';
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof Node && $node->bundle() == 'page') {
if (_node_is_a_collection($node)) {
$bid = $node->book['bid'];
// Add cache tags so the header re-renders when the collection nav
// position or book structure changes.
$cache_metadata = new CacheableMetadata();
$cache_metadata->addCacheTags(['ys_collection_nav:' . $bid, 'node:' . $bid]);
\Drupal::service('renderer')->addCacheableDependency($variables, $cache_metadata);
if (_get_collection_nav_position($bid) === 'in_header') {
$variables['header_variant'] = 'in_header';
_get_collection_nav_details($node, $variables);
}
}
}
}
/**
* Implements hook_preprocess_menu().
*/
function atomic_preprocess_menu(&$variables, $hook) {
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
if ($item['url']->toString() == $current_path) {
// Set top level.
$variables['items'][$key]['is_active'] = TRUE;
}
if ($item['below']) {
foreach ($item['below'] as $secondaryKey => $secondaryItem) {
if ($secondaryItem['url']->toString() == $current_path) {
// Set secondary level.
$variables['items'][$key]['below'][$secondaryKey]['is_active'] = TRUE;
}
if ($secondaryItem['below']) {
foreach ($secondaryItem['below'] as $tertiaryKey => $tertiaryItem) {
if ($tertiaryItem['url']->toString() == $current_path) {
// Set tertiary level.
$variables['items'][$key]['below'][$secondaryKey]['below'][$tertiaryKey]['is_active'] = TRUE;
}
}
}
}
}
}
}
/**
* Implements hook_preprocess_pager().
*
* Adds total pages to the pager variables.
*/
function atomic_preprocess_pager(array &$variables) {
$element = $variables['pager']['#element'];
$pager_manager = \Drupal::service('pager.manager');
$pager = $pager_manager->getPager($element);
$variables['pager']['#total_pages'] = $pager->getTotalPages();
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for pager.
*/
function atomic_theme_suggestions_pager_alter(array &$suggestions, array $variables) {
// Remove gin-lb suggestion(s).
foreach ($suggestions as $key => $suggestion) {
if ($suggestion === 'pager__gin_lb' || $suggestion === 'pager--gin-lb') {
unset($suggestions[$key]);
}
}
}
/**
* Implements hook_theme_suggestions_hook_alter() for containers.
*/
function atomic_theme_suggestions_container_alter(array &$suggestions, array &$variables) {
$type = '';
$name = '';
if (isset($variables['element']['#type'])) {
// Get the element type.
$type = $variables['element']['#type'];
// If type is defined, but not a generic "container" suggest the type.
if ($type !== 'container') {
$suggestions[] = 'container__' . $type;
}
if (isset($variables['element']['#name'])) {
// Get the element name.
$name = $variables['element']['#name'];
// If both type and name are defined, create a suggestion with both.
$suggestions[] = 'container__' . $type . '__' . $name;
}
}
}
/**
* Implements hook_preprocess_node().
*/
function atomic_preprocess_node(&$variables) {
// Allows for multiple node types to use the formatted publish date.
if (in_array($variables['node']->getType(), ['post', 'resource'])) {
$date = strtotime($variables['node']->field_publish_date?->first()?->getValue()['value']);
if ($date) {
$variables['date_formatted'] = \Drupal::service('date.formatter')->format($date, '', 'c');
}
}
// Handle resource thumbnail processing.
if ($variables['node']->getType() === 'resource') {
$view_mode = $variables['elements']['#view_mode'] ?? 'default';
$responsive_image_style_id = $view_mode === 'portrait_grid'
? 'card_grid_portrait_5_8'
: 'resource_thumbnail';
// Skip for search_result view mode as it's not needed.
if ($view_mode === 'search_result') {
return;
}
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
$fieldTeaserMedia = $node?->field_teaser_media;
// Build the merged + sorted author list via the ys_layouts service so
// view-row templates and the ResourceMetaBlock produce identical output.
// Non-affiliated authors come back with url === NULL; templates render
// those as plain text.
$author_cache_tags = [];
$variables['resource_authors'] = \Drupal::service('ys_layouts.resource_author_builder')
->build($node, $author_cache_tags);
if ($author_cache_tags) {
$variables['#cache']['tags'] = \Drupal\Core\Cache\Cache::mergeTags(
$variables['#cache']['tags'] ?? [],
$author_cache_tags
);
}
// field_discipline is rendered manually in the resource card / list-item
// templates (so the inline "Discipline: term1, term2" format with a
// single label is preserved). The manual render uses #items, which
// bypasses the field formatter pipeline, so attach the referenced term
// cache tags here — otherwise card renders won't be invalidated when a
// discipline term is renamed or deleted.
if ($node->hasField('field_discipline') && !$node->get('field_discipline')->isEmpty()) {
$discipline_cache_tags = [];
foreach ($node->get('field_discipline')->referencedEntities() as $term) {
$discipline_cache_tags = \Drupal\Core\Cache\Cache::mergeTags(
$discipline_cache_tags,
$term->getCacheTags()
);
}
if ($discipline_cache_tags) {
$variables['#cache']['tags'] = \Drupal\Core\Cache\Cache::mergeTags(
$variables['#cache']['tags'] ?? [],
$discipline_cache_tags
);
}
}
$fieldMedia = $node?->field_media;
if ($view_mode === 'portrait_grid' && $fieldTeaserMedia && !$fieldTeaserMedia->isEmpty()) {
/** @var \Drupal\media\Entity\Media $teaser_media */
$teaser_media = $fieldTeaserMedia->entity;
if ($teaser_media) {
$variables['content']['field_teaser_media'] = [
0 => \Drupal::entityTypeManager()
->getViewBuilder('media')
->view($teaser_media, 'card_grid_portrait_5_8'),
];
}
}
if (!$fieldMedia) {
return;
}
/** @var \Drupal\media\Entity\Media $media */
$referenced_entities = $fieldMedia?->referencedEntities() ?? [];
$media = reset($referenced_entities);
// Skip building responsive_media_thumbnail for video Resources: the
// video media's thumbnail is the auto-generated frame, which is rarely
// useful as a card image. Document and image bundles still get the
// thumbnail so PDF previews and image resources don't render with a
// blank card when no field_teaser_media is set.
if ($media && $media->bundle() === 'video') {
return;
}
$thumbnail = $media?->thumbnail;
if (!$thumbnail) {
return;
}
/** @var \Drupal\file\Entity\File $file */
$referenced_entities = $thumbnail->referencedEntities();
$file = reset($referenced_entities);
if (!$file) {
return;
}
// Create responsive_image render array using the thumbnail uri.
$variables['responsive_media_thumbnail'] = [
'#theme' => 'responsive_image',
'#uri' => $file->getFileUri(),
'#responsive_image_style_id' => $responsive_image_style_id,
'#height' => $thumbnail?->height,
'#width' => $thumbnail?->width,
'#attributes' => [
'loading' => 'lazy',
'alt' => $media->label(),
],
];
}
}
/**
* Implements hook_preprocess_paragraph__accordion_item().
*
* Inject the heading_level based on the parent's heading presence.
*/
function atomic_preprocess_paragraph__accordion_item(&$variables) {
if ($accordion = $variables['paragraph']->getParentEntity()) {
if ($accordion->hasField('field_heading')) {
$heading = $accordion->field_heading->value;
$variables['attributes']['heading_level'] = empty($heading) ? 2 : 3;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for field templates.
*/
function atomic_theme_suggestions_field_alter(array &$suggestions, array $variables) {
// Check if the field is attached to a node.
if ($variables['element']['#entity_type'] == 'node') {
$view_mode = $variables['element']['#view_mode'];
$field_name = $variables['element']['#field_name'];
// Add a theme suggestion for the field based on the parent node's view mode.
$suggestions[] = 'field__' . $variables['element']['#entity_type'] . '__' . $field_name . '__' . $view_mode;
}
}
/**
* Implements hook_preprocess_views_view().
*
* Adds section wrapper and aria-label for YS Views Basic views.
*/
function atomic_preprocess_views_view(&$variables) {
$view = $variables['view'];
// Only process views_basic_scaffold and views_basic_scaffold_events views.
if (!in_array($view->id(), ['views_basic_scaffold', 'views_basic_scaffold_events'])) {
return;
}
// Content type labels for aria-label.
$content_type_labels = [
'post' => 'Posts',
'event' => 'Events',
'page' => 'Pages',
'profile' => 'Profiles',
];
// Default aria-label.
$aria_label = 'Content';
// Extract content type from view arguments if available.
if (!empty($view->args)) {
// Views basic passes JSON parameters in the arguments.
// The content type filter is typically in the first argument.
$first_arg = reset($view->args);
if ($first_arg && is_string($first_arg)) {
if (isset($content_type_labels[$first_arg])) {
$aria_label = $content_type_labels[$first_arg] . " collection";
}
}
}
// Add section wrapper variables.
$variables['use_section_wrapper'] = TRUE;
$variables['section_aria_label'] = $aria_label;
}
/**
* Implements hook_preprocess_HOOK().
*
* Attempt to pre-set the main content ID based on whether
* the node associated is in a content collection.
*/
function atomic_preprocess_html(array &$variables) {
$variables['main_content_id'] = '#main-content';
$variables['secondary_nav_present'] = FALSE;
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof Node && $node->bundle() == 'page') {
if (_node_is_a_collection($node)) {
$bid = $node->book['bid'];
// Add cache tags so the HTML re-renders when the collection nav
// position or book structure changes.
$cache_metadata = new CacheableMetadata();
$cache_metadata->addCacheTags(['ys_collection_nav:' . $bid, 'node:' . $bid]);
\Drupal::service('renderer')->addCacheableDependency($variables, $cache_metadata);
// Page is just a normal collection (not using primary nav).
if (_get_collection_nav_position($bid) !== 'in_header') {
$variables['secondary_nav_present'] = TRUE;
$variables['main_content_id'] = '#main-content-after-secondary-nav';
}
}
}
}
/**
* Implements hook_preprocess_layout for page_meta().
*
* Checks if page uses collection primary nav and hides the secondary nav.
*/
function atomic_preprocess_layout__page_meta(&$variables) {
/** @var \Drupal\node\Entity\Node $entity_node */
$entity_node = $variables['content']['#entity'];
if (_node_is_a_collection($entity_node)) {
$bid = $entity_node->book['bid'];
// Add cache tags so the layout re-renders when the collection nav
// position or book structure changes.
$cache_metadata = new CacheableMetadata();
$cache_metadata->addCacheTags(['ys_collection_nav:' . $bid, 'node:' . $bid]);
\Drupal::service('renderer')->addCacheableDependency($variables, $cache_metadata);
$variables['show_secondary_nav'] = TRUE;
if (_get_collection_nav_position($bid) === 'in_header') {
$variables['show_secondary_nav'] = FALSE;
}
}
}
/**
* Helper function to determine if a node is part of a book.
*
* @param Node $node
* @return void
*/
function _node_is_a_collection($node) {
return ($node->book) ? TRUE : FALSE;
}
/**
* Helper function to get the collection navigation position.
*
* @param int $nid
* The node ID of the book root.
*
* @return string
* The navigation position value (e.g., 'in_header',
* 'in_content'), or empty string if not set.
*/
function _get_collection_nav_position(int $nid) {
$cid = 'collection:nav_position:' . $nid;
// Try to get from persistent cache first.
if ($cache = \Drupal::cache()->get($cid)) {
return $cache->data;
}
/** @var \Drupal\node\Entity\Node $node */
$node = Node::load($nid);
$nav_position = $node?->field_collection_nav_position?->getString() ?: '';
// Store in persistent cache with cache tags for targeted invalidation.
\Drupal::cache()->set($cid, $nav_position, -1, ['ys_collection_nav:' . $nid]);
return $nav_position;
}
/**
* Helper function to get collection navigation details.
*
* All $variables set are passed back to the calling function.
*
* @param \Drupal\node\Entity\Node $node
* The current node.
* @param array $variables
* The template variables array.
*/
function _get_collection_nav_details(Node $node, array &$variables) {
$bid = $node->book['bid'];
$book_manager = \Drupal::service('book.manager');
$book_tree_data = $book_manager->bookTreeAllData($bid);
$book_tree = $book_manager->bookTreeOutput($book_tree_data);
/** @var \Drupal\node\Entity\Node $parent_node */
$parent_node = \Drupal::EntityTypeManager()->getStorage('node')->load($bid);
if ($parent_node) {
$variables['collection_nav_name'] = $parent_node->getTitle();
$variables['collection_nav_link'] = $parent_node->toUrl()->toString();
$variables['collection_nav_menu_items'] = _process_collection_nav_menu_items($book_tree['#items']);
}
}
/**
* Helper function to duplicate and add menu items to collection nav menu.
*
* Any item with a "submenu" gets duplicated.
*
* @param array|null $items
* The menu items to process.
*
* @return array|null
* The processed menu items, or NULL if empty.
*/
function _process_collection_nav_menu_items($items) {
if (empty($items)) {
return NULL;
}
$menu_items = reset($items)['below'];
foreach ($menu_items as &$menuItem) {
// Skip menu items that do not have subitems.
if (empty($menuItem['below'])) {
continue;
}
// Clone top level menu item.
$clonedMenuItem = $menuItem;
// We don't want any other subitems, just the top level.
$clonedMenuItem['below'] = NULL;
// Default the node_title to the menu title.
$clonedMenuItem['node_title'] = $clonedMenuItem['title'];
// If the menu item is associated with a node, replace the node_title
// with the node's title.
if ($menuItem['url']->isRouted()) {
$routeParameters = $menuItem['url']->getRouteParameters();
if (array_key_exists('node', $routeParameters)) {
$nodeId = $routeParameters['node'];
if ($nodeId) {
/** @var \Drupal\node\Entity\Node $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
if ($node) {
$clonedMenuItem['node_title'] = $node->getTitle();
}
}
}
}
// Add cloned item to the beginning of the menu.
array_unshift($menuItem['below'], $clonedMenuItem);
}
return $menu_items;
}
/**
* Implements hook_ENTITY_TYPE_view_alter() for media entities.
*
* Suppresses the contextual edit pencil on media rendered as a reference
* from a Resource node. Authors can still reach the media edit form via
* the parent node's Edit screen. Scoped to Resource only per YISP-122;
* site-wide application is documented as a follow-up pitch in
* docs-local/2026-04-30-suppress-media-contextual-links-option-2.md.
*
* Implemented at the entity_view_alter stage (rather than preprocess) so
* the unset happens before contextual.module's preprocess injects the
* pencil's data-contextual-token attribute and contextual-region class.
*/
function atomic_media_view_alter(array &$build, MediaInterface $entity, EntityViewDisplayInterface $display) {
if (!isset($entity->_referringItem)) {
return;
}
$parent = $entity->_referringItem->getEntity();
if ($parent instanceof Node && $parent->bundle() === 'resource') {
unset($build['#contextual_links']);
}
}