annotate core/modules/quickedit/src/QuickEditController.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\quickedit;
Chris@0 4
Chris@0 5 use Drupal\Core\Controller\ControllerBase;
Chris@0 6 use Drupal\Core\Form\FormState;
Chris@0 7 use Drupal\Core\Render\RendererInterface;
Chris@0 8 use Drupal\user\PrivateTempStoreFactory;
Chris@0 9 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 10 use Symfony\Component\HttpFoundation\JsonResponse;
Chris@0 11 use Symfony\Component\HttpFoundation\Request;
Chris@0 12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Chris@0 13 use Drupal\Core\Ajax\AjaxResponse;
Chris@0 14 use Drupal\Core\Entity\EntityInterface;
Chris@0 15 use Drupal\quickedit\Ajax\FieldFormCommand;
Chris@0 16 use Drupal\quickedit\Ajax\FieldFormSavedCommand;
Chris@0 17 use Drupal\quickedit\Ajax\FieldFormValidationErrorsCommand;
Chris@0 18 use Drupal\quickedit\Ajax\EntitySavedCommand;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Returns responses for Quick Edit module routes.
Chris@0 22 */
Chris@0 23 class QuickEditController extends ControllerBase {
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The PrivateTempStore factory.
Chris@0 27 *
Chris@0 28 * @var \Drupal\user\PrivateTempStoreFactory
Chris@0 29 */
Chris@0 30 protected $tempStoreFactory;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The in-place editing metadata generator.
Chris@0 34 *
Chris@0 35 * @var \Drupal\quickedit\MetadataGeneratorInterface
Chris@0 36 */
Chris@0 37 protected $metadataGenerator;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The in-place editor selector.
Chris@0 41 *
Chris@0 42 * @var \Drupal\quickedit\EditorSelectorInterface
Chris@0 43 */
Chris@0 44 protected $editorSelector;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * The renderer.
Chris@0 48 *
Chris@0 49 * @var \Drupal\Core\Render\RendererInterface
Chris@0 50 */
Chris@0 51 protected $renderer;
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Constructs a new QuickEditController.
Chris@0 55 *
Chris@0 56 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
Chris@0 57 * The PrivateTempStore factory.
Chris@0 58 * @param \Drupal\quickedit\MetadataGeneratorInterface $metadata_generator
Chris@0 59 * The in-place editing metadata generator.
Chris@0 60 * @param \Drupal\quickedit\EditorSelectorInterface $editor_selector
Chris@0 61 * The in-place editor selector.
Chris@0 62 * @param \Drupal\Core\Render\RendererInterface $renderer
Chris@0 63 * The renderer.
Chris@0 64 */
Chris@0 65 public function __construct(PrivateTempStoreFactory $temp_store_factory, MetadataGeneratorInterface $metadata_generator, EditorSelectorInterface $editor_selector, RendererInterface $renderer) {
Chris@0 66 $this->tempStoreFactory = $temp_store_factory;
Chris@0 67 $this->metadataGenerator = $metadata_generator;
Chris@0 68 $this->editorSelector = $editor_selector;
Chris@0 69 $this->renderer = $renderer;
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public static function create(ContainerInterface $container) {
Chris@0 76 return new static(
Chris@0 77 $container->get('user.private_tempstore'),
Chris@0 78 $container->get('quickedit.metadata.generator'),
Chris@0 79 $container->get('quickedit.editor.selector'),
Chris@0 80 $container->get('renderer')
Chris@0 81 );
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Returns the metadata for a set of fields.
Chris@0 86 *
Chris@0 87 * Given a list of field quick edit IDs as POST parameters, run access checks
Chris@0 88 * on the entity and field level to determine whether the current user may
Chris@0 89 * edit them. Also retrieves other metadata.
Chris@0 90 *
Chris@0 91 * @return \Symfony\Component\HttpFoundation\JsonResponse
Chris@0 92 * The JSON response.
Chris@0 93 */
Chris@0 94 public function metadata(Request $request) {
Chris@0 95 $fields = $request->request->get('fields');
Chris@0 96 if (!isset($fields)) {
Chris@0 97 throw new NotFoundHttpException();
Chris@0 98 }
Chris@0 99 $entities = $request->request->get('entities');
Chris@0 100
Chris@0 101 $metadata = [];
Chris@0 102 foreach ($fields as $field) {
Chris@0 103 list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);
Chris@0 104
Chris@0 105 // Load the entity.
Chris@0 106 if (!$entity_type || !$this->entityManager()->getDefinition($entity_type)) {
Chris@0 107 throw new NotFoundHttpException();
Chris@0 108 }
Chris@0 109 $entity = $this->entityManager()->getStorage($entity_type)->load($entity_id);
Chris@0 110 if (!$entity) {
Chris@0 111 throw new NotFoundHttpException();
Chris@0 112 }
Chris@0 113
Chris@0 114 // Validate the field name and language.
Chris@0 115 if (!$field_name || !$entity->hasField($field_name)) {
Chris@0 116 throw new NotFoundHttpException();
Chris@0 117 }
Chris@0 118 if (!$langcode || !$entity->hasTranslation($langcode)) {
Chris@0 119 throw new NotFoundHttpException();
Chris@0 120 }
Chris@0 121
Chris@0 122 $entity = $entity->getTranslation($langcode);
Chris@0 123
Chris@0 124 // If the entity information for this field is requested, include it.
Chris@0 125 $entity_id = $entity->getEntityTypeId() . '/' . $entity_id;
Chris@0 126 if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) {
Chris@0 127 $metadata[$entity_id] = $this->metadataGenerator->generateEntityMetadata($entity);
Chris@0 128 }
Chris@0 129
Chris@0 130 $metadata[$field] = $this->metadataGenerator->generateFieldMetadata($entity->get($field_name), $view_mode);
Chris@0 131 }
Chris@0 132
Chris@0 133 return new JsonResponse($metadata);
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * Returns AJAX commands to load in-place editors' attachments.
Chris@0 138 *
Chris@0 139 * Given a list of in-place editor IDs as POST parameters, render AJAX
Chris@0 140 * commands to load those in-place editors.
Chris@0 141 *
Chris@0 142 * @return \Drupal\Core\Ajax\AjaxResponse
Chris@0 143 * The Ajax response.
Chris@0 144 */
Chris@0 145 public function attachments(Request $request) {
Chris@0 146 $response = new AjaxResponse();
Chris@0 147 $editors = $request->request->get('editors');
Chris@0 148 if (!isset($editors)) {
Chris@0 149 throw new NotFoundHttpException();
Chris@0 150 }
Chris@0 151
Chris@0 152 $response->setAttachments($this->editorSelector->getEditorAttachments($editors));
Chris@0 153
Chris@0 154 return $response;
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * Returns a single field edit form as an Ajax response.
Chris@0 159 *
Chris@0 160 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 161 * The entity being edited.
Chris@0 162 * @param string $field_name
Chris@0 163 * The name of the field that is being edited.
Chris@0 164 * @param string $langcode
Chris@0 165 * The name of the language for which the field is being edited.
Chris@0 166 * @param string $view_mode_id
Chris@0 167 * The view mode the field should be rerendered in.
Chris@0 168 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 169 * The current request object containing the search string.
Chris@0 170 *
Chris@0 171 * @return \Drupal\Core\Ajax\AjaxResponse
Chris@0 172 * The Ajax response.
Chris@0 173 */
Chris@0 174 public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request) {
Chris@0 175 $response = new AjaxResponse();
Chris@0 176
Chris@0 177 // Replace entity with PrivateTempStore copy if available and not resetting,
Chris@0 178 // init PrivateTempStore copy otherwise.
Chris@0 179 $tempstore_entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
Chris@0 180 if ($tempstore_entity && $request->request->get('reset') !== 'true') {
Chris@0 181 $entity = $tempstore_entity;
Chris@0 182 }
Chris@0 183 else {
Chris@0 184 $this->tempStoreFactory->get('quickedit')->set($entity->uuid(), $entity);
Chris@0 185 }
Chris@0 186
Chris@0 187 $form_state = (new FormState())
Chris@0 188 ->set('langcode', $langcode)
Chris@0 189 ->disableRedirect()
Chris@0 190 ->addBuildInfo('args', [$entity, $field_name]);
Chris@0 191 $form = $this->formBuilder()->buildForm('Drupal\quickedit\Form\QuickEditFieldForm', $form_state);
Chris@0 192
Chris@0 193 if ($form_state->isExecuted()) {
Chris@0 194 // The form submission saved the entity in PrivateTempStore. Return the
Chris@0 195 // updated view of the field from the PrivateTempStore copy.
Chris@0 196 $entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
Chris@0 197
Chris@0 198 // Closure to render the field given a view mode.
Chris@0 199 $render_field_in_view_mode = function ($view_mode_id) use ($entity, $field_name, $langcode) {
Chris@0 200 return $this->renderField($entity, $field_name, $langcode, $view_mode_id);
Chris@0 201 };
Chris@0 202
Chris@0 203 // Re-render the updated field.
Chris@0 204 $output = $render_field_in_view_mode($view_mode_id);
Chris@0 205
Chris@0 206 // Re-render the updated field for other view modes (i.e. for other
Chris@0 207 // instances of the same logical field on the user's page).
Chris@0 208 $other_view_mode_ids = $request->request->get('other_view_modes') ?: [];
Chris@0 209 $other_view_modes = array_map($render_field_in_view_mode, array_combine($other_view_mode_ids, $other_view_mode_ids));
Chris@0 210
Chris@0 211 $response->addCommand(new FieldFormSavedCommand($output, $other_view_modes));
Chris@0 212 }
Chris@0 213 else {
Chris@0 214 $output = $this->renderer->renderRoot($form);
Chris@0 215 // When working with a hidden form, we don't want its CSS/JS to be loaded.
Chris@0 216 if ($request->request->get('nocssjs') !== 'true') {
Chris@0 217 $response->setAttachments($form['#attached']);
Chris@0 218 }
Chris@0 219 $response->addCommand(new FieldFormCommand($output));
Chris@0 220
Chris@0 221 $errors = $form_state->getErrors();
Chris@0 222 if (count($errors)) {
Chris@0 223 $status_messages = [
Chris@0 224 '#type' => 'status_messages'
Chris@0 225 ];
Chris@0 226 $response->addCommand(new FieldFormValidationErrorsCommand($this->renderer->renderRoot($status_messages)));
Chris@0 227 }
Chris@0 228 }
Chris@0 229
Chris@0 230 return $response;
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * Renders a field.
Chris@0 235 *
Chris@0 236 * If the view mode ID is not an Entity Display view mode ID, then the field
Chris@0 237 * was rendered using a custom render pipeline (not the Entity/Field API
Chris@0 238 * render pipeline).
Chris@0 239 *
Chris@0 240 * An example could be Views' render pipeline. In that case, the view mode ID
Chris@0 241 * would probably contain the View's ID, display and the row index.
Chris@0 242 *
Chris@0 243 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 244 * The entity being edited.
Chris@0 245 * @param string $field_name
Chris@0 246 * The name of the field that is being edited.
Chris@0 247 * @param string $langcode
Chris@0 248 * The name of the language for which the field is being edited.
Chris@0 249 * @param string $view_mode_id
Chris@0 250 * The view mode the field should be rerendered in. Either an Entity Display
Chris@0 251 * view mode ID, or a custom one. See hook_quickedit_render_field().
Chris@0 252 *
Chris@0 253 * @return \Drupal\Component\Render\MarkupInterface
Chris@0 254 * Rendered HTML.
Chris@0 255 *
Chris@0 256 * @see hook_quickedit_render_field()
Chris@0 257 */
Chris@0 258 protected function renderField(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
Chris@0 259 $entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId()));
Chris@0 260 if (in_array($view_mode_id, $entity_view_mode_ids)) {
Chris@0 261 $entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
Chris@0 262 $output = $entity->get($field_name)->view($view_mode_id);
Chris@0 263 }
Chris@0 264 else {
Chris@0 265 // Each part of a custom (non-Entity Display) view mode ID is separated
Chris@0 266 // by a dash; the first part must be the module name.
Chris@0 267 $mode_id_parts = explode('-', $view_mode_id, 2);
Chris@0 268 $module = reset($mode_id_parts);
Chris@0 269 $args = [$entity, $field_name, $view_mode_id, $langcode];
Chris@0 270 $output = $this->moduleHandler()->invoke($module, 'quickedit_render_field', $args);
Chris@0 271 }
Chris@0 272
Chris@0 273 return $this->renderer->renderRoot($output);
Chris@0 274 }
Chris@0 275
Chris@0 276 /**
Chris@0 277 * Saves an entity into the database, from PrivateTempStore.
Chris@0 278 *
Chris@0 279 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 280 * The entity being edited.
Chris@0 281 *
Chris@0 282 * @return \Drupal\Core\Ajax\AjaxResponse
Chris@0 283 * The Ajax response.
Chris@0 284 */
Chris@0 285 public function entitySave(EntityInterface $entity) {
Chris@0 286 // Take the entity from PrivateTempStore and save in entity storage.
Chris@0 287 // fieldForm() ensures that the PrivateTempStore copy exists ahead.
Chris@0 288 $tempstore = $this->tempStoreFactory->get('quickedit');
Chris@0 289 $tempstore->get($entity->uuid())->save();
Chris@0 290 $tempstore->delete($entity->uuid());
Chris@0 291
Chris@0 292 // Return information about the entity that allows a front end application
Chris@0 293 // to identify it.
Chris@0 294 $output = [
Chris@0 295 'entity_type' => $entity->getEntityTypeId(),
Chris@0 296 'entity_id' => $entity->id()
Chris@0 297 ];
Chris@0 298
Chris@0 299 // Respond to client that the entity was saved properly.
Chris@0 300 $response = new AjaxResponse();
Chris@0 301 $response->addCommand(new EntitySavedCommand($output));
Chris@0 302 return $response;
Chris@0 303 }
Chris@0 304
Chris@0 305 }