Passed
Branch feature/linting (138974)
by Christopher
02:18
created

ParagraphEntityForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 164
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A form() 0 7 1
A actions() 0 23 1
A ajaxSubmit() 0 12 1
A bootstrapContext() 0 10 3
A save() 0 10 1
B __construct() 0 24 1
1
<?php
2
3
namespace Drupal\paragraphs_editor\Form;
4
5
use Drupal\Core\Ajax\AjaxResponse;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Ajax\AjaxResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Entity\ContentEntityForm;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\ContentEntityForm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Entity\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Core\Extension\ModuleHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Extension\ModuleHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\Core\Form\FormStateInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Form\FormStateInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface;
12
use Drupal\paragraphs_editor\EditorCommand\CommandContextInterface;
13
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerInterface;
14
15
/**
16
 * The form that is shown for editing paragraph entities in ckeditor.
17
 *
18
 * This is basically just the core content entity form with a few overrides to
19
 * ajaxify the experience and integrate with the delivery provider plugin
20
 * system.
21
 */
22
class ParagraphEntityForm extends ContentEntityForm {
23
24
  /**
25
   * The context the editor command is being executed in.
26
   *
27
   * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
28
   */
29
  protected $context;
30
31
  /**
32
   * The buffer item being edited by this form.
33
   *
34
   * @var \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface
35
   */
36
  protected $bufferItem;
37
38
  /**
39
   * The widget binder data compiler service.
40
   *
41
   * @var \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerInterface
42
   */
43
  protected $dataCompiler;
44
45
  /**
46
   * Creates a ParagraphEntityForm object.
47
   *
48
   * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
49
   *   The context of the command that is invoking this form.
50
   * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface $item
51
   *   An editor item (wrapped paragraph entity) to show the edit form for.
52
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53
   *   The module handler service.
54
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
55
   *   The entity type manager service.
56
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
57
   *   The entity manager service.
58
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerInterface $data_compiler
59
   *   The widget binder data compiler service.
60
   */
61
  public function __construct(CommandContextInterface $context,
62
    EditBufferItemInterface $item,
63
    ModuleHandlerInterface $module_handler,
64
    EntityTypeManagerInterface $entity_type_manager,
65
    EntityManagerInterface $entity_manager,
66
    WidgetBinderDataCompilerInterface $data_compiler) {
67
68
    // The ContentEntityForm class actually has a whole bunch of hidden
69
    // dependendencies. They are injected by core via setters, however we
70
    // explicitly use constructor injection here to make them clear. I realize
71
    // this is ugly, but at least its clear (I hope).
72
    // The code we rely on still uses the old (deprecated) EntityManager. We
73
    // will rely on the standard EntityTypeManager within this class.
74
    parent::__construct($entity_manager);
75
76
    // Inject dependencies for parent classes.
77
    $this->setEntityTypeManager($entity_type_manager);
78
    $this->setModuleHandler($module_handler);
79
    $this->setEntity($item->getEntity());
80
81
    // Set dependencies for this class.
82
    $this->context = $context;
83
    $this->bufferItem = $item;
84
    $this->dataCompiler = $data_compiler;
85
  }
86
87
  /**
88
   * Rebuilds the additional temporary context key value pairs in the context.
89
   *
90
   * @param \Drupal\Core\Form\FormStateInterface $form_state
91
   *   The form state to bootstrap the additional context from.
92
   *
93
   * @return \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
94
   *   The bootstrapped context.
95
   */
96
  protected function bootstrapContext(FormStateInterface $form_state) {
97
    $saved = $form_state->getValue('paragraphs_editor_additional_context');
98
    if ($saved) {
99
      $saved = unserialize($saved);
100
      foreach ($saved as $key => $value) {
101
        $this->context->addAdditionalContext($key, $value);
102
      }
103
    }
104
105
    return $this->context;
106
  }
107
108
  /**
109
   * {@inheritdoc}
110
   */
111
  public function form(array $form, FormStateInterface $form_state) {
112
    $form = parent::form($form, $form_state);
113
    $form['paragraphs_editor_additional_context'] = [
114
      '#type' => 'hidden',
115
      '#default_value' => serialize($this->bootstrapContext($form_state)->getAdditionalContext()),
116
    ];
117
    return $form;
118
  }
119
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function save(array $form, FormStateInterface $form_state) {
124
    $context = $this->bootstrapContext($form_state);
125
126
    // Save the changes to the editor buffer.
127
    $this->bufferItem->overwrite($this->entity);
128
    $this->bufferItem->save();
129
130
    // Make properties available to the static ajax handler.
131
    $form_state->setTemporaryValue(['paragraphs_editor', 'data'], $this->dataCompiler->compile($context, $this->bufferItem));
132
    $form_state->setTemporaryValue(['paragraphs_editor', 'context'], $this->context);
133
  }
134
135
  /**
136
   * {@inheritdoc}
137
   */
138
  protected function actions(array $form, FormStateInterface $form_state) {
139
    $actions = parent::actions($form, $form_state);
140
141
    // Make the default entity save button submit via ajax.
142
    $actions['submit']['#ajax'] = [
143
      'callback' => [get_class($this), 'ajaxSubmit'],
144
    ];
145
146
    // Provide a cancel link for users to cancel the edit operation.
147
    $url = $this->context->createCommandUrl('cancel');
148
    $actions['cancel'] = [
149
      '#type' => 'button',
150
      '#value' => $this->t('Cancel'),
151
      '#weight' => 10,
152
      '#ajax' => [
153
        'url' => $url,
154
        'options' => $url->getOptions(),
155
      ],
156
    ];
157
158
    unset($actions['delete']);
159
160
    return $actions;
161
  }
162
163
  /**
164
   * Handles submissions via ajax.
165
   *
166
   * @param array $form
167
   *   The complete form render array.
168
   * @param \Drupal\Core\Form\FormStateInterface $form_state
169
   *   The associated form state.
170
   *
171
   * @return \Drupal\Core\Ajax\AjaxResponse
172
   *   An ajax response object that delivers a rendered paragraph.
173
   */
174
  public static function ajaxSubmit(array $form, FormStateInterface $form_state) {
175
    // Retrieve class mambers needed to build a response.
176
    $data = $form_state->getTemporaryValue(['paragraphs_editor', 'data']);
177
    $delivery = $form_state->getTemporaryValue(['paragraphs_editor', 'context'])->getPlugin('delivery_provider');
178
179
    // Create a response that includes the rendered paragraph and signal that
180
    // the ajax workflow is completed.
181
    $response = new AjaxResponse();
182
    $delivery->sendData($response, $data);
183
    $delivery->close($response);
184
185
    return $response;
186
  }
187
188
}
189