forked from YCloudYUSA/y_lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
y_lb.module
115 lines (103 loc) · 3.92 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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\layout_builder\Form\DefaultsEntityForm;
use Drupal\layout_builder\Form\OverridesEntityForm;
/**
* 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'
]
];
}
/**
* 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);
}
}
}
/**
* Implements hook_form_alter().
*/
function y_lb_form_alter(&$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
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();
// Attach the libraries only in entity canonical route.
if (in_array($route_match->getRouteName(), $entity_canonical_routes)) {
// Attach the Y-LB main styles.
$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;
}
}