-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbricks.module
More file actions
224 lines (202 loc) · 7.76 KB
/
Copy pathbricks.module
File metadata and controls
224 lines (202 loc) · 7.76 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
<?php
use Drupal\bricks\Bricks;
use Drupal\bricks\BricksFieldItemInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Prepares variables for `field.html.twig`.
*/
function bricks_preprocess_field(&$variables) {
$element = $variables['element'];
if (substr($element['#formatter'], 0, 7) === 'bricks_') {
$variables['items'] = [
[
'content' => Bricks::nestItems($variables['items'], $variables['element']['#items']),
],
];
}
}
/* BRICKS EDITING */
/**
* Implements hook_field_widget_info_alter().
*/
function bricks_field_widget_info_alter(array &$info) {
// Let Bricks to re-use ANY Entity Reference -compatible widgets:
foreach ($info as $widget_id => &$widget_info) {
if (in_array('entity_reference', $widget_info['field_types'])) {
$widget_info['field_types'][] = 'bricks';
}
if (in_array('entity_reference_revisions', $widget_info['field_types'])) {
$widget_info['field_types'][] = 'bricks_revisioned';
}
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter() for
* `entity_reference_autocomplete`.
*/
function bricks_field_widget_entity_reference_autocomplete_form_alter(&$element, FormStateInterface $form_state, $context) {
$field_type = $context['items']->getFieldDefinition()->getType();
// @TODO: Replace by 'Nested bricks' widget setting.
if (in_array($field_type, ['bricks'])) {
// @TODO: Find a better way to be used in _bricks_preprocess_tabledrag_form().
$element['#widget'] = 'entity_reference_autocomplete';
// #default_value is en Entity or NULL.
_bricks_form_element_alter($element, $context['items'][$context['delta']], $element['target_id']['#default_value']);
hide($element['depth']);
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter() for
* `bricks_tree_autocomplete`.
*/
function bricks_field_widget_bricks_tree_autocomplete_form_alter(&$element, FormStateInterface $form_state, $context) {
bricks_field_widget_entity_reference_autocomplete_form_alter($element, $form_state, $context);
}
/**
* Prepares variables for `field-multiple-value-form.html.twig`.
*/
function bricks_preprocess_field_multiple_value_form(&$variables) {
_bricks_preprocess_tabledrag_form($variables, 'element', 'entity_reference_autocomplete', $variables['element']['#field_name'] . '-delta-order');
}
/**
* Helper function for hook_preprocess_field_multiple_value_form().
*/
function _bricks_preprocess_tabledrag_form(&$variables, $element_key, $widget, $order_class, $render_options = FALSE) {
$element = $variables[$element_key];
$operation_key = NULL;
// @TODO: Replace by 'Nested bricks' widget setting.
if (isset($element['#widget']) && $element['#widget'] == $widget ||
isset($element[0]['#widget']) && $element[0]['#widget'] == $widget) {
// @TODO: Tmp hack for the proper indent width calculation.
$variables['table']['#header'][0]['style'] = 'min-width: 150px';
$variables['table']['#header'][] = ['data' => t('Depth'), 'class' => ['bricks-depth-header']];
if ($render_options) {
// Find Operations column
$operation_key = array_filter($variables['table']['#header'], function($item) {
return isset($item['is_operation']);
});
if (!empty($operation_key) && is_array($operation_key)) {
$operation_key = array_keys($operation_key);
$operation_key = array_pop($operation_key);
// Insert new options column before operations.
array_splice($variables['table']['#header'], $operation_key, 0, [['data' => t('Options')]]);
}
}
$row = 0;
foreach (Element::children($element) as $i => $key) {
if ($key !== 'add_more' && $key !== 'header_actions') {
$depth = $element[$key]['depth']['#value'];
$indentation = [];
if ($depth > 0) {
$indentation = [
'#theme' => 'indentation',
'#size' => $depth,
];
}
$drag_cell = &$variables['table']['#rows'][$row]['data'][0];
$drag_cell['data'] = !empty($indentation) ? \Drupal::service('renderer')->render($indentation) : '' . $drag_cell['data'];
// @TODO
$drag_cell['style'] = 'width: auto; min-width: 150px';
show($element[$key]['depth']);
$variables['table']['#rows'][$row]['data'][] = \Drupal::service('renderer')->render($element[$key]['depth']);
if ($render_options && !is_null($operation_key)) {
// Insert data row in options column.
array_splice($variables['table']['#rows'][$row]['data'], $operation_key, 0,[['data' => \Drupal::service('renderer')->render($element[$key]['options']), 'class' => 'inline-entity-form-brick-options']]);
}
}
if ($key !== 'add_more') {
$row++;
}
}
$tabledrag_options = &$variables['table']['#tabledrag'];
$tabledrag_options[0]['relationship'] = 'all';
$tabledrag_options[] = [
'action' => 'depth',
'relationship' => 'group',
'group' => 'bricks-depth',
];
// Fake option to enable indentation:
$tabledrag_options[] = [
'action' => 'match',
'relationship' => 'parent',
'group' => $order_class,
];
$variables['table']['#attached']['library'][] = 'bricks/tabledrag.relationship-all';
}
}
/**
* Helper function for widget's formElement().
*/
function _bricks_form_element_alter(&$element, $item, $entity) {
$element['depth'] = [
// @TODO: Other types break the correct indentations.
'#type' => 'hidden',
'#default_value' => !empty($item->depth) ? $item->depth : 0,
'#weight' => 10,
'#attributes' => [
'class' => ['bricks-depth'],
],
];
$element['options'] = [
'#type' => 'container',
'#weight' => 100,
'#attributes' => [
'class' => ['container-inline'],
],
];
if ($entity) {
if ($entity->bundle() == 'layout' && \Drupal::service('module_handler')->moduleExists('layout_discovery')) {
$element['options']['layout'] = [
'#type' => 'select',
'#options' => \Drupal::service('plugin.manager.core.layout')->getLayoutOptions(),
'#default_value' => !empty($item->options['layout']) ? $item->options['layout'] : NULL,
];
}
if ($entity->bundle() != 'layout') {
$element['options']['view_mode'] = [
'#type' => 'select',
'#options' => \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle($entity->getEntityTypeId(), $entity->bundle()),
'#default_value' => !empty($item->options['view_mode']) ? $item->options['view_mode'] : NULL,
];
}
}
$element['options']['css_class'] = [
'#type' => 'textfield',
'#default_value' => !empty($item->options['css_class']) ? $item->options['css_class'] : '',
'#size' => 10,
'#attributes' => [
'placeholder' => t('CSS class(-es)'),
],
];
$element['options']['css_id'] = [
'#type' => 'textfield',
'#default_value' => !empty($item->options['css_id']) ? $item->options['css_id'] : '',
'#size' => 10,
'#attributes' => [
'placeholder' => t('CSS ID'),
],
];
}
/* MISC */
/**
* Prepares variables for `block.html.twig` for `system_powered_by_block`.
*/
function bricks_preprocess_block__system_powered_by_block(&$variables) {
$bricks_link = '<a href="https://uibricks.com">Bricks</a>';
$variables['content']['#markup'] = str_replace('>Drupal</a>', '>Drupal</a> & ' . $bricks_link, $variables['content']['#markup']);
}
/**
* Implements hook_entity_presave().
*/
function bricks_entity_presave(EntityInterface $entity) {
if ($entity instanceof FieldableEntityInterface) {
foreach ($entity->getFields() as $field_item_list) {
if ($field_item_list->get(0) instanceof BricksFieldItemInterface) {
Bricks::correctDepths($field_item_list);
}
}
}
}