This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paragraphs_inline_entity_form.module
75 lines (69 loc) · 2.42 KB
/
paragraphs_inline_entity_form.module
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
<?php
/**
* @file Module.
*/
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\entity_embed\Form\EntityEmbedDialog;
/**
* Implements hook_form_alter().
*/
function paragraphs_inline_entity_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/* @var Drupal\Core\Entity\FieldableEntityInterface $entity */
$formObject = $form_state->getFormObject();
if ($formObject instanceof EntityFormInterface) {
$form['#attached']['library'][] = 'paragraphs_inline_entity_form/dialog';
}
if ($formObject instanceof EntityEmbedDialog) {
if (paragraphs_inline_entity_form_entity_browser_is_paragraph_item($form)) {
$form['#attached']['library'][] = 'paragraphs_inline_entity_form/dialog';
switch ($form_state->get('step')) {
case 'select':
// do nothing.
break;
case 'embed':
// Change the back button to link to the paragraph edit form.
$entity_uuid = $form['attributes']['data-entity-uuid']['#value'];
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity = $entity_type_manager->getStorage('paragraph')
->loadByProperties(['uuid' => $entity_uuid]);
$paragraph = current($entity);
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => t('Edit paragraph'),
'#submit' => [],
'#attributes' => [
'onclick' => [
'window.ckeditorSaveCallback=Drupal.ckeditor.saveCallback'
],
],
'#ajax' => [
'url' => Url::fromRoute('entity_browser.edit_form', [
'entity_type' => 'paragraph',
'entity' => $paragraph->id(),
]),
'event' => 'click',
],
];
break;
}
}
}
}
/**
* Checks that the browser type is a paragraph_item.
*
* @param $form The form.
* @return TRUE If it is a paragraph_item entity browser.
*/
function paragraphs_inline_entity_form_entity_browser_is_paragraph_item($form) {
if (isset($form['entity_browser']['#entity_browser'])
&& $form['entity_browser']['#entity_browser'] == 'paragraph_items') {
return TRUE;
}
if (isset($form['attributes']['data-embed-button'])
&& $form['attributes']['data-embed-button']['#value'] == 'paragraphs_inline_entity_form') {
return TRUE;
}
}