|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\paragraphs_editor\WidgetBinder\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Field\EntityReferenceFieldItemListInterface; |
|
6
|
|
|
use Drupal\paragraphs\ParagraphInterface; |
|
7
|
|
|
use Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface; |
|
8
|
|
|
use Drupal\paragraphs_editor\Utility\TypeUtility; |
|
9
|
|
|
use Drupal\paragraphs_editor\WidgetBinder\GeneratorBase; |
|
10
|
|
|
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData; |
|
11
|
|
|
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState; |
|
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
|
|
|
*/ |
|
20
|
|
|
class ContextGenerator extends GeneratorBase { |
|
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
|
3 |
|
public function __construct(CommandContextFactoryInterface $context_factory) { |
|
36
|
3 |
|
$this->contextFactory = $context_factory; |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function id() { |
|
43
|
1 |
|
return 'context'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph) { |
|
50
|
1 |
|
$editable_contexts = $state->getItemContext()->getAdditionalContext('editableContexts'); |
|
51
|
1 |
|
$state->set('regenerate_contexts', $editable_contexts); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field) { |
|
58
|
1 |
|
if ($is_editor_field) { |
|
59
|
1 |
|
$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
|
1 |
|
$entity = $items->getEntity(); |
|
65
|
1 |
|
$uuid = $entity->uuid(); |
|
66
|
1 |
|
$field_config_id = TypeUtility::ensureFieldConfig($items->getFieldDefinition())->id(); |
|
67
|
1 |
|
if (!empty($regenerate_contexts[$uuid][$field_config_id])) { |
|
68
|
1 |
|
$from_context = $this->contextFactory->get($regenerate_contexts[$uuid][$field_config_id]); |
|
69
|
1 |
|
$context = $this->contextFactory->regenerate($from_context); |
|
70
|
1 |
|
$data->addModel('context', $from_context->getContextString(), [ |
|
71
|
1 |
|
'id' => $context->getContextString(), |
|
72
|
1 |
|
'ownerId' => $uuid, |
|
73
|
1 |
|
'fieldId' => $field_config_id, |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
else { |
|
77
|
1 |
|
$context = $this->contextFactory->create($field_config_id, $entity->id()); |
|
78
|
1 |
|
$data->addModel('context', $context->getContextString(), [ |
|
79
|
1 |
|
'id' => $context->getContextString(), |
|
80
|
1 |
|
'ownerId' => $uuid, |
|
81
|
1 |
|
'fieldId' => $field_config_id, |
|
82
|
1 |
|
'schemaId' => $field_config_id, |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
1 |
|
} |
|
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
|
|
|
} |
|
99
|
|
|
|