/
LocationHierarchyForm.php
356 lines (313 loc) · 10.9 KB
/
LocationHierarchyForm.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
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
<?php
namespace Drupal\farm_ui_location\Form;
use Drupal\asset\Entity\AssetInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\farm_location\AssetLocationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form for changing the hierarchy of location assets.
*
* @ingroup farm
*/
class LocationHierarchyForm extends FormBase {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The asset location service.
*
* @var \Drupal\farm_location\AssetLocationInterface
*/
protected $assetLocation;
/**
* Constructs a new LocationHierarchyForm.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\farm_location\AssetLocationInterface $asset_location
* The asset location service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, AssetLocationInterface $asset_location) {
$this->entityTypeManager = $entity_type_manager;
$this->assetLocation = $asset_location;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('asset.location')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'farm_ui_location_form';
}
/**
* Check access.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The account to check.
* @param \Drupal\asset\Entity\AssetInterface|null $asset
* The asset to check (optional).
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(AccountInterface $account, AssetInterface $asset = NULL) {
// If the asset is not a location, forbid access.
if (!$this->assetLocation->isLocation($asset)) {
return AccessResult::forbidden();
}
// If the asset does not have child locations, forbid access.
if (empty($this->getLocations($asset))) {
return AccessResult::forbidden();
}
// Allow access if the asset has child locations.
return AccessResult::allowedIf($asset->access('view', $account));
}
/**
* Generate the page title.
*
* @param \Drupal\asset\Entity\AssetInterface|null $asset
* Optionally specify the parent asset that this page is being built for.
*
* @return string
* Returns the translated page title.
*/
public function getTitle(AssetInterface $asset = NULL) {
if (!empty($asset)) {
return $this->t('Locations in %location', ['%location' => $asset->label()]);
}
return $this->t('Locations');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, AssetInterface $asset = NULL) {
// If no asset was specified, show a map of all locations.
if (is_null($asset)) {
$form['map'] = [
'#type' => 'farm_map',
'#map_type' => 'locations',
];
}
// Add a DIV for the JavaScript content.
$form['content'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => [
'locations-tree',
],
],
];
// Create a hidden field to store hierarchy changes recorded client-side.
$form['changes'] = [
'#type' => 'hidden',
];
// Add buttons for toggling drag and drop, saving, and resetting.
$form['actions'] = ['#type' => 'actions'];
$form['actions']['toggle'] = [
'#type' => 'button',
'#value' => $this->t('Toggle drag and drop'),
'#attributes' => [
'class' => [
'button--secondary',
],
],
];
$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#attributes' => [
'class' => [
'button--primary',
],
],
];
$form['actions']['reset'] = [
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#attributes' => [
'class' => [
'button--danger',
],
],
];
// Attach the location drag and drop JavaScript.
$form['#attached']['library'][] = 'farm_ui_location/locations-drag-and-drop';
$tree = [
[
'asset_id' => !empty($asset) ? $asset->id() : '',
'text' => !empty($asset) ? $asset->label() : $this->t('All locations'),
'children' => !empty($asset) ? $this->buildTree($asset) : $this->buildTree(),
'url' => !empty($asset) ? $asset->toUrl('canonical', ['absolute' => TRUE])->toString() : Url::fromRoute('farm.locations', [], ['absolute' => TRUE])->toString(),
],
];
$form['#attached']['drupalSettings']['asset_tree'] = $tree;
$form['#attached']['drupalSettings']['asset_parent'] = !empty($asset) ? $asset->id() : '';
// Return the form.
return $form;
}
/**
* Build the asset tree.
*
* @param \Drupal\asset\Entity\AssetInterface|null $asset
* Optionally specify the parent asset, to only build a sub-tree. If
* omitted, all assets will be included.
*
* @return array
* Returns the asset tree for use in Drupal JS settings.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function buildTree(AssetInterface $asset = NULL) {
$locations = $this->getLocations($asset);
$tree = [];
if ($locations) {
foreach ($locations as $location) {
$element = [
'asset_id' => $location->id(),
'text' => $location->label(),
'children' => $this->buildTree($location),
'url' => $location->toUrl('canonical', ['absolute' => TRUE])->toString(),
];
$element['original_parent'] = $asset ? $asset->id() : '';
$tree[] = $element;
}
}
return $tree;
}
/**
* Gets location assets.
*
* @param \Drupal\asset\Entity\AssetInterface|null $asset
* Optionally provide a parent asset to only retrieve its direct children.
*
* @return \Drupal\asset\Entity\AssetInterface[]
* An array of location assets.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getLocations(AssetInterface $asset = NULL) {
// Query unarchived location assets.
$storage = $this->entityTypeManager->getStorage('asset');
$query = $storage->getQuery()
->accessCheck(TRUE)
->condition('is_location', TRUE)
->condition('status', 'archived', '!=');
// Limit to a specific parent or no parent.
if ($asset) {
$query->condition('parent', $asset->id());
}
else {
$query->condition('parent', NULL, 'IS NULL');
}
// Query and load the assets.
$asset_ids = $query->execute();
if (empty($asset_ids)) {
return [];
}
/** @var \Drupal\asset\Entity\AssetInterface[] $assets */
$assets = $storage->loadMultiple($asset_ids);
// Filter out assets that the user cannot view.
$assets = array_filter($assets, function ($asset) {
return $asset->access('view');
});
// Sort assets by name, using natural sort algorithm.
usort($assets, function ($a, $b) {
return strnatcmp($a->label(), $b->label());
});
return $assets;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Only process the form if the "Save" button was clicked.
if ($form_state->getTriggeringElement()['#id'] != 'edit-save') {
return;
}
// Load hierarchy changes. If there are none, do nothing.
$changes = Json::decode($form_state->getValue('changes'));
if (empty($changes)) {
$this->messenger()->addStatus($this->t('No changes were made.'));
return;
}
// Get asset storage.
$storage = $this->entityTypeManager->getStorage('asset');
// Maintain a list of assets that need to be saved.
$save_assets = [];
// Maintain a list of assets that were not editable by the user.
$restricted_assets = [];
// Iterate through the changes.
foreach ($changes as $change) {
// Load the asset.
$asset = $storage->load($change['asset_id']);
// If the user does not have permission to edit the asset, count it so
// that we can add a warning message later, and skip it.
if (!$asset->access('edit')) {
$restricted_assets[] = $asset;
continue;
}
// Remove the original parent.
if (!empty($asset->get('parent'))) {
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $parent */
foreach ($asset->get('parent') as $delta => $parent) {
$parent_id = $parent->getValue()['target_id'];
if ($change['original_parent'] == $parent_id) {
unset($asset->get('parent')[$delta]);
if (!array_key_exists($asset->id(), $save_assets)) {
$save_assets[$asset->id()] = $asset;
}
}
}
}
// Add the new parent, if applicable.
if (!empty($change['new_parent'])) {
$asset->get('parent')[] = ['target_id' => $change['new_parent']];
if (!array_key_exists($asset->id(), $save_assets)) {
$save_assets[$asset->id()] = $asset;
}
}
}
// Save assets with a revision message.
/** @var \Drupal\asset\Entity\AssetInterface[] $save_assets */
foreach ($save_assets as $asset) {
$message = $this->t('Parents removed via the Locations drag and drop editor.');
$parent_names = [];
foreach ($asset->get('parent') as $parent) {
$parent_names[] = $storage->load($parent->getValue()['target_id'])->label();
}
if (!empty($parent_names)) {
$message = $this->t('Parents changed to %parents via the Locations drag and drop editor.', ['%parents' => implode(', ', $parent_names)]);
}
$asset->setNewRevision(TRUE);
$asset->setRevisionLogMessage($message);
$asset->save();
}
// Show a summary of the results.
$message = $this->formatPlural(count($save_assets), 'Updated the parent hierarchy of %count asset.', 'Updated the parent hierarchy of %count assets.', ['%count' => count($save_assets)]);
$this->messenger()->addStatus($message);
// If any edits were restricted, show a warning.
if ($restricted_assets) {
$message = $this->formatPlural(count($restricted_assets), '%count asset could not be changed because you do not have permission.', '%count assets could not be changed because you do not have permission.', ['%count' => count($restricted_assets)]);
$this->messenger()->addWarning($message);
}
}
}