Paragraphs Editor
CommandContext.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 /**
11  * Represents the command execution context.
12  *
13  * @see Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
14  */
16 
17  /**
18  * The entity that owns the field being edited.
19  *
20  * @var \Drupal\Core\Entity\EntityInterface
21  */
22  protected $entity;
23 
24  /**
25  * The field configuration for the field being edited.
26  *
27  * @var \Drupal\Core\Field\FieldConfigInterface
28  */
29  protected $fieldDefinition;
30 
31  /**
32  * The edit buffer associated with the editor instance.
33  *
34  * @var \Drupal\paragraphs_editor\EditBuffer\EditBufferInterface
35  */
36  protected $editBuffer;
37 
38  /**
39  * The field widget settings for the field being edited.
40  *
41  * @var array
42  */
43  protected $settings;
44 
45  /**
46  * An array of paragraph bundles that can be inserted into the field.
47  *
48  * If this is empty, we allow all paragraph items.
49  *
50  * @var \Drupal\paragraphs_editor\EditorCommand\ParagraphBundleFilterInterface
51  */
52  protected $bundleFilter;
53 
54  /**
55  * A mapping of plugin types to plugin instances associated with the command.
56  *
57  * @var array
58  */
59  protected $plugins = [];
60 
61  /**
62  * A temporary value store for persisting information across a single request.
63  *
64  * @var array
65  */
66  protected $temporary = [];
67 
68  /**
69  * An array of additional context entries the route provided.
70  *
71  * @var array
72  */
73  protected $additionalContext = [];
74 
75  /**
76  * The random build id identifying this context instance.
77  *
78  * @var string|null
79  */
80  protected $buildId = NULL;
81 
82  /**
83  * Creates a command context object.
84  *
85  * @param \Drupal\Core\Entity\EntityInterface|null $entity
86  * The entity that the field being edited belongs to.
87  * @param \Drupal\Core\Field\FieldConfigInterface|null $field_config
88  * The field configuration object for the field being edited.
89  * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferInterface|null $edit_buffer
90  * The edit buffer associated with the editor instance.
91  * @param \Drupal\paragraphs_editor\EditorCommand\ParagraphBundleFilterInterface|null $bundle_filter
92  * The bundle filter for determining which paragraph bundles can be children
93  * of this context.
94  * @param array $settings
95  * The field widget settings for the editor.
96  */
97  public function __construct(EntityInterface $entity = NULL, FieldConfigInterface $field_config = NULL, EditBufferInterface $edit_buffer = NULL, ParagraphBundleFilterInterface $bundle_filter = NULL, array $settings = []) {
98  $this->entity = $entity;
99  $this->fieldDefinition = $field_config;
100  $this->editBuffer = $edit_buffer;
101  $this->bundleFilter = $bundle_filter;
102  $this->settings = $settings;
103  if ($edit_buffer) {
104  $context_parts = explode(':', $edit_buffer->getContextString());
105  $this->buildId = end($context_parts);
106  }
107  }
108 
109  /**
110  * {@inheritdoc}
111  */
112  public function getEntity() {
113  return $this->entity;
114  }
115 
116  /**
117  * {@inheritdoc}
118  */
119  public function getFieldConfig() {
120  return $this->fieldDefinition;
121  }
122 
123  /**
124  * {@inheritdoc}
125  */
126  public function getBundleFilter() {
127  return $this->bundleFilter;
128  }
129 
130  /**
131  * {@inheritdoc}
132  */
133  public function getEditBuffer() {
134  return $this->editBuffer;
135  }
136 
137  /**
138  * {@inheritdoc}
139  */
140  public function getContextString() {
141  return $this->editBuffer->getContextString();
142  }
143 
144  /**
145  * {@inheritdoc}
146  */
147  public function getBuildId() {
148  return $this->buildId;
149  }
150 
151  /**
152  * {@inheritdoc}
153  */
154  public function setTemporary($name, $value) {
155  $this->temporary[$name] = $value;
156  }
157 
158  /**
159  * {@inheritdoc}
160  */
161  public function getTemporary($name) {
162  return isset($this->temporary[$name]) ? $this->temporary[$name] : NULL;
163  }
164 
165  /**
166  * {@inheritdoc}
167  */
168  public function isValid() {
169  return TRUE;
170  }
171 
172  /**
173  * {@inheritdoc}
174  */
175  public function setPlugin($type, $plugin_object) {
176  $this->plugins[$type] = $plugin_object;
177  }
178 
179  /**
180  * {@inheritdoc}
181  */
182  public function getPlugin($type) {
183  return $this->plugins[$type];
184  }
185 
186  /**
187  * {@inheritdoc}
188  */
189  public function getSetting($name) {
190  return isset($this->settings[$name]) ? $this->settings[$name] : NULL;
191  }
192 
193  /**
194  * {@inheritdoc}
195  */
196  public function getSettings() {
197  return $this->settings;
198  }
199 
200  /**
201  * {@inheritdoc}
202  */
203  public function createCommandUrl($command, array $params = []) {
204  return Url::fromRoute("paragraphs_editor.command.$command", [
205  'context' => $this->getContextString(),
206  ] + $params,
207  [
208  'query' => [
209  'settings' => $this->getSettings(),
210  'additional_context' => serialize($this->additionalContext),
211  ],
212  ]);
213  }
214 
215  /**
216  * {@inheritdoc}
217  */
218  public function addAdditionalContext($key, $value) {
219  $this->additionalContext[$key] = $value;
220  }
221 
222  /**
223  * {@inheritdoc}
224  */
225  public function getAdditionalContext($key = NULL) {
226  if (!empty($key)) {
227  return isset($this->additionalContext[$key]) ? $this->additionalContext[$key] : NULL;
228  }
229  else {
231  }
232  }
233 
234 }
__construct(EntityInterface $entity=NULL, FieldConfigInterface $field_config=NULL, EditBufferInterface $edit_buffer=NULL, ParagraphBundleFilterInterface $bundle_filter=NULL, array $settings=[])