Paragraphs Editor
WidgetGenerator.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 /**
11  * Generates widget models for updating widget properties in the editor.
12  *
13  * For most operations the widget doesn't need to be directly altered from an
14  * explicit server-side update.
15  *
16  * The lone exception is for duplication requests. When an edit buffer item is
17  * duplicated, we need to instruct the widget that it should no longer be in a
18  * duplicating state, and we need to map edits from the original widget to the
19  * copy.
20  */
22 
23  /**
24  * {@inheritdoc}
25  */
26  public function id() {
27  return 'widget';
28  }
29 
30  /**
31  * {@inheritdoc}
32  */
33  public function complete(WidgetBinderData $data, WidgetBinderDataCompilerState $state, RenderContext $render_context, $markup) {
34  $context = $state->getItemContext();
35  $widget_id = $context->getAdditionalContext('widgetId');
36  if ($widget_id) {
37  $new_edits = [];
38  $old_edits = $context->getAdditionalContext('edits');
39  $contexts = $context->getAdditionalContext('editableContexts');
40  $entity_map = $context->getAdditionalContext('entityMap');
41  foreach ($contexts as $uuid => $fields) {
42  foreach ($fields as $field_name => $old_context_id) {
43  $new_context_id = $data->getContextId($entity_map[$uuid], $field_name);
44  if (!empty($old_edits[$old_context_id])) {
45  $new_edits[$new_context_id] = $old_edits[$old_context_id];
46  }
47  }
48  }
49 
50  $data->addModel('widget', $widget_id, [
51  'contextId' => $context->getContextString(),
52  'editorContextId' => $context->getAdditionalContext('editorContext'),
53  'itemContextId' => $context->getContextString(),
54  'itemId' => $state->getItem()->getEntity()->uuid(),
55  'duplicating' => FALSE,
56  'edits' => $new_edits,
57  ]);
58  }
59  }
60 
61 }
complete(WidgetBinderData $data, WidgetBinderDataCompilerState $state, RenderContext $render_context, $markup)