forked from YCloudYUSA/y_lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
y_lb.module
423 lines (382 loc) · 13.5 KB
/
y_lb.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
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
<?php
/**
* @file
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\editor\Entity\Editor;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Form\DefaultsEntityForm;
use Drupal\layout_builder\Form\OverridesEntityForm;
use Drupal\node\NodeInterface;
use Drupal\y_lb\Form\YLBEntityViewDisplay;
use Drupal\y_lb\Form\YLBOverridesEntityForm;
/**
* Implements hook_theme().
*/
function y_lb_theme($existing, $type, $theme, $path) {
return [
'block__lb_node_share' => [
'base hook' => 'block',
],
'block__lb_node_locations' => [
'base hook' => 'block',
],
'block__lb_node_title' => [
'base hook' => 'block',
],
'block__ws_site_logo' => [
'base hook' => 'block',
],
'block__ws_site_name' => [
'base hook' => 'block'
],
'block__ws_search_bar' => [
'base hook' => 'block'
],
'blb_section__y_lb' => [
'template' => 'blb-section--y-lb',
'render element' => 'content',
'base hook' => 'layout',
],
'page__node__landing_page_lb' => [
'base hook' => 'page',
],
'page__node__layout__discard_changes' => [
'base hook' => 'page',
'template' => 'page--node--layout--discard-revert',
],
'page__node__layout__revert' => [
'base hook' => 'page',
'template' => 'page--node--layout--discard-revert',
],
'menu__main' => [
'base hook' => 'menu__main',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for page templates.
*/
function y_lb_theme_suggestions_page_alter(array &$suggestions) {
// Add content type suggestions for node types.
$node = \Drupal::routeMatch()->getParameter('node');
// Skip admin route.
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
return;
}
// Skip page without node.
if (!$node instanceof NodeInterface) {
return;
}
// Skip nodes without LB usage.
if (!$node->hasField('layout_builder__layout')) {
return;
}
// Skip nodes with optional layout builder usage with deactivated usage.
if ($node->hasField('field_use_layout_builder')
&& !$node->field_use_layout_builder->value) {
return;
}
// Add custom suggestion.
$node_type = $node->getType();
$page__node_pos = array_search('page__node', $suggestions);
$page__node__type = 'page__node__' . $node_type;
array_splice($suggestions, $page__node_pos + 1, 0, $page__node__type);
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
*/
function y_lb_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$content = $variables['elements']['content'];
if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) {
// Add 'block--BLOCK-TYPE.html.twig'.
$block_type_suggestions[] = 'block__' . $content['#block_content']->bundle();
// Add 'block--BLOCK-TYPE--VIEW-MODE.html.twig'.
$block_type_suggestions[] = 'block__' . $content['#block_content']->bundle() . '__' . $content['#view_mode'];
// Because block__block_content exists twice in $suggestions,
// the suggestion arrays are reversed for further processing.
$suggestions_rev = array_reverse($suggestions);
$block_type_suggestions = array_reverse($block_type_suggestions);
// Insert the block type and view mode suggestions between
// block__block_content and the block instance-specific suggestions.
$index = array_search('block__block_content', $suggestions_rev);
if (is_numeric($index)) {
array_splice($suggestions_rev, $index, 0, $block_type_suggestions);
$suggestions = array_reverse($suggestions_rev);
}
// If block__block_content isn't present as a suggestion.
else {
$suggestions_rev = array_merge($suggestions_rev, $block_type_suggestions);
$suggestions = array_reverse($suggestions_rev);
}
}
// Add specific suggestion if custom WS template should be used.
if (isset($variables['elements']['#ws_template'])) {
$suggestions[] = str_replace('-', '_', $variables['elements']['#ws_template']);
}
}
/**
* Implements hook_form_alter().
*/
function y_lb_form_alter(&$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
$form_id = $form_object->getFormId();
if ($form_object instanceof OverridesEntityForm
|| $form_object instanceof DefaultsEntityForm
|| $form_object->getFormId() === 'layout_layout_builder_form') {
$form['#attached']['library'][] = 'y_lb/layout_builder';
$form['#attached']['library'][] = 'y_lb/main';
}
}
/**
* Implements hook_page_attachments_alter().
*/
function y_lb_page_attachments_alter(array &$page) {
$entity_types = array_keys(\Drupal::entityTypeManager()->getDefinitions());
$entity_canonical_routes = [];
foreach ($entity_types as $entity_type_id) {
$entity_canonical_routes[] = 'entity.' . $entity_type_id . '.canonical';
}
$route_match = \Drupal::routeMatch();
$current_route = $route_match->getRouteName();
// Attach the libraries only in entity canonical route and only when the
// Layout Builder is enabled for the view mode.
if (in_array($current_route, $entity_canonical_routes)) {
$current_route_exploded = explode('.', $current_route);
$route_entity_type_id = $current_route_exploded[1];
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $route_match->getParameter($route_entity_type_id);
// Do not apply the y_lb/main since we can't find the entity.
if (!$entity) {
return;
}
// Skip entities without LB usage.
if (!$entity->hasField('layout_builder__layout')) {
return;
}
// Skip entities with optional layout builder usage with deactivated usage.
if ($entity->hasField('field_use_layout_builder')
&& !$entity->field_use_layout_builder->value) {
return;
}
$page['#attached']['library'][] = 'y_lb/main';
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for layout_builder_add_block.
*
* @todo Keep an eye on https://www.drupal.org/project/drupal/issues/3074435
*/
function y_lb_form_layout_builder_add_block_alter(&$form, FormStateInterface $form_state) {
if (isset($form['settings']['block_form']['#block'])
&& strpos($form['settings']['block_form']['#block']->bundle(), 'lb_') === 0
&& isset($form['settings']['label_display'])) {
// Uncheck the 'Display Title' by default.
$form['settings']['label_display']['#default_value'] = FALSE;
}
}
/**
* Implements hook_preprocess_HOOK() for block templates.
*/
function y_lb_preprocess_block(&$variables) {
switch ($variables['plugin_id']) {
case 'system_breadcrumb_block':
$variables['attributes']['class'][] = 'block-lb-breadcrumbs';
$variables['#attached']['library'][] = 'y_lb/breadcrumbs';
break;
case 'lb_node_share':
case 'lb_node_tags':
case 'lb_node_locations':
$variables['#attached']['library'][] = 'y_lb/blocks';
break;
}
$variables['attributes']['class'][] = 'block-' . Html::getClass($variables['plugin_id']);
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function y_lb_theme_suggestions_layout_alter(array &$suggestions, array $variables) {
$original_hook = $variables['theme_hook_original'];
if ($original_hook !== 'blb_section' && !str_starts_with($original_hook, 'blb_section__')) {
return;
}
// Remove existing suggestions.
$suggestions = [];
$suggestions[] = 'blb_section__y_lb';
$entity = $variables['content']['#entity'] ?? NULL;
if ($entity && $entity instanceof EntityInterface) {
$suggestions[] = 'blb_section__' . $entity->bundle();
$label = $variables['content']['#settings']['label'] ?? NULL;
if ($label) {
$suggestions[] = 'blb_section__' . $entity->bundle() . '__' . strtolower($label);
}
}
}
/**
* Implements hook_ckeditor_css_alter().
*/
function y_lb_ckeditor_css_alter(array &$css, Editor $editor) {
$css[] = \Drupal::service('extension.list.module')->getPath('y_lb') . '/assets/css/typography.css';
}
/**
* Implements hook_preprocess_blb_container_wrapper().
*/
function y_lb_preprocess_blb_container_wrapper(&$variables) {
$variables['attributes']['class'][] = 'container-wrapper';
}
/**
* Implements hook_entity_type_alter().
*/
function y_lb_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['entity_view_display']
->setFormClass('layout_builder', YLBEntityViewDisplay::class);
$entity_types['node']
->setFormClass('layout_builder', YLBOverridesEntityForm::class);
}
/**
* Implements hook_entity_base_field_info().
*/
function y_lb_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'node') {
$fields['override_styles'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Override default Y Styles'))
->setDescription(t('Whether or not the node should override the default Y styles (e.g. Color scheme, etc.).'))
->setRevisionable(TRUE)
->setDefaultValue(FALSE)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['styles'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Y Styles'))
->setDescription(t('Node specific Y Styles to be applied.'))
->setRevisionable(TRUE)
->setSetting('case_sensitive', TRUE)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
}
return $fields;
}
/**
* Implements hook_preprocess_HOOK() for pages.
*/
function y_lb_preprocess_page(&$variables) {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'] ?? NULL;
// Skip page without node.
if (!$node instanceof NodeInterface) {
return;
}
// Skip nodes without LB usage.
if (!$node->hasField('layout_builder__layout')) {
return;
}
// Skip nodes with optional layout builder usage with deactivated usage.
if ($node->hasField('field_use_layout_builder')
&& !$node->field_use_layout_builder->value) {
return;
}
$libraries = [];
$classes = [];
/** @var \Drupal\y_lb\WSStyleManager $ws_styles */
$ws_style = \Drupal::service('plugin.manager.ws_style');
/** @var \Drupal\y_lb\WSStyleOptionManager $ws_style_options */
$ws_style_option = \Drupal::service('plugin.manager.ws_style_option');
// Check individual node for overridden Y Styles.
if ($node->override_styles->value) {
$styles = unserialize($node->styles->value);
}
else {
// Get Y Styles from node view display settings.
foreach (['full', 'default'] as $display) {
$view_display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('node.' . $node->bundle() . '.' . $display);
if (!$view_display) {
continue;
}
$settings = $view_display->getThirdPartySettings('y_lb');
$styles = $settings['styles'] ?? [];
if ($styles) {
break;
}
}
}
// Get libraries from the style's groups.
foreach (array_keys($styles) as $group) {
$libraries = array_merge($libraries, $ws_style->getLibraries($group));
}
// Get libraries from the style's options.
foreach ($styles as $group => $style) {
if (is_string($style)) {
$libraries = array_merge($libraries, $ws_style_option->getLibraries($group, $style));
$classes = array_merge($classes, $ws_style_option->getClasses($group, $style));
}
}
// Attach WS libraries to the node.
foreach ($libraries as $library) {
$variables['#attached']['library'][] = $library;
}
if ($classes) {
$variables['page_classes'] = $classes;
}
}
/**
* Implements hook_preprocess_html().
*/
function y_lb_preprocess_html(array &$variables) {
// Preprocess the node to find if it is related to Layout Builder.
if (!empty($variables['node_type'])) {
// Add content type suggestions for node types.
$node = \Drupal::routeMatch()->getParameter('node');
// Skip admin route.
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
return;
}
// Skip page without node.
if (!$node instanceof NodeInterface) {
return;
}
// Skip nodes without LB usage.
if (!$node->hasField('layout_builder__layout')) {
return;
}
// Skip nodes with optional layout builder usage with deactivated usage.
if ($node->hasField('field_use_layout_builder')
&& !$node->field_use_layout_builder->value) {
return;
}
$variables['body_classes'][] = 'page-with-lb';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function y_lb_theme_suggestions_node_alter(array &$suggestions, array $variables) {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['elements']['#node'];
$view_mode = $variables['elements']['#view_mode'];
if ($node instanceof NodeInterface &&
$view_mode =='full'
&& $node->hasField('field_use_layout_builder')
&& $node->field_use_layout_builder->value) {
$suggestions[] = 'node__' . $node->getType() . '__lb';
}
}
/**
* Remove show preview to fix section duplication.
*
* Implements hook_form_FORM_ID_alter().
*/
function y_lb_form_layout_builder_configure_section_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form['layout_settings']['ui']['tab_content']['layout']['breakpoints'])) {
foreach ($form['layout_settings']['ui']['tab_content']['layout']['breakpoints'] as &$breakpoint) {
unset($breakpoint['#ajax']);
}
}
}