-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPatternDisplayFormTrait.php
246 lines (227 loc) · 7.89 KB
/
PatternDisplayFormTrait.php
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
<?php
namespace Drupal\ui_patterns\Form;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray;
use Drupal\ui_patterns\Plugin\PatternSourceBase;
/**
* Trait PatternDisplayFormTrait.
*
* @property \Drupal\ui_patterns\UiPatternsManager $patternsManager
* @property \Drupal\ui_patterns\UiPatternsSourceManager $sourceManager
* @property \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* @method \Drupal\Core\StringTranslation\TranslatableMarkup t($string, array $args = [], array $options = [])
*
* @package Drupal\ui_patterns\Form
*/
trait PatternDisplayFormTrait {
/**
* Build pattern display form.
*
* @param array $form
* Form array.
* @param string $tag
* Source field tag.
* @param array $context
* Plugin context.
* @param array $configuration
* Default configuration coming form the host form.
*/
public function buildPatternDisplayForm(array &$form, $tag, array $context, array $configuration) {
$pattern_select_id = Html::getUniqueId('patterns-select');
$form['pattern'] = [
'#type' => 'select',
'#empty_value' => '_none',
'#title' => $this->t('Pattern'),
'#options' => $this->patternsManager->getPatternsOptions(),
'#default_value' => isset($configuration['pattern']) ? $configuration['pattern'] : NULL,
'#required' => TRUE,
'#id' => $pattern_select_id,
];
$form['variants'] = ['#type' => 'container'];
/** @var \Drupal\ui_patterns\Definition\PatternDefinition $definition */
foreach ($this->patternsManager->getDefinitions() as $pattern_id => $definition) {
if ($definition->hasVariants()) {
$form['variants'][$pattern_id] = [
'#type' => 'select',
'#title' => $this->t('Variant'),
'#options' => $definition->getVariantsAsOptions(),
'#default_value' => isset($configuration['pattern_variant']) ? $configuration['pattern_variant'] : NULL,
'#weight' => 0,
'#states' => [
'visible' => [
'select[id="' . $pattern_select_id . '"]' => ['value' => $pattern_id],
],
],
];
}
$form['pattern_mapping'][$pattern_id] = [
'#type' => 'container',
'#weight' => 1,
'#states' => [
'visible' => [
'select[id="' . $pattern_select_id . '"]' => ['value' => $pattern_id],
],
],
'settings' => $this->getMappingForm($pattern_id, $tag, $context, $configuration),
];
}
$this->moduleHandler->alter('ui_patterns_display_settings_form', $form, $configuration);
}
/**
* Get mapping form.
*
* @param string $pattern_id
* Pattern ID for which to print the mapping form for.
* @param string $tag
* Source field plugin tag.
* @param array $context
* Plugin context.
* @param array $configuration
* Default configuration coming form the host form.
*
* @return array
* Mapping form.
*/
public function getMappingForm($pattern_id, $tag, array $context, array $configuration) {
/** @var \Drupal\ui_patterns\Definition\PatternDefinition $pattern */
$pattern = $this->patternsManager->getDefinition($pattern_id);
$elements = [
'#type' => 'table',
'#header' => [
$this->t('Source'),
$this->t('Plugin'),
$this->t('Destination'),
$this->t('Weight'),
],
];
$elements['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'field-weight',
];
$destinations = ['_hidden' => $this->t('- Hidden -')] + $pattern->getFieldsAsOptions();
$fields = [];
foreach ($this->sourceManager->getFieldsByTag($tag, $context) as $field_name => $field) {
$weight = (int) $this->getDefaultValue($configuration, $field_name, 'weight');
$fields[$field_name] = [
'info' => [
'#plain_text' => $field->getFieldLabel(),
],
'plugin' => [
'#plain_text' => $field->getPluginLabel(),
],
'destination' => [
'#type' => 'select',
'#title' => $this->t('Destination for @field', ['@field' => $field->getFieldLabel()]),
'#title_display' => 'invisible',
'#default_value' => $this->getDefaultValue($configuration, $field_name, 'destination'),
'#options' => $destinations,
],
'weight' => [
'#type' => 'weight',
'#default_value' => $weight,
'#delta' => 20,
'#title' => $this->t('Weight for @field field', ['@field' => $field->getFieldLabel()]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => ['field-weight'],
],
],
'#attributes' => [
'class' => ['draggable'],
],
'#weight' => $weight,
];
}
uasort($fields, [SortArray::class, 'sortByWeightProperty']);
return array_merge($elements, $fields);
}
/**
* Normalize settings coming from a form submission.
*
* @param array $settings
* Pattern display form values array.
*/
public static function processFormStateValues(array &$settings) {
if (isset($settings['variants']) && isset($settings['variants'][$settings['pattern']])) {
$settings['pattern_variant'] = $settings['variants'][$settings['pattern']];
unset($settings['variants']);
}
// Normalize only when necessary.
if (isset($settings['pattern_mapping'][$settings['pattern']]['settings'])) {
$settings['pattern_mapping'] = $settings['pattern_mapping'][$settings['pattern']]['settings'];
// Process fields and filter out the hidden ones.
foreach ($settings['pattern_mapping'] as $key => $setting) {
if ($setting['destination'] == '_hidden') {
unset($settings['pattern_mapping'][$key]);
}
else {
list($plugin, $source) = explode(PatternSourceBase::DERIVATIVE_SEPARATOR, $key, 2);
$settings['pattern_mapping'][$key]['plugin'] = $plugin;
$settings['pattern_mapping'][$key]['source'] = $source;
}
}
// Normalize weights.
$weight = 0;
uasort($settings['pattern_mapping'], [SortArray::class, 'sortByWeightElement']);
foreach ($settings['pattern_mapping'] as $key => $setting) {
$settings['pattern_mapping'][$key]['weight'] = $weight++;
}
}
}
/**
* Helper function: return mapping destination given plugin id and field name.
*
* @param string $plugin
* Current plugin ID.
* @param string $source
* Source field name.
* @param array $settings
* Setting array.
*
* @return string|null
* Destination field or NULL if none found.
*/
public function getMappingDestination($plugin, $source, array $settings) {
$mapping_id = $plugin . PatternSourceBase::DERIVATIVE_SEPARATOR . $source;
if (isset($settings['pattern_mapping'][$mapping_id])) {
return $settings['pattern_mapping'][$mapping_id]['destination'];
}
return NULL;
}
/**
* Helper function: check if given source field has mapping destination.
*
* @param string $plugin
* Current plugin ID.
* @param string $source
* Source field name.
* @param array $settings
* Setting array.
*
* @return bool
* TRUE if source has destination field, FALSE otherwise.
*/
public function hasMappingDestination($plugin, $source, array $settings) {
return $this->getMappingDestination($plugin, $source, $settings) !== NULL;
}
/**
* Helper function: get default value.
*
* @param array $configuration
* Configuration.
* @param string $field_name
* Field name.
* @param string $value
* Value name.
*
* @return string
* Field property value.
*/
protected function getDefaultValue(array $configuration, $field_name, $value) {
if (isset($configuration['pattern_mapping'][$field_name][$value])) {
return $configuration['pattern_mapping'][$field_name][$value];
}
return NULL;
}
}