This repository was archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathEntityEmbedDialog.php
More file actions
572 lines (522 loc) · 21.2 KB
/
EntityEmbedDialog.php
File metadata and controls
572 lines (522 loc) · 21.2 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
/**
* @file
* Contains \Drupal\entity_embed\Form\EntityEmbedDialog.
*/
namespace Drupal\entity_embed\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\editor\EditorInterface;
use Drupal\embed\EmbedButtonInterface;
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
use Drupal\entity_embed\EntityHelperTrait;
use Drupal\Component\Serialization\Json;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form to embed entities by specifying data attributes.
*/
class EntityEmbedDialog extends FormBase {
use EntityHelperTrait;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs a EntityEmbedDialog object.
*
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager
* The Module Handler.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The Form Builder.
*/
public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder) {
$this->setDisplayPluginManager($plugin_manager);
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity_embed.display'),
$container->get('form_builder')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_embed_dialog';
}
/**
* {@inheritdoc}
*
* @param \Drupal\editor\EditorInterface $editor
* The editor to which this dialog corresponds.
* @param \Drupal\embed\EmbedButtonInterface $embed_button
* The URL button to which this dialog corresponds.
*/
public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) {
$values = $form_state->getValues();
$input = $form_state->getUserInput();
// Set embed button element in form state, so that it can be used later in
// validateForm() function.
$form_state->set('embed_button', $embed_button);
$form_state->set('editor', $editor);
// Initialize entity element with form attributes, if present.
$entity_element = empty($values['attributes']) ? array() : $values['attributes'];
// The default values are set directly from \Drupal::request()->request,
// provided by the editor plugin opening the dialog.
if (!$form_state->get('entity_element')) {
$form_state->set('entity_element', isset($input['editor_object']) ? $input['editor_object'] : array());
}
$entity_element += $form_state->get('entity_element');
$entity_element += array(
'data-entity-type' => $embed_button->getTypeSetting('entity_type'),
'data-entity-uuid' => '',
'data-entity-id' => '',
'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view',
'data-entity-embed-settings' => array(),
'data-align' => '',
);
$form_state->set('entity_element', $entity_element);
$form_state->set('entity', $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid'] ?: $entity_element['data-entity-id']));
if (!$form_state->get('step')) {
// If an entity has been selected, then always skip to the embed options.
if ($form_state->get('entity')) {
$form_state->set('step', 'embed');
}
else {
$form_state->set('step', 'select');
}
}
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
$form['#attached']['library'][] = 'entity_embed/drupal.entity_embed.dialog';
$form['#prefix'] = '<div id="entity-embed-dialog-form">';
$form['#suffix'] = '</div>';
if ($form_state->get('step') == 'select') {
$form = $this->buildSelectStep($form, $form_state);
}
else {
$form = $this->buildEmbedStep($form, $form_state);
}
return $form;
}
/**
* Form constructor for the entity selection step.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildSelectStep(array &$form, FormStateInterface $form_state) {
$entity_element = $form_state->get('entity_element');
/** @var \Drupal\embed\EmbedButtonInterface $embed_button */
$embed_button = $form_state->get('embed_button');
$entity = $form_state->get('entity');
$form['attributes']['data-entity-type'] = array(
'#type' => 'value',
'#value' => $entity_element['data-entity-type'],
);
$label = $this->t('Label');
// Attempt to display a better label if we can by getting it from
// the label field definition.
$entity_type = $this->entityManager()->getDefinition($entity_element['data-entity-type']);
if ($entity_type->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface') && $entity_type->hasKey('label')) {
$field_definitions = $this->entityManager()->getBaseFieldDefinitions($entity_type->id());
if (isset($field_definitions[$entity_type->getKey('label')])) {
$label = $field_definitions[$entity_type->getKey('label')]->getLabel();
}
}
$form['attributes']['data-entity-id'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => $entity_element['data-entity-type'],
'#selection_settings' => array(
'target_bundles' => $embed_button->getTypeSetting('bundles'),
),
'#title' => $label,
'#default_value' => $entity,
'#required' => TRUE,
'#description' => $this->t('Type label and pick the right one from suggestions. Note that the unique ID will be saved.'),
);
$form['attributes']['data-entity-uuid'] = array(
'#type' => 'value',
'#title' => $entity_element['data-entity-uuid'],
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['save_modal'] = array(
'#type' => 'submit',
'#value' => $this->t('Next'),
// No regular submit-handler. This form only works via JavaScript.
'#submit' => array(),
'#ajax' => array(
'callback' => '::submitSelectStep',
'event' => 'click',
),
);
return $form;
}
/**
* Form constructor for the entity embedding step.
*
* @todo Re-add caption attribute.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildEmbedStep(array $form, FormStateInterface $form_state) {
$entity_element = $form_state->get('entity_element');
/** @var \Drupal\embed\EmbedButtonInterface $embed_button */
$embed_button = $form_state->get('embed_button');
/** @var \Drupal\editor\EditorInterface $editor */
$editor = $form_state->get('editor');
$entity = $form_state->get('entity');
$values = $form_state->getValues();
$entity_label = '';
try {
$entity_label = $entity->link();
}
catch (\Exception $e) {
// Construct markup of the link to the entity manually if link() fails.
// @see https://www.drupal.org/node/2402533
$entity_label = '<a href="' . $entity->url() . '">' . $entity->label() . '</a>';
}
$form['entity'] = array(
'#type' => 'item',
'#title' => $this->t('Selected entity'),
'#markup' => $entity_label,
);
$form['attributes']['data-entity-type'] = array(
'#type' => 'value',
'#value' => $entity_element['data-entity-type'],
);
$form['attributes']['data-entity-id'] = array(
'#type' => 'value',
'#value' => $entity_element['data-entity-id'],
);
$form['attributes']['data-entity-uuid'] = array(
'#type' => 'value',
'#value' => $entity_element['data-entity-uuid'],
);
// Build the list of allowed display plugins.
$display_plugin_options = $this->getDisplayPluginOptions($embed_button, $entity);
// If the currently selected display is not in the available options,
// use the first from the list instead. This can happen if an alter
// hook customizes the list based on the entity.
if (!isset($display_plugin_options[$entity_element['data-entity-embed-display']])) {
$entity_element['data-entity-embed-display'] = key($display_plugin_options);
}
// The default display plugin has been deprecated by the rendered entity
// field formatter.
if ($entity_element['data-entity-embed-display'] === 'default') {
$entity_element['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
}
$form['attributes']['data-entity-embed-display'] = array(
'#type' => 'select',
'#title' => $this->t('Display as'),
'#options' => $display_plugin_options,
'#default_value' => $entity_element['data-entity-embed-display'],
'#required' => TRUE,
'#ajax' => array(
'callback' => '::updatePluginConfigurationForm',
'wrapper' => 'data-entity-embed-settings-wrapper',
'effect' => 'fade',
),
// Hide the selection if only one option is available.
'#access' => count($display_plugin_options) > 1,
);
$form['attributes']['data-entity-embed-settings'] = array(
'#type' => 'container',
'#prefix' => '<div id="data-entity-embed-settings-wrapper">',
'#suffix' => '</div>',
);
$form['attributes']['data-embed-button'] = array(
'#type' => 'value',
'#value' => $embed_button->id(),
);
$form['attributes']['data-entity-label'] = array(
'#type' => 'value',
'#value' => $embed_button->label(),
);
$plugin_id = !empty($values['attributes']['data-entity-embed-display']) ? $values['attributes']['data-entity-embed-display'] : $entity_element['data-entity-embed-display'];
if (!empty($plugin_id)) {
if (is_string($entity_element['data-entity-embed-settings'])) {
$entity_element['data-entity-embed-settings'] = Json::decode($entity_element['data-entity-embed-settings'], TRUE);
}
$display = $this->displayPluginManager()->createInstance($plugin_id, $entity_element['data-entity-embed-settings']);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$form['attributes']['data-entity-embed-settings'] += $display->buildConfigurationForm($form, $form_state);
}
// When Drupal core's filter_align is being used, the text editor may
// offer the ability to change the alignment.
if (isset($entity_element['data-align']) && $editor->getFilterFormat()->filters('filter_align')->status) {
$form['attributes']['data-align'] = array(
'#title' => $this->t('Align'),
'#type' => 'radios',
'#options' => array(
'none' => $this->t('None'),
'left' => $this->t('Left'),
'center' => $this->t('Center'),
'right' => $this->t('Right'),
),
'#default_value' => $entity_element['data-align'] === '' ? 'none' : $entity_element['data-align'],
'#wrapper_attributes' => array('class' => array('container-inline')),
'#attributes' => array('class' => array('container-inline')),
'#parents' => array('attributes', 'data-align'),
);
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['back'] = array(
'#type' => 'submit',
'#value' => $this->t('Back'),
// No regular submit-handler. This form only works via JavaScript.
'#submit' => array(),
'#ajax' => array(
'callback' => '::goBack',
'event' => 'click',
),
);
$form['actions']['save_modal'] = array(
'#type' => 'submit',
'#value' => $this->t('Embed'),
// No regular submit-handler. This form only works via JavaScript.
'#submit' => array(),
'#ajax' => array(
'callback' => '::submitEmbedStep',
'event' => 'click',
),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form_state->get('step') == 'select') {
$this->validateSelectStep($form, $form_state);
}
else {
$this->validateEmbedStep($form, $form_state);
}
}
/**
* Form validation handler for the entity selection step.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function validateSelectStep(array $form, FormStateInterface $form_state) {
$values = $form_state->getValues();
if ($entity_type = $values['attributes']['data-entity-type']) {
$id = trim($values['attributes']['data-entity-id']);
if ($entity = $this->loadEntity($entity_type, $id)) {
if (!$entity->access('view')) {
$form_state->setError($form['attributes']['data-entity-id'], $this->t('Unable to access @type entity @id.', array('@type' => $entity_type, '@id' => $id)));
}
else {
$form_state->setValueForElement($form['attributes']['data-entity-id'], $entity->id());
if ($uuid = $entity->uuid()) {
$form_state->setValueForElement($form['attributes']['data-entity-uuid'], $uuid);
}
else {
$form_state->setValueForElement($form['attributes']['data-entity-uuid'], '');
}
// Ensure that at least one display plugin is present before
// proceeding to the next step. Rasie an error otherwise.
$embed_button = $form_state->get('embed_button');
$display_plugin_options = $this->getDisplayPluginOptions($embed_button, $entity);
// If no plugin is available after taking the intersection,
// raise error. Also log an exception.
if (empty($display_plugin_options)) {
$form_state->setError($form['attributes']['data-entity-id'], $this->t('No display options available for the selected entity. Please select another entity.'));
$this->logger('entity_embed')->warning('No display options available for "@type:" entity "@id" while embedding using button "@button". Please ensure that at least one display plugin is allowed for this embed button which is available for this entity.', array('@type' => $entity_type, '@id' => $entity->id(), '@button' => $embed_button->id()));
}
}
}
else {
$form_state->setError($form['attributes']['data-entity-id'], $this->t('Unable to load @type entity @id.', array('@type' => $entity_type, '@id' => $id)));
}
}
}
/**
* Form validation handler for the entity embedding step.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function validateEmbedStep(array $form, FormStateInterface $form_state) {
// Validate configuration forms for the display plugin used.
$entity_element = $form_state->getValue('attributes');
$entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']);
$plugin_id = $entity_element['data-entity-embed-display'];
$plugin_settings = $entity_element['data-entity-embed-settings'] ?: array();
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$display->validateConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {}
/**
* Form submission handler to update the plugin configuration form.
*
* @param array $form
* The build form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function updatePluginConfigurationForm(array &$form, FormStateInterface $form_state) {
return $form['attributes']['data-entity-embed-settings'];
}
/**
* Form submission handler to go back to the previous step of the form.
*
* @param array $form
* The build form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The ajax response.
*/
public function goBack(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$form_state->setStorage(array('step' => 'select'));
$form_state->setRebuild(TRUE);
$rebuild_form = $this->formBuilder->rebuildForm('entity_embed_dialog', $form_state, $form);
unset($rebuild_form['#prefix'], $rebuild_form['#suffix']);
$response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $rebuild_form));
return $response;
}
/**
* Form submission handler for the entity selection step.
*
* On success will send the user to the next step of the form to select the
* embed display settings. On form errors, this will rebuild the form and
* display the error messages.
*
* @param array $form
* The build form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The ajax response.
*/
public function submitSelectStep(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Display errors in form, if any.
if ($form_state->hasAnyErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = array(
'#type' => 'status_messages',
'#weight' => -10,
);
$response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $form));
}
else {
$form_state->setStorage(array('step' => 'embed'));
$form_state->setRebuild(TRUE);
$rebuild_form = $this->formBuilder->rebuildForm('entity_embed_dialog', $form_state, $form);
unset($rebuild_form['#prefix'], $rebuild_form['#suffix']);
$response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $rebuild_form));
}
return $response;
}
/**
* Form submission handler for the entity embedding step.
*
* On success this will submit the command to save the embedded entity with
* the configured display settings to the WYSIWYG element, and then close the
* modal dialog. On form errors, this will rebuild the form and display the
* error messages.
*
* @param array $form
* An associative array containing the structure of the form.
* @param FormStateInterface $form_state
* An associative array containing the current state of the form.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The ajax response.
*/
public function submitEmbedStep(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Submit configuration form the selected display plugin.
$entity_element = $form_state->getValue('attributes');
$entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']);
$plugin_id = $entity_element['data-entity-embed-display'];
$plugin_settings = $entity_element['data-entity-embed-settings'] ?: array();
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$display->submitConfigurationForm($form, $form_state);
$values = $form_state->getValues();
// Display errors in form, if any.
if ($form_state->hasAnyErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = array(
'#type' => 'status_messages',
'#weight' => -10,
);
$response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $form));
}
else {
// Serialize entity embed settings to JSON string.
if (!empty($values['attributes']['data-entity-embed-settings'])) {
$values['attributes']['data-entity-embed-settings'] = Json::encode($values['attributes']['data-entity-embed-settings']);
}
$response->addCommand(new EditorDialogSave($values));
$response->addCommand(new CloseModalDialogCommand());
}
return $response;
}
/**
* Returns the allowed display plugins given an embed button and an entity.
*
* @param \Drupal\embed\EmbedButtonInterface $embed_button
* The embed button.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return array
* List of allowed display plugins.
*/
public function getDisplayPluginOptions(EmbedButtonInterface $embed_button, EntityInterface $entity) {
$plugins = $this->displayPluginManager->getDefinitionOptionsForEntity($entity);
if ($allowed_plugins = $embed_button->getTypeSetting('display_plugins')) {
$plugins = array_intersect_key($plugins, array_flip($allowed_plugins));
}
//natsort($plugins);
return $plugins;
}
}