|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Contains \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay; |
|
8
|
|
|
|
|
9
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
10
|
|
|
use Drupal\entity_browser\SelectionDisplayBase; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Show current selection and delivers selected entities. |
|
14
|
|
|
* |
|
15
|
|
|
* @EntityBrowserSelectionDisplay( |
|
16
|
|
|
* id = "multi_step_display", |
|
17
|
|
|
* label = @Translation("Multi step selection display"), |
|
18
|
|
|
* description = @Translation("Show current selection display and delivers selected entities.") |
|
19
|
|
|
* ) |
|
20
|
|
|
*/ |
|
21
|
|
|
class MultiStepDisplay extends SelectionDisplayBase { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state) { |
|
27
|
|
|
$selected_entities = $form_state->get(['entity_browser', 'selected_entities']); |
|
28
|
|
|
|
|
29
|
|
|
$form = []; |
|
30
|
|
|
$form['#attached']['library'][] = 'entity_browser/multi_step_display'; |
|
31
|
|
|
$form['selected'] = [ |
|
32
|
|
|
'#theme_wrappers' => ['container'], |
|
33
|
|
|
'#attributes' => ['class' => ['selected-entities-list']], |
|
34
|
|
|
'#tree' => TRUE |
|
35
|
|
|
]; |
|
36
|
|
|
foreach ($selected_entities as $id => $entity) { |
|
37
|
|
|
$form['selected']['items_'. $entity->id()] = [ |
|
38
|
|
|
'#theme_wrappers' => ['container'], |
|
39
|
|
|
'#attributes' => [ |
|
40
|
|
|
'class' => ['selected-item-container'], |
|
41
|
|
|
'data-entity-id' => $entity->id() |
|
42
|
|
|
], |
|
43
|
|
|
'display' => ['#markup' => $entity->label()], |
|
44
|
|
|
'remove_button' => [ |
|
45
|
|
|
'#type' => 'submit', |
|
46
|
|
|
'#value' => $this->t('Remove'), |
|
47
|
|
|
'#submit' => [[get_class($this), 'removeItemSubmit']], |
|
48
|
|
|
'#name' => 'remove_' . $entity->id(), |
|
49
|
|
|
'#attributes' => [ |
|
50
|
|
|
'data-row-id' => $id, |
|
51
|
|
|
'data-remove-entity' => 'items_' . $entity->id(), |
|
52
|
|
|
] |
|
53
|
|
|
], |
|
54
|
|
|
'weight' => [ |
|
55
|
|
|
'#type' => 'hidden', |
|
56
|
|
|
'#default_value' => $id, |
|
57
|
|
|
'#attributes' => ['class' => ['weight']] |
|
58
|
|
|
] |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
$form['use_selected'] = array( |
|
62
|
|
|
'#type' => 'submit', |
|
63
|
|
|
'#value' => t('Use selected'), |
|
64
|
|
|
'#name' => 'use_selected', |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
return $form; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Submit callback for remove buttons. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $form |
|
74
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function removeItemSubmit(array &$form, FormStateInterface $form_state) { |
|
77
|
|
|
$triggering_element = $form_state->getTriggeringElement(); |
|
78
|
|
|
|
|
79
|
|
|
// Remove weight of entity being removed. |
|
80
|
|
|
$form_state->unsetValue(['selected', $triggering_element['#attributes']['data-remove-entity']]); |
|
81
|
|
|
|
|
82
|
|
|
// Remove entity itself. |
|
83
|
|
|
$selected_entities = &$form_state->get(['entity_browser', 'selected_entities']); |
|
84
|
|
|
unset($selected_entities[$triggering_element['#attributes']['data-row-id']]); |
|
85
|
|
|
|
|
86
|
|
|
static::saveNewOrder($form_state); |
|
87
|
|
|
$form_state->setRebuild(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function submit(array &$form, FormStateInterface $form_state) { |
|
94
|
|
|
$this->saveNewOrder($form_state); |
|
95
|
|
|
if ($form_state->getTriggeringElement()['#name'] == 'use_selected') { |
|
96
|
|
|
$this->selectionDone($form_state); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Saves new ordering of entities based on weight. |
|
102
|
|
|
* |
|
103
|
|
|
* @param FormStateInterface $form_state |
|
104
|
|
|
* Form state. |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function saveNewOrder(FormStateInterface $form_state) { |
|
107
|
|
|
$selected = $form_state->getValue('selected'); |
|
108
|
|
|
if (!empty($selected)) { |
|
109
|
|
|
$weights = array_column($selected, 'weight'); |
|
110
|
|
|
$selected_entities = $form_state->get(['entity_browser', 'selected_entities']); |
|
111
|
|
|
|
|
112
|
|
|
// If we added new entities to the selection at this step we won't have |
|
113
|
|
|
// weights for them so we have to fake them. |
|
114
|
|
|
if (sizeof($weights) < sizeof($selected_entities)) { |
|
115
|
|
|
for ($new_weigth = (max($weights) + 1); $new_weigth < sizeof($selected_entities); $new_weigth++) { |
|
|
|
|
|
|
116
|
|
|
$weights[] = $new_weigth; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$ordered = array_combine($weights, $selected_entities); |
|
121
|
|
|
ksort($ordered); |
|
122
|
|
|
$form_state->set(['entity_browser', 'selected_entities'], $ordered); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: