1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains \Drupal\entity_browser\Form\EntityBrowserForm. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\entity_browser\Form; |
9
|
|
|
|
10
|
|
|
use Drupal\Core\Entity\EntityForm; |
11
|
|
|
use Drupal\Core\Entity\EntityInterface; |
12
|
|
|
use Drupal\Core\Form\FormBase; |
13
|
|
|
use Drupal\Core\Form\FormStateInterface; |
14
|
|
|
use Drupal\entity_browser\DisplayAjaxInterface; |
15
|
|
|
use Drupal\entity_browser\EntityBrowserFormInterface; |
16
|
|
|
use Drupal\entity_browser\EntityBrowserInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The entity browser form. |
20
|
|
|
*/ |
21
|
|
|
class EntityBrowserForm extends FormBase implements EntityBrowserFormInterface { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The entity browser object. |
25
|
|
|
* |
26
|
|
|
* @var \Drupal\entity_browser\EntityBrowserInterface |
27
|
|
|
*/ |
28
|
|
|
protected $entity_browser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getFormId() { |
34
|
|
|
return 'entity_browser_' . $this->entity_browser->id() . '_form'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function setEntityBrowser(EntityBrowserInterface $entity_browser) { |
41
|
|
|
$this->entity_browser = $entity_browser; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initializes form state. |
46
|
|
|
* |
47
|
|
|
* @param \Drupal\Core\Form\FormStateInterface |
48
|
|
|
* Form state object. |
49
|
|
|
*/ |
50
|
|
|
protected function init(FormStateInterface $form_state) { |
51
|
|
|
// Flag that this form has been initialized. |
52
|
|
|
$form_state->set('entity_form_initialized', TRUE); |
53
|
|
|
$form_state->set(['entity_browser', 'instance_uuid'], \Drupal::service('uuid')->generate()); |
54
|
|
|
$form_state->set(['entity_browser', 'selected_entities'], []); |
55
|
|
|
$form_state->set(['entity_browser', 'selection_completed'], FALSE); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function buildForm(array $form, FormStateInterface $form_state) { |
62
|
|
|
// During the initial form build, add this form object to the form state and |
63
|
|
|
// allow for initial preparation before form building and processing. |
64
|
|
|
if (!$form_state->has('entity_form_initialized')) { |
65
|
|
|
$this->init($form_state); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$form['#attributes']['class'][] = 'entity-browser-form'; |
69
|
|
|
$form['#browser_parts'] = [ |
70
|
|
|
'widget_selector' => 'widget_selector', |
71
|
|
|
'widget' => 'widget', |
72
|
|
|
'selection_display' => 'selection_display', |
73
|
|
|
]; |
74
|
|
|
$this->entity_browser |
75
|
|
|
->getWidgetSelector() |
76
|
|
|
->setDefaultWidget($this->getCurrentWidget($form_state)); |
77
|
|
|
$form[$form['#browser_parts']['widget_selector']] = $this->entity_browser |
78
|
|
|
->getWidgetSelector() |
79
|
|
|
->getForm($form, $form_state); |
80
|
|
|
$form[$form['#browser_parts']['widget']] = $this->entity_browser |
81
|
|
|
->getWidgets() |
82
|
|
|
->get($this->getCurrentWidget($form_state)) |
83
|
|
|
->getForm($form, $form_state, $this->entity_browser->getAdditionalWidgetParameters()); |
84
|
|
|
|
85
|
|
|
$form['actions'] = [ |
86
|
|
|
'submit' => [ |
87
|
|
|
'#type' => 'submit', |
88
|
|
|
'#value' => t('Select'), |
89
|
|
|
'#attributes' => [ |
90
|
|
|
'class' => ['is-entity-browser-submit'], |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$form[$form['#browser_parts']['selection_display']] = $this->entity_browser |
96
|
|
|
->getSelectionDisplay() |
97
|
|
|
->getForm($form, $form_state); |
98
|
|
|
|
99
|
|
|
if ($this->entity_browser->getDisplay() instanceOf DisplayAjaxInterface) { |
100
|
|
|
$this->entity_browser->getDisplay()->addAjax($form); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $form; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function validateForm(array &$form, FormStateInterface $form_state) { |
110
|
|
|
$this->entity_browser->getWidgetSelector()->validate($form, $form_state); |
111
|
|
|
$this->entity_browser->getWidgets()->get($this->getCurrentWidget($form_state))->validate($form, $form_state); |
112
|
|
|
$this->entity_browser->getSelectionDisplay()->validate($form, $form_state); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
119
|
|
|
$original_widget = $this->getCurrentWidget($form_state); |
120
|
|
|
if ($new_widget = $this->entity_browser->getWidgetSelector()->submit($form, $form_state)) { |
121
|
|
|
$this->setCurrentWidget($new_widget, $form_state); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Only call widget submit if we didn't change the widget. |
125
|
|
|
if ($original_widget == $this->getCurrentWidget($form_state)) { |
126
|
|
|
$this->entity_browser |
127
|
|
|
->getWidgets() |
128
|
|
|
->get($this->getCurrentWidget($form_state)) |
129
|
|
|
->submit($form[$form['#browser_parts']['widget']], $form, $form_state); |
130
|
|
|
|
131
|
|
|
$this->entity_browser |
132
|
|
|
->getSelectionDisplay() |
133
|
|
|
->submit($form, $form_state); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!$this->isSelectionCompleted($form_state)) { |
137
|
|
|
$form_state->setRebuild(); |
138
|
|
|
} |
139
|
|
|
else { |
140
|
|
|
$this->entity_browser->getDisplay()->selectionCompleted($this->getSelectedEntities($form_state)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the widget that is currently selected. |
146
|
|
|
* |
147
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
148
|
|
|
* The current state of the form. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
* ID of currently selected widget. |
152
|
|
|
*/ |
153
|
|
|
protected function getCurrentWidget(FormStateInterface $form_state) { |
154
|
|
|
// Do not use has() as that returns TRUE if the value is NULL. |
155
|
|
|
if (!$form_state->get('entity_browser_current_widget')) { |
156
|
|
|
$form_state->set('entity_browser_current_widget', $this->entity_browser->getFirstWidget()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $form_state->get('entity_browser_current_widget'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Sets widget that is currently active. |
164
|
|
|
* |
165
|
|
|
* @param string $widget |
166
|
|
|
* New active widget UUID. |
167
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
168
|
|
|
* Form state. |
169
|
|
|
*/ |
170
|
|
|
protected function setCurrentWidget($widget, FormStateInterface $form_state) { |
171
|
|
|
$form_state->set('entity_browser_current_widget', $widget); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Indicates selection is done. |
176
|
|
|
* |
177
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
178
|
|
|
* Form state. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
* Indicates selection is done. |
182
|
|
|
*/ |
183
|
|
|
protected function isSelectionCompleted(FormStateInterface $form_state) { |
184
|
|
|
return (bool) $form_state->get(['entity_browser', 'selection_completed']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns currently selected entities. |
189
|
|
|
* |
190
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
191
|
|
|
* Form state. |
192
|
|
|
* |
193
|
|
|
* @return \Drupal\Core\Entity\EntityInterface[] |
194
|
|
|
* Array of currently selected entities. |
195
|
|
|
*/ |
196
|
|
|
protected function getSelectedEntities(FormStateInterface $form_state) { |
197
|
|
|
return $form_state->get(['entity_browser', 'selected_entities']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|