Paragraphs Editor
EditorCommandController.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
11 /**
12  * The entry point for commands being sent from the widget client.
13  *
14  * This controller defines the operations that the client can perform. Each
15  * operation has a method in the controller, which returns an AjaxResponse
16  * object containing a list of ajax commands to be executed by the client in
17  * response to the editor command.
18  */
19 class EditorCommandController implements ContainerInjectionInterface {
20 
21  /**
22  * The handler used to provide command responses.
23  *
24  * @var \Drupal\paragraphs_editor\EditorCommand\ResponseHandlerInterface
25  */
26  protected $responseHandler;
27 
28  /**
29  * The factory for creating edit buffer items.
30  *
31  * @var \Drupal\paragraphs_editor\EditBuffer\EditBufferItemFactoryInterface
32  */
33  protected $itemFactory;
34 
35  /**
36  * Constructs an editor command controller.
37  *
38  * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemFactoryInterface $item_factory
39  * The edit buffer item factory for loading, updating and creating edit
40  * buffer items.
41  * @param \Drupal\paragraphs_editor\EditorCommand\ResponseHandlerInterface $response_handler
42  * The handler obejct that will serve the command responses.
43  */
44  public function __construct(EditBufferItemFactoryInterface $item_factory, ResponseHandlerInterface $response_handler) {
45  $this->itemFactory = $item_factory;
46  $this->responseHandler = $response_handler;
47  }
48 
49  /**
50  * {@inheritdoc}
51  */
52  public static function create(ContainerInterface $container) {
53  return new static(
54  $container->get('paragraphs_editor.edit_buffer.item_factory'),
55  $container->get('paragraphs_editor.command.response_handler')
56  );
57  }
58 
59  /**
60  * Entry point for requests to insert a new paragraph item.
61  *
62  * If bundle name is NULL, this provides a bundle selection mechanism and
63  * forwards the client back to this controller method with the bundle name
64  * filled in. If bundle name is not NULL, a new paragraph of the given bundle
65  * name will be created and an edit form for that paragraph item will be
66  * delivered to the client.
67  *
68  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
69  * The context for the editor instance.
70  * @param string|null $bundle_name
71  * The name of a paragraph bundle to be inserted or NULL to display the
72  * bundle selection form.
73  *
74  * @return \Drupal\Core\Ajax\AjaxResponse
75  * The ajax response for the command.
76  */
77  public function insert(CommandContextInterface $context, $bundle_name = NULL) {
78  if (empty($bundle_name)) {
79  $response = $this->responseHandler->deliverBundleSelectForm($context);
80  }
81  else {
82  $item = $this->itemFactory->createBufferItem($context, $bundle_name);
83  $response = $this->responseHandler->deliverParagraphEditForm($context, $item);
84  }
85  return $response;
86  }
87 
88  /**
89  * Entry point for requests to edit a paragraph item.
90  *
91  * This loads the correct paragraph and any existing edits, then delivers an
92  * edit form for the paragarph based on its current state.
93  *
94  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
95  * The context for the editor instance.
96  * @param string $paragraph_uuid
97  * The UUID of the paragraph to generate an edit form for.
98  *
99  * @return \Drupal\Core\Ajax\AjaxResponse
100  * The ajax response for the command.
101  */
102  public function edit(CommandContextInterface $context, $paragraph_uuid) {
103  $item = $this->itemFactory->getBufferItem($context, $paragraph_uuid);
104  return $this->responseHandler->deliverParagraphEditForm($context, $item);
105  }
106 
107  /**
108  * Entry point for requests to render a paragraph item.
109  *
110  * Renders a paragraph item and delivers markup back to the editor so it can
111  * be displayed in Editor.
112  *
113  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
114  * The context for the editor instance.
115  * @param string $paragraph_uuid
116  * The UUID of the paragraph to deliver rendered markup for.
117  *
118  * @return \Drupal\Core\Ajax\AjaxResponse
119  * The ajax response for the command.
120  */
121  public function render(CommandContextInterface $context, $paragraph_uuid) {
122  $item = $this->itemFactory->getBufferItem($context, $paragraph_uuid);
123  return $this->responseHandler->deliverRenderedParagraph($context, $item);
124  }
125 
126  /**
127  * Entry point for requests to copy a paragraph item.
128  *
129  * In order to support copy and paste within the editor, we need a mechanism
130  * for cloning existing paragraph entities. Additionally we handle situations
131  * where paragraph items might be copied from another editor instance.
132  *
133  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $target_context
134  * The context for the editor instance that will receive the copy.
135  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $source_context
136  * The context for the editor instance that will provide the copy.
137  * @param string $paragraph_uuid
138  * The uuid of the paragraph entity to be copied.
139  * @param string $editor_widget_id
140  * The Editor widget id of the Editor widget to be updated with the newl
141  * created paragraph.
142  *
143  * @return \Drupal\Core\Ajax\AjaxResponse
144  * The ajax response for the command.
145  */
146  public function duplicate(CommandContextInterface $target_context, CommandContextInterface $source_context, $paragraph_uuid, $editor_widget_id) {
147  $item = $this->itemFactory->getBufferItem($source_context, $paragraph_uuid);
148  $item = $this->itemFactory->duplicateBufferItem($target_context, $item);
149  $item->save();
150  return $this->responseHandler->deliverDuplicate($target_context, $item, $editor_widget_id);
151  }
152 
153  /**
154  * Entry points for requests to cancel an ongoing multi-step command.
155  *
156  * Several operations can potentially be multi-step processes. For instance, a
157  * request to insert a new paragraph starts with the user selecting which
158  * bundle they want to use, then an edit form for the newly created paragraph
159  * item. This method allows a user to opt out of the process at any time.
160  *
161  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
162  * The context for the editor instance.
163  *
164  * @return \Drupal\Core\Ajax\AjaxResponse
165  * The ajax response for the command.
166  */
167  public function cancel(CommandContextInterface $context) {
168  return $this->responseHandler->deliverCloseForm($context);
169  }
170 
171 }
edit(CommandContextInterface $context, $paragraph_uuid)
duplicate(CommandContextInterface $target_context, CommandContextInterface $source_context, $paragraph_uuid, $editor_widget_id)
__construct(EditBufferItemFactoryInterface $item_factory, ResponseHandlerInterface $response_handler)
insert(CommandContextInterface $context, $bundle_name=NULL)
render(CommandContextInterface $context, $paragraph_uuid)