1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Ajax\AjaxResponse; |
6
|
|
|
use Drupal\Core\Ajax\ReplaceCommand; |
7
|
|
|
use Drupal\Core\Entity\EntityInterface; |
8
|
|
|
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
9
|
|
|
use Drupal\Core\Entity\EntityTypeInterface; |
10
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
11
|
|
|
use Drupal\Core\Form\FormStateInterface; |
12
|
|
|
use Drupal\entity_browser\WidgetBase; |
13
|
|
|
use Drupal\entity_browser\WidgetValidationManager; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Drupal\Core\Entity\EntityDisplayRepositoryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Uses a view to provide entity listing in a browser's widget. |
20
|
|
|
* |
21
|
|
|
* @EntityBrowserWidget( |
22
|
|
|
* id = "entity_form", |
23
|
|
|
* label = @Translation("Entity form"), |
24
|
|
|
* description = @Translation("Provides entity form widget."), |
25
|
|
|
* auto_select = FALSE |
26
|
|
|
* ) |
27
|
|
|
*/ |
28
|
|
|
class EntityForm extends WidgetBase { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The entity type bundle info service. |
32
|
|
|
* |
33
|
|
|
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
34
|
|
|
*/ |
35
|
|
|
protected $entityTypeBundleInfo; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The entity display repository. |
39
|
|
|
* |
40
|
|
|
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected $entityDisplayRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructs widget plugin. |
46
|
|
|
* |
47
|
|
|
* @param array $configuration |
48
|
|
|
* A configuration array containing information about the plugin instance. |
49
|
|
|
* @param string $plugin_id |
50
|
|
|
* The plugin_id for the plugin instance. |
51
|
|
|
* @param mixed $plugin_definition |
52
|
|
|
* The plugin implementation definition. |
53
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
54
|
|
|
* Event dispatcher service. |
55
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
56
|
|
|
* The entity type manager service. |
57
|
|
|
* @param \Drupal\entity_browser\WidgetValidationManager $validation_manager |
58
|
|
|
* The Widget Validation Manager service. |
59
|
|
|
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
60
|
|
|
* The entity type bundle info service. |
61
|
|
|
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository |
62
|
|
|
* The entity display repository. |
63
|
|
|
*/ |
64
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository) { |
65
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager); |
66
|
|
|
$this->entityTypeBundleInfo = $entity_type_bundle_info; |
67
|
|
|
$this->entityDisplayRepository = $entity_display_repository; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|
|
74
|
|
|
return new static( |
75
|
|
|
$configuration, |
76
|
|
|
$plugin_id, |
77
|
|
|
$plugin_definition, |
78
|
|
|
$container->get('event_dispatcher'), |
79
|
|
|
$container->get('entity_type.manager'), |
80
|
|
|
$container->get('plugin.manager.entity_browser.widget_validation'), |
81
|
|
|
$container->get('entity_type.bundle.info'), |
82
|
|
|
$container->get('entity_display.repository') |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function defaultConfiguration() { |
90
|
|
|
return [ |
91
|
|
|
'entity_type' => NULL, |
92
|
|
|
'bundle' => NULL, |
93
|
|
|
'form_mode' => 'default', |
94
|
|
|
'submit_text' => $this->t('Save entity'), |
95
|
|
|
] + parent::defaultConfiguration(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { |
102
|
|
|
if (empty($this->configuration['entity_type']) || empty($this->configuration['bundle']) || empty($this->configuration['form_mode'])) { |
103
|
|
|
return ['#markup' => $this->t('The settings for this widget (Entity type, Bundle or Form mode) are not configured correctly.')]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters); |
107
|
|
|
|
108
|
|
|
// Pretend to be IEFs submit button. |
109
|
|
|
$form['#submit'] = [['Drupal\inline_entity_form\ElementSubmit', 'trigger']]; |
110
|
|
|
$form['actions']['submit']['#ief_submit_trigger'] = TRUE; |
111
|
|
|
$form['actions']['submit']['#ief_submit_trigger_all'] = TRUE; |
112
|
|
|
|
113
|
|
|
$form['inline_entity_form'] = [ |
114
|
|
|
'#type' => 'inline_entity_form', |
115
|
|
|
'#op' => 'add', |
116
|
|
|
'#entity_type' => $this->configuration['entity_type'], |
117
|
|
|
'#bundle' => $this->configuration['bundle'], |
118
|
|
|
'#form_mode' => $this->configuration['form_mode'], |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
return $form; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
protected function prepareEntities(array $form, FormStateInterface $form_state) { |
128
|
|
|
return [$form[$form['#browser_parts']['widget']]['inline_entity_form']['#entity']]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
135
|
|
|
if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) { |
136
|
|
|
$entities = $this->prepareEntities($form, $form_state); |
137
|
|
|
array_walk( |
138
|
|
|
$entities, |
139
|
|
|
function (EntityInterface $entity) { |
140
|
|
|
$entity->save(); |
141
|
|
|
} |
142
|
|
|
); |
143
|
|
|
$this->selectEntities($entities, $form_state); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
151
|
|
|
$form = parent::buildConfigurationForm($form, $form_state); |
152
|
|
|
|
153
|
|
|
$parents = ['table', $this->uuid(), 'form']; |
154
|
|
|
$entity_type = $form_state->hasValue(array_merge($parents, ['entity_type'])) ? $form_state->getValue(array_merge($parents, ['entity_type'])) : $this->configuration['entity_type']; |
155
|
|
|
$bundle = $form_state->hasValue(array_merge($parents, ['bundle', 'select'])) ? $form_state->getValue(array_merge($parents, ['bundle', 'select'])) : $this->configuration['bundle']; |
156
|
|
|
$form_mode = $form_state->hasValue(array_merge($parents, ['form_mode', 'form_select'])) ? $form_state->hasValue(array_merge($parents, ['form_mode', 'form_select'])) : $this->configuration['form_mode']; |
157
|
|
|
|
158
|
|
|
$definitions = $this->entityTypeManager->getDefinitions(); |
159
|
|
|
$entity_types = array_combine( |
160
|
|
|
array_keys($definitions), |
161
|
|
|
array_map(function (EntityTypeInterface $item) { |
162
|
|
|
return $item->getLabel(); |
163
|
|
|
}, $definitions) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$form['entity_type'] = [ |
167
|
|
|
'#type' => 'select', |
168
|
|
|
'#title' => $this->t('Entity type'), |
169
|
|
|
'#options' => $entity_types, |
170
|
|
|
'#default_value' => $entity_type, |
171
|
|
|
'#ajax' => [ |
172
|
|
|
'callback' => [$this, 'updateFormElements'], |
173
|
|
|
], |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
$bundles = []; |
177
|
|
|
if ($entity_type) { |
178
|
|
|
$definitions = $this->entityTypeBundleInfo->getBundleInfo($entity_type); |
179
|
|
|
$bundles = array_map(function ($item) { |
180
|
|
|
return $item['label']; |
181
|
|
|
}, $definitions); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$form['bundle'] = [ |
185
|
|
|
'#type' => 'container', |
186
|
|
|
'select' => [ |
187
|
|
|
'#type' => 'select', |
188
|
|
|
'#title' => $this->t('Bundle'), |
189
|
|
|
'#options' => $bundles, |
190
|
|
|
'#default_value' => $bundle, |
191
|
|
|
], |
192
|
|
|
'#attributes' => ['id' => 'bundle-wrapper-' . $this->uuid()], |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$form['form_mode'] = [ |
196
|
|
|
'#type' => 'container', |
197
|
|
|
'form_select' => [ |
198
|
|
|
'#type' => 'select', |
199
|
|
|
'#title' => $this->t('Form mode'), |
200
|
|
|
'#default_value' => $form_mode, |
201
|
|
|
'#options' => $this->entityDisplayRepository->getFormModeOptions($entity_type), |
202
|
|
|
], |
203
|
|
|
'#attributes' => ['id' => 'form-mode-wrapper-' . $this->uuid()], |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
return $form; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* AJAX callback for bundle dropdown update. |
211
|
|
|
*/ |
212
|
|
|
public function updateBundle($form, FormStateInterface $form_state) { |
213
|
|
|
return $form['widgets']['table'][$this->uuid()]['form']['bundle']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* AJAX callback for the Form Mode dropdown update. |
218
|
|
|
*/ |
219
|
|
|
public function updateFormMode($form, FormStateInterface $form_state) { |
220
|
|
|
return $form['widgets']['table'][$this->uuid()]['form']['form_mode']; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* AJAX callback to update the two form elements: bundle and form_mode. |
225
|
|
|
*/ |
226
|
|
|
public function updateFormElements($form, FormStateInterface $form_state) { |
227
|
|
|
$response = new AjaxResponse(); |
228
|
|
|
$response->addCommand(new ReplaceCommand('#bundle-wrapper-' . $this->uuid(), $this->updateBundle($form, $form_state))); |
229
|
|
|
$response->addCommand(new ReplaceCommand('#form-mode-wrapper-' . $this->uuid(), $this->updateFormMode($form, $form_state))); |
230
|
|
|
return $response; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
237
|
|
|
parent::submitConfigurationForm($form, $form_state); |
238
|
|
|
$this->configuration['bundle'] = $this->configuration['bundle']['select']; |
239
|
|
|
$this->configuration['form_mode'] = $this->configuration['form_mode']['form_select']; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function access() { |
246
|
|
|
return $this->entityTypeManager |
247
|
|
|
->getAccessControlHandler($this->configuration['entity_type']) |
248
|
|
|
->createAccess($this->configuration['bundle'], NULL, [], TRUE); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.