Paragraphs Editor
ResponseHandler.php
Go to the documentation of this file.
1 <?php
2 
4 
13 
14 /**
15  * Response handler for paragraphs editor commands.
16  *
17  * This is the default class responsible for assembling symfony responses to
18  * paragraphs editor commands.
19  *
20  * @see Drupal\paragraphs_editor\EditorCommand\ResponseHandlerInterface
21  */
23 
24  /**
25  * The form builder service for serving up forms.
26  *
27  * @var \Drupal\Core\Form\FormBuilderInterface
28  */
29  protected $formBuilder;
30 
31  /**
32  * The entity type manager service.
33  *
34  * @var \Drupal\Core\Entity\EntityTypeManagerInterface
35  */
36  protected $entityTypeManager;
37 
38  /**
39  * The module handler service.
40  *
41  * @var \Drupal\Core\Extension\ModuleHandlerInterface
42  */
43  protected $moduleHandler;
44 
45  /**
46  * The (deprecated) entity manager service.
47  *
48  * This serves basically the same function as the entity type manager.
49  * The only reason it is included here is because the drupal core content
50  * entity edit form depends on it.
51  *
52  * @var \Drupal\Core\Entity\EntityManagerInterface
53  */
54  protected $entityManager;
55 
56  /**
57  * The widget binder data compiler service.
58  *
59  * @var \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerInterface
60  */
61  protected $dataCompiler;
62 
63  /**
64  * Creates a ResponseHandler object.
65  *
66  * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
67  * The form builder for generating forms.
68  * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
69  * The entity type manager.
70  * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
71  * The module handler service.
72  * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
73  * The entity manager service.
74  * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerInterface $data_compiler
75  * The widget binder data compiler service.
76  */
77  public function __construct(FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, WidgetBinderDataCompilerInterface $data_compiler) {
78  $this->formBuilder = $form_builder;
79  $this->entityTypeManager = $entity_type_manager;
80  $this->moduleHandler = $module_handler;
81  $this->entityManager = $entity_manager;
82  $this->dataCompiler = $data_compiler;
83  }
84 
85  /**
86  * {@inheritdoc}
87  */
89  return $this->navigate($context, $this->getBundleSelectForm($context));
90  }
91 
92  /**
93  * {@inheritdoc}
94  */
96  return $this->navigate($context, $this->getParagraphEditForm($context, $item));
97  }
98 
99  /**
100  * {@inheritdoc}
101  */
103  return $this->render($context, $item);
104  }
105 
106  /**
107  * {@inheritdoc}
108  */
109  public function deliverDuplicate(CommandContextInterface $context, EditBufferItemInterface $item, $editor_widget_id) {
110  return $this->duplicate($context, $item, $editor_widget_id);
111  }
112 
113  /**
114  * {@inheritdoc}
115  */
116  public function deliverCloseForm(CommandContextInterface $context) {
117  return $this->close($context);
118  }
119 
120  /**
121  * Generates an ajax response for opening a form.
122  *
123  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
124  * The context the command is executing within.
125  * @param mixed $contents
126  * A render array or markup string containing the contents to be delivered.
127  *
128  * @return \Drupal\Core\Ajax\AjaxResponse
129  * An ajax response to deliver.
130  */
131  protected function navigate(CommandContextInterface $context, $contents) {
132  $response = new AjaxResponse();
133  $context->getPlugin('delivery_provider')->navigate($response, $this->getDialogTitle($context), $contents);
134  return $response;
135  }
136 
137  /**
138  * Generates an ajax response for delivering a paragraph.
139  *
140  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
141  * The context the command is executing within.
142  * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface $item
143  * The buffer item to be rendered in the response.
144  *
145  * @return \Drupal\Core\Ajax\AjaxResponse
146  * An ajax response to deliver.
147  */
148  protected function render(CommandContextInterface $context, EditBufferItemInterface $item) {
149  $response = new AjaxResponse();
150  $context->getPlugin('delivery_provider')->sendData($response, $this->dataCompiler->compile($context, $item));
151  return $response;
152  }
153 
154  /**
155  * Generates an ajax response for delivering a duplicated paragraph.
156  *
157  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
158  * The context the command is executing within.
159  * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface $item
160  * The buffer item to be duplicated in the response.
161  * @param string $editor_widget_id
162  * The editor widget id to target for receiving the duplicated item.
163  *
164  * @return \Drupal\Core\Ajax\AjaxResponse
165  * An ajax response to deliver.
166  */
167  protected function duplicate(CommandContextInterface $context, EditBufferItemInterface $item, $editor_widget_id) {
168  $response = new AjaxResponse();
169  $context->addAdditionalContext('widgetId', $editor_widget_id);
170  $data = $this->dataCompiler->compile($context, $item);
171  $context->getPlugin('delivery_provider')->sendData($response, $data);
172  return $response;
173  }
174 
175  /**
176  * Generates an ajax response for closing a form.
177  *
178  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
179  * The context the command is executing within.
180  *
181  * @return \Drupal\Core\Ajax\AjaxResponse
182  * An ajax response to deliver.
183  */
184  protected function close(CommandContextInterface $context) {
185  $response = new AjaxResponse();
186  $context->getPlugin('delivery_provider')->close($response);
187  return $response;
188  }
189 
190  /**
191  * Gets a bundle select form object to deliver the user.
192  *
193  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
194  * The context the command is executing within.
195  *
196  * @return array
197  * A render array for the bundle select form.
198  */
199  protected function getBundleSelectForm(CommandContextInterface $context) {
200  $form = $context->getPlugin('bundle_selector');
201  return $this->formBuilder->getForm($form);
202  }
203 
204  /**
205  * Gets a paragraph edit form object to deliver the user.
206  *
207  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
208  * The context the command is executing within.
209  * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface $item
210  * The edit buffer item to get the form for.
211  *
212  * @return array
213  * A render array for the paragraph edit form.
214  */
216  $form = new ParagraphEntityForm($context, $item, $this->moduleHandler, $this->entityTypeManager, $this->entityManager, $this->dataCompiler);
217  return $this->formBuilder->getForm($form);
218  }
219 
220  /**
221  * Gets the dialog title for paragraph editor command dialogs.
222  *
223  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
224  * The context the command is exuting in.
225  *
226  * @return string
227  * The title to use in the dialog box.
228  */
229  protected function getDialogTitle(CommandContextInterface $context) {
230  if ($context->getAdditionalContext('command') == 'insert') {
231  return t('Insert @title', ['@title' => $context->getSetting('title')]);
232  }
233  else {
234  return t('Edit @title', ['@title' => $context->getSetting('title')]);
235  }
236  }
237 
238 }
deliverParagraphEditForm(CommandContextInterface $context, EditBufferItemInterface $item)
duplicate(CommandContextInterface $context, EditBufferItemInterface $item, $editor_widget_id)
__construct(FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, WidgetBinderDataCompilerInterface $data_compiler)
getBundleSelectForm(CommandContextInterface $context)
navigate(CommandContextInterface $context, $contents)
getParagraphEditForm(CommandContextInterface $context, EditBufferItemInterface $item)
render(CommandContextInterface $context, EditBufferItemInterface $item)
deliverBundleSelectForm(CommandContextInterface $context)
deliverDuplicate(CommandContextInterface $context, EditBufferItemInterface $item, $editor_widget_id)
deliverRenderedParagraph(CommandContextInterface $context, EditBufferItemInterface $item)