This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodifiers.module
197 lines (164 loc) · 6.09 KB
/
modifiers.module
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
<?php
/**
* @file
* Modifiers module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Render\Element;
use Drupal\modifiers\Modifiers;
/**
* Implements hook_preprocess_HOOK().
*/
function modifiers_preprocess_html(&$variables) {
// Attach initialization script for all JS modifications.
$variables['#attached']['library'][] = 'modifiers/init';
}
/**
* Implements hook_entity_view_alter().
*/
function modifiers_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if (!($entity instanceof FieldableEntityInterface) || !$entity->hasField(Modifiers::FIELD)) {
return;
}
$entity_type = $entity->getEntityTypeId();
$entity_id = Html::getClass($entity_type . '-' . $entity->id());
$display_mode = $display->getMode();
// Skip preview display mode on paragraph entities.
if ($entity_type === 'paragraph' && $display_mode === 'preview') {
return;
}
// Add common class for all content entities.
$build['#attributes']['class'][] = 'modifiers';
// Create an unique entity ID class.
$build['#attributes']['class'][] = Html::getClass('modifiers-id-' . $entity_id);
// Add entity type class.
$build['#attributes']['class'][] = Html::getClass('modifiers-type-' . $entity_type);
// Add entity bundle class.
$build['#attributes']['class'][] = Html::getClass('modifiers-bundle-' . $entity->bundle());
// Add entity display mode class.
$build['#attributes']['class'][] = Html::getClass('modifiers-display-' . $display_mode);
// Initialize modifiers service.
/** @var \Drupal\modifiers\Modifiers $modifiers */
$modifiers = \Drupal::service('modifiers');
$config = $modifiers->extractEntityConfig($entity, Modifiers::FIELD, ['modifiers' => []]);
// Allow other modules to alter extracted config.
$context = [
'build' => $build,
'entity' => $entity,
'display' => $display,
];
\Drupal::moduleHandler()
->alter('modifiers_entity_view_config', $config['modifiers'], $context);
if (empty($config['modifiers'])) {
return;
}
$build['#modifiers'][] = $config['modifiers'];
// Selector needs to be very specific.
$selector = 'html body .modifiers.modifiers-id-' . $entity_id;
$modifications = [];
// Fill all modifications.
$modifiers->process($modifications, $config['modifiers'], $selector);
if (!empty($modifications)) {
$modifiers->apply($modifications, $build, $entity_id);
}
}
/**
* Implements template_preprocess_layout().
*/
function modifiers_preprocess_layout(&$variables) {
$blocks = [];
// Find all modifier blocks between regions.
foreach (Element::children($variables['content']) as $region_key) {
$block_keys = Element::children($variables['content'][$region_key]);
// Search between all blocks inside specific region.
foreach ($block_keys as $block_key) {
$region_block = $variables['content'][$region_key][$block_key];
// Only if block contains a custom block entity.
if (isset($region_block['content']['#block_content'])) {
/** @var \Drupal\block_content\Entity\BlockContent $entity */
$entity = $region_block['content']['#block_content'];
// Check if this block is of a modifier block type.
if (substr($entity->bundle(), -9) === '_modifier') {
// Apply to section if checkbox is checked.
if ($entity->hasField('field_lb_modifiers_section')
&& !empty($entity->get('field_lb_modifiers_section')->value)
) {
$blocks['section'][] = $entity;
}
else {
// Apply this modifier to region.
$blocks['region'][$region_key][] = $entity;
}
}
}
}
}
// Process all modifiers if found.
if (!empty($blocks)) {
// Get unique ID for every section with modifiers.
$section_id = &drupal_static(__FUNCTION__, 0);
$section_id++;
// Process all section modifiers if available.
if (!empty($blocks['section'])) {
$entity_id = 'section-' . $section_id;
_modifiers_layout_apply($variables, $blocks['section'], $entity_id);
}
// Process all region modifiers if available.
if (!empty($blocks['region'])) {
foreach ($blocks['region'] as $region_key => $entities) {
$entity_id = 'section-' . $section_id . '-region-' . $region_key;
_modifiers_layout_apply($variables, $entities, $entity_id, $region_key);
}
}
}
}
/**
* Apply modifications extracted from Layout Builder blocks.
*
* @param array $build
* The build array for rendering.
* @param array|\Drupal\block_content\Entity\BlockContent[] $entities
* The set of modifier blocks.
* @param string $entity_id
* The block entity ID.
* @param string|null $region
* The region name or NULL for section.
*/
function _modifiers_layout_apply(array &$build, $entities, $entity_id, $region = NULL) {
// Initialize modifiers service.
/** @var \Drupal\modifiers\Modifiers $modifiers */
$modifiers = \Drupal::service('modifiers');
// Compose selector and config to field mapping.
$selector = 'html body .modifiers.modifiers-id-' . $entity_id;
$config = [];
// Process all entities.
foreach ($entities as $entity) {
$entity_config = [];
// Process all fields attached to entity.
foreach ($entity->getFields() as $field) {
$modifiers->extractFieldConfig($field, $entity_config);
}
// Fill entity values into config array.
$config[$entity->bundle()][] = $entity_config;
}
$modifications = [];
// Fill all modifications.
$modifiers->process($modifications, $config, $selector);
// Apply modifications if available.
if (!empty($modifications)) {
if (!empty($region)) {
$build['region_attributes'][$region]->addClass([
'modifiers',
Html::getClass('modifiers-id-' . $entity_id),
]);
}
else {
$build['attributes']['class'][] = 'modifiers';
$build['attributes']['class'][] = Html::getClass('modifiers-id-' . $entity_id);
}
$modifiers->apply($modifications, $build, $entity_id);
}
}