Paragraphs Editor
ContextGenerator.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
13 /**
14  * Regenerates editor contexts for the editables inside an edit buffer item.
15  *
16  * Some paragraphs may contain nested inline editables. This generator creates
17  * the editing context for each nested editable, and maps old contexts that have
18  * been regenerated to their new context id so that existing edits aren't lost.
19  */
21 
22  /**
23  * The context factory for generating contexts.
24  *
25  * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface
26  */
27  protected $contextFactory;
28 
29  /**
30  * Creates a ContextGenerator object.
31  *
32  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface $context_factory
33  * The context factory for generating contexts.
34  */
35  public function __construct(CommandContextFactoryInterface $context_factory) {
36  $this->contextFactory = $context_factory;
37  }
38 
39  /**
40  * {@inheritdoc}
41  */
42  public function id() {
43  return 'context';
44  }
45 
46  /**
47  * {@inheritdoc}
48  */
49  public function initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph) {
50  $editable_contexts = $state->getItemContext()->getAdditionalContext('editableContexts');
51  $state->set('regenerate_contexts', $editable_contexts);
52  }
53 
54  /**
55  * {@inheritdoc}
56  */
57  public function processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field) {
58  if ($is_editor_field) {
59  $regenerate_contexts = $state->get('regenerate_contexts');
60 
61  // We regenerate the context each time the field item is rendered to
62  // prevent issues with form caching. This means we have to map existing
63  // edits fro mthe old context to the new one.
64  $entity = $items->getEntity();
65  $uuid = $entity->uuid();
66  $field_config_id = TypeUtility::ensureFieldConfig($items->getFieldDefinition())->id();
67  if (!empty($regenerate_contexts[$uuid][$field_config_id])) {
68  $from_context = $this->contextFactory->get($regenerate_contexts[$uuid][$field_config_id]);
69  $context = $this->contextFactory->regenerate($from_context);
70  $data->addModel('context', $from_context->getContextString(), [
71  'id' => $context->getContextString(),
72  'ownerId' => $uuid,
73  'fieldId' => $field_config_id,
74  ]);
75  }
76  else {
77  $context = $this->contextFactory->create($field_config_id, $entity->id());
78  $data->addModel('context', $context->getContextString(), [
79  'id' => $context->getContextString(),
80  'ownerId' => $uuid,
81  'fieldId' => $field_config_id,
82  'schemaId' => $field_config_id,
83  ]);
84  }
85  }
86  }
87 
88  /**
89  * A public interface to get context objects from context ids.
90  *
91  * @return \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
92  * The requested context, or NULL if it could not be found.
93  */
94  public function getContext($context_id) {
95  return $this->contextFactory->get($context_id);
96  }
97 
98 }
static ensureFieldConfig(FieldDefinitionInterface $field_definition=NULL)
Definition: TypeUtility.php:39
__construct(CommandContextFactoryInterface $context_factory)
processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field)
initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph)