Commit bab8bdd9 authored by catch's avatar catch

Issue #2033433 by Wim Leers: Clean up edit controller.

parent 197ad58c
......@@ -16,7 +16,7 @@
/**
* Access check for editing entities.
*/
class EditEntityAccessCheck implements StaticAccessCheckInterface, EditEntityAccessCheckInterface {
class EditEntityAccessCheck implements StaticAccessCheckInterface {
/**
* {@inheritdoc}
......@@ -35,13 +35,13 @@ public function access(Route $route, Request $request) {
// http://drupal.org/node/1798214.
$this->validateAndUpcastRequestAttributes($request);
return $this->accessEditEntity($request->attributes->get('entity'));
return $this->accessEditEntity($request->attributes->get('entity')) ? static::ALLOW : static::DENY;
}
/**
* {@inheritdoc}
*/
public function accessEditEntity(EntityInterface $entity) {
protected function accessEditEntity(EntityInterface $entity) {
return $entity->access('update');
}
......
<?php
/**
* @file
* Contains \Drupal\edit\Access\EditEntityAccessCheckInterface.
*/
namespace Drupal\edit\Access;
use Drupal\Core\Entity\EntityInterface;
/**
* Access check for editing entities.
*/
interface EditEntityAccessCheckInterface {
/**
* Checks access to edit the requested entity.
*/
public function accessEditEntity(EntityInterface $entity);
}
......@@ -8,6 +8,7 @@
namespace Drupal\edit\Access;
use Drupal\Core\Access\StaticAccessCheckInterface;
use Drupal\edit\Access\EditEntityFieldAccessCheckInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -26,7 +27,7 @@ public function appliesTo() {
}
/**
* Implements AccessCheckInterface::access().
* {@inheritdoc}
*/
public function access(Route $route, Request $request) {
// @todo Request argument validation and object loading should happen
......@@ -34,11 +35,11 @@ public function access(Route $route, Request $request) {
// http://drupal.org/node/1798214.
$this->validateAndUpcastRequestAttributes($request);
return $this->accessEditEntityField($request->attributes->get('entity'), $request->attributes->get('field_name'));
return $this->accessEditEntityField($request->attributes->get('entity'), $request->attributes->get('field_name')) ? static::ALLOW : static::DENY;
}
/**
* Implements EntityFieldAccessCheckInterface::accessEditEntityField().
* {@inheritdoc}
*/
public function accessEditEntityField(EntityInterface $entity, $field_name) {
return $entity->access('update') && ($field = field_info_field($field_name)) && field_access('edit', $field, $entity->entityType(), $entity);
......
......@@ -8,12 +8,17 @@
namespace Drupal\edit;
use Drupal;
use Symfony\Component\DependencyInjection\ContainerAware;
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\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\field\FieldInfo;
use Drupal\edit\MetadataGeneratorInterface;
use Drupal\edit\EditorSelectorInterface;
use Drupal\edit\Ajax\FieldFormCommand;
use Drupal\edit\Ajax\FieldFormSavedCommand;
use Drupal\edit\Ajax\FieldFormValidationErrorsCommand;
......@@ -24,23 +29,76 @@
/**
* Returns responses for Edit module routes.
*/
class EditController extends ContainerAware {
class EditController implements ControllerInterface {
/**
* Stores the tempstore factory.
* The TempStore factory.
*
* @var \Drupal\user\TempStoreFactory
*/
protected $tempStoreFactory;
/**
* The in-place editing metadata generator.
*
* @var \Drupal\edit\MetadataGeneratorInterface
*/
protected $metadataGenerator;
/**
* The in-place editor selector.
*
* @var \Drupal\edit\EditorSelectorInterface
*/
protected $editorSelector;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* The field info service.
*
* @var \Drupal\field\FieldInfo
*/
protected $fieldInfo;
/**
* Constructs a new EditController.
*
* @param \Drupal\user\TempStoreFactory $temp_store_factory
* The factory for the temp store object.
* The TempStore factory.
* @param \Drupal\edit\MetadataGeneratorInterface $metadata_generator
* The in-place editing metadata generator.
* @param Drupal\edit\EditorSelectorInterface $editor_selector
* The in-place editor selector.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\field\FieldInfo $field_info
* The field info service.
*/
public function __construct(TempStoreFactory $temp_store_factory = NULL) {
$this->tempStoreFactory = $temp_store_factory ?: Drupal::service('user.tempstore');
public function __construct(TempStoreFactory $temp_store_factory, MetadataGeneratorInterface $metadata_generator, EditorSelectorInterface $editor_selector, EntityManager $entity_manager, FieldInfo $field_info) {
$this->tempStoreFactory = $temp_store_factory;
$this->metadataGenerator = $metadata_generator;
$this->editorSelector = $editor_selector;
$this->entityManager = $entity_manager;
$this->fieldInfo = $field_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('user.tempstore'),
$container->get('edit.metadata.generator'),
$container->get('edit.editor.selector'),
$container->get('plugin.manager.entity'),
$container->get('field.info')
);
}
/**
......@@ -60,23 +118,21 @@ public function metadata(Request $request) {
}
$entities = $request->request->get('entities');
$metadataGenerator = $this->container->get('edit.metadata.generator');
$metadata = array();
foreach ($fields as $field) {
list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);
// Load the entity.
if (!$entity_type || !entity_get_info($entity_type)) {
if (!$entity_type || !$this->entityManager->getDefinition($entity_type)) {
throw new NotFoundHttpException();
}
$entity = entity_load($entity_type, $entity_id);
$entity = $this->entityManager->getStorageController($entity_type)->load($entity_id);
if (!$entity) {
throw new NotFoundHttpException();
}
// Validate the field name and language.
if (!$field_name || !($instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()))) {
if (!$field_name || !($instance = $this->fieldInfo->getInstance($entity->entityType(), $entity->bundle(), $field_name))) {
throw new NotFoundHttpException();
}
if (!$langcode || (field_valid_language($langcode) !== $langcode)) {
......@@ -86,10 +142,10 @@ public function metadata(Request $request) {
// If the entity information for this field is requested, include it.
$entity_id = $entity->entityType() . '/' . $entity_id;
if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) {
$metadata[$entity_id] = $metadataGenerator->generateEntity($entity, $langcode);
$metadata[$entity_id] = $this->metadataGenerator->generateEntity($entity, $langcode);
}
$metadata[$field] = $metadataGenerator->generateField($entity, $instance, $langcode, $view_mode);
$metadata[$field] = $this->metadataGenerator->generateField($entity, $instance, $langcode, $view_mode);
}
return new JsonResponse($metadata);
......@@ -111,8 +167,7 @@ public function attachments(Request $request) {
throw new NotFoundHttpException();
}
$editorSelector = $this->container->get('edit.editor.selector');
$elements['#attached'] = $editorSelector->getEditorAttachments($editors);
$elements['#attached'] = $this->editorSelector->getEditorAttachments($editors);
drupal_process_attached($elements);
return $response;
......@@ -141,7 +196,7 @@ public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view
// Replace entity with tempstore copy if available and not resetting, init
// tempstore copy otherwise.
$tempstore_entity = $this->tempStoreFactory->get('edit')->get($entity->uuid());
if ($tempstore_entity && !(isset($_POST['reset']) && $_POST['reset'] === 'true')) {
if ($tempstore_entity && $request->request->get('reset') !== 'true') {
$entity = $tempstore_entity;
}
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment