Newer
Older
<?php
/**
* @file
* Contains \Drupal\quickedit\QuickEditController.
*/
namespace Drupal\quickedit;
Angie Byron
committed
use Drupal\Core\Controller\ControllerBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormState;
Angie Byron
committed
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\EntityInterface;
use Drupal\quickedit\Ajax\FieldFormCommand;
use Drupal\quickedit\Ajax\FieldFormSavedCommand;
use Drupal\quickedit\Ajax\FieldFormValidationErrorsCommand;
use Drupal\quickedit\Ajax\EntitySavedCommand;
/**
* Returns responses for Quick Edit module routes.
*/
class QuickEditController extends ControllerBase {
Dries Buytaert
committed
/**
Angie Byron
committed
* The PrivateTempStore factory.
Dries Buytaert
committed
*
Angie Byron
committed
* @var \Drupal\user\PrivateTempStoreFactory
Dries Buytaert
committed
*/
protected $tempStoreFactory;
/**
* The in-place editing metadata generator.
*
* @var \Drupal\quickedit\MetadataGeneratorInterface
*/
protected $metadataGenerator;
/**
* The in-place editor selector.
*
* @var \Drupal\quickedit\EditorSelectorInterface
*/
protected $editorSelector;
Dries Buytaert
committed
/**
* Constructs a new QuickEditController.
Dries Buytaert
committed
*
Angie Byron
committed
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The PrivateTempStore factory.
* @param \Drupal\quickedit\MetadataGeneratorInterface $metadata_generator
* The in-place editing metadata generator.
* @param \Drupal\quickedit\EditorSelectorInterface $editor_selector
* The in-place editor selector.
Dries Buytaert
committed
*/
Angie Byron
committed
public function __construct(PrivateTempStoreFactory $temp_store_factory, MetadataGeneratorInterface $metadata_generator, EditorSelectorInterface $editor_selector) {
$this->tempStoreFactory = $temp_store_factory;
$this->metadataGenerator = $metadata_generator;
$this->editorSelector = $editor_selector;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
Angie Byron
committed
$container->get('user.private_tempstore'),
$container->get('quickedit.metadata.generator'),
Alex Pott
committed
$container->get('quickedit.editor.selector')
Dries Buytaert
committed
}
/**
* Returns the metadata for a set of fields.
*
* Given a list of field quick edit IDs as POST parameters, run access checks
* on the entity and field level to determine whether the current user may
* edit them. Also retrieves other metadata.
*
Dries Buytaert
committed
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function metadata(Request $request) {
$fields = $request->request->get('fields');
if (!isset($fields)) {
throw new NotFoundHttpException();
}
Dries Buytaert
committed
$entities = $request->request->get('entities');
$metadata = array();
foreach ($fields as $field) {
Angie Byron
committed
list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);
// Load the entity.
Angie Byron
committed
if (!$entity_type || !$this->entityManager()->getDefinition($entity_type)) {
throw new NotFoundHttpException();
}
catch
committed
$entity = $this->entityManager()->getStorage($entity_type)->load($entity_id);
if (!$entity) {
throw new NotFoundHttpException();
}
// Validate the field name and language.
if (!$field_name || !$entity->hasField($field_name)) {
throw new NotFoundHttpException();
}
Angie Byron
committed
if (!$langcode || !$entity->hasTranslation($langcode)) {
throw new NotFoundHttpException();
}
$entity = $entity->getTranslation($langcode);
Dries Buytaert
committed
// If the entity information for this field is requested, include it.
$entity_id = $entity->getEntityTypeId() . '/' . $entity_id;
Dries Buytaert
committed
if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) {
$metadata[$entity_id] = $this->metadataGenerator->generateEntityMetadata($entity);
Dries Buytaert
committed
}
$metadata[$field] = $this->metadataGenerator->generateFieldMetadata($entity->get($field_name), $view_mode);
}
Dries Buytaert
committed
return new JsonResponse($metadata);
}
Angie Byron
committed
Dries Buytaert
committed
/**
* Returns AJAX commands to load in-place editors' attachments.
*
* Given a list of in-place editor IDs as POST parameters, render AJAX
* commands to load those in-place editors.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
*/
public function attachments(Request $request) {
$response = new AjaxResponse();
$editors = $request->request->get('editors');
if (!isset($editors)) {
throw new NotFoundHttpException();
Angie Byron
committed
}
Dries Buytaert
committed
$response->setAttachments($this->editorSelector->getEditorAttachments($editors));
Angie Byron
committed
return $response;
}
/**
* Returns a single field edit form as an Ajax response.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being edited.
* @param string $field_name
* The name of the field that is being edited.
* @param string $langcode
* The name of the language for which the field is being edited.
* @param string $view_mode_id
* The view mode the field should be rerendered in.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object containing the search string.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
*/
public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request) {
$response = new AjaxResponse();
Angie Byron
committed
// Replace entity with PrivateTempStore copy if available and not resetting,
// init PrivateTempStore copy otherwise.
$tempstore_entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
if ($tempstore_entity && $request->request->get('reset') !== 'true') {
Dries Buytaert
committed
$entity = $tempstore_entity;
}
else {
$this->tempStoreFactory->get('quickedit')->set($entity->uuid(), $entity);
Dries Buytaert
committed
}
$form_state = (new FormState())
->set('langcode', $langcode)
->disableRedirect()
->addBuildInfo('args', [$entity, $field_name]);
$form = $this->formBuilder()->buildForm('Drupal\quickedit\Form\QuickEditFieldForm', $form_state);
Angie Byron
committed
if ($form_state->isExecuted()) {
Angie Byron
committed
// The form submission saved the entity in PrivateTempStore. Return the
// updated view of the field from the PrivateTempStore copy.
$entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
Dries Buytaert
committed
// Closure to render the field given a view mode.
Angie Byron
committed
$render_field_in_view_mode = function ($view_mode_id) use ($entity, $field_name, $langcode) {
return $this->renderField($entity, $field_name, $langcode, $view_mode_id);
Dries Buytaert
committed
};
// Re-render the updated field.
$output = $render_field_in_view_mode($view_mode_id);
// Re-render the updated field for other view modes (i.e. for other
// instances of the same logical field on the user's page).
$other_view_mode_ids = $request->request->get('other_view_modes') ?: array();
catch
committed
$other_view_modes = array_map($render_field_in_view_mode, array_combine($other_view_mode_ids, $other_view_mode_ids));
Dries Buytaert
committed
$response->addCommand(new FieldFormSavedCommand($output, $other_view_modes));
}
else {
$output = drupal_render($form);
// When working with a hidden form, we don't want its CSS/JS to be loaded.
if ($request->request->get('nocssjs') !== 'true') {
$response->setAttachments($form['#attached']);
}
$response->addCommand(new FieldFormCommand($output));
$errors = $form_state->getErrors();
if (count($errors)) {
$status_messages = array(
'#theme' => 'status_messages'
);
$response->addCommand(new FieldFormValidationErrorsCommand(drupal_render($status_messages)));
}
}
return $response;
}
Dries Buytaert
committed
/**
* Renders a field.
*
* If the view mode ID is not an Entity Display view mode ID, then the field
* was rendered using a custom render pipeline (not the Entity/Field API
* render pipeline).
*
* An example could be Views' render pipeline. In that case, the view mode ID
* would probably contain the View's ID, display and the row index.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being edited.
* @param string $field_name
* The name of the field that is being edited.
* @param string $langcode
* The name of the language for which the field is being edited.
* @param string $view_mode_id
* The view mode the field should be rerendered in. Either an Entity Display
* view mode ID, or a custom one. See hook_quickedit_render_field().
Dries Buytaert
committed
*
* @return string
* Rendered HTML.
*
* @see hook_quickedit_render_field()
Dries Buytaert
committed
*/
Angie Byron
committed
protected function renderField(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
$entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId()));
Dries Buytaert
committed
if (in_array($view_mode_id, $entity_view_mode_ids)) {
$entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
$output = $entity->get($field_name)->view($view_mode_id);
Dries Buytaert
committed
}
else {
// Each part of a custom (non-Entity Display) view mode ID is separated
// by a dash; the first part must be the module name.
$mode_id_parts = explode('-', $view_mode_id, 2);
$module = reset($mode_id_parts);
$args = array($entity, $field_name, $view_mode_id, $langcode);
$output = $this->moduleHandler()->invoke($module, 'quickedit_render_field', $args);
Dries Buytaert
committed
}
return drupal_render($output);
}
Dries Buytaert
committed
/**
Angie Byron
committed
* Saves an entity into the database, from PrivateTempStore.
Dries Buytaert
committed
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being edited.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
Dries Buytaert
committed
*/
public function entitySave(EntityInterface $entity) {
Angie Byron
committed
// Take the entity from PrivateTempStore and save in entity storage.
// fieldForm() ensures that the PrivateTempStore copy exists ahead.
$tempstore = $this->tempStoreFactory->get('quickedit');
Dries Buytaert
committed
$tempstore->get($entity->uuid())->save();
$tempstore->delete($entity->uuid());
// Return information about the entity that allows a front end application
// to identify it.
$output = array(
'entity_type' => $entity->getEntityTypeId(),
Dries Buytaert
committed
'entity_id' => $entity->id()
);
// Respond to client that the entity was saved properly.
$response = new AjaxResponse();
$response->addCommand(new EntitySavedCommand($output));
return $response;
}