Commit 77c666c3 authored by alexpott's avatar alexpott

Issue #1987882 by disasm, vijaycs85, penyaskito, kim.pepper, likin, YesCT,...

Issue #1987882 by disasm, vijaycs85, penyaskito, kim.pepper, likin, YesCT, InternetDevels, dawehner, wamilton, Letharion, katbailey, googletorp, tim.plunkett, acrollet, neetu morwani: Convert content_translation routes to a new style controller.
parent f485925f
......@@ -17,7 +17,7 @@ services:
content_translation.manage_access:
class: Drupal\content_translation\Access\ContentTranslationManageAccessCheck
arguments: ['@entity.manager']
arguments: ['@entity.manager', '@language_manager']
tags:
- { name: access_check, applies_to: _access_content_translation_manage }
......
......@@ -9,10 +9,11 @@
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Access check for entity translation CRUD operation.
......@@ -26,14 +27,24 @@ class ContentTranslationManageAccessCheck implements AccessInterface {
*/
protected $entityManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a ContentTranslationManageAccessCheck object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityManagerInterface $manager) {
public function __construct(EntityManagerInterface $manager, LanguageManagerInterface $language_manager) {
$this->entityManager = $manager;
$this->languageManager = $language_manager;
}
/**
......@@ -52,39 +63,43 @@ public function __construct(EntityManagerInterface $manager) {
* @param string $language
* (optional) For an update or delete operation, the language code of the
* translation being updated or deleted.
* @param string $entity_type_id
* (optional) The entity type ID.
*
* @return string
* A \Drupal\Core\Access\AccessInterface constant value.
*/
public function access(Route $route, Request $request, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL) {
$entity_type = $request->attributes->get('_entity_type_id');
/** @var $entity \Drupal\Core\Entity\EntityInterface */
if ($entity = $request->attributes->get($entity_type)) {
public function access(Route $route, Request $request, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) {
/* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if ($entity = $request->attributes->get($entity_type_id)) {
$operation = $route->getRequirement('_access_content_translation_manage');
$controller = content_translation_controller($entity_type, $account);
/* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
$handler = $this->entityManager->getController($entity->getEntityTypeId(), 'translation');
// Load translation.
$translations = $entity->getTranslationLanguages();
$languages = language_list();
$languages = $this->languageManager->getLanguages();
switch ($operation) {
case 'create':
$source = language_load($source) ?: $entity->language();
$target = language_load($target) ?: \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
return ($source->id != $target->id
&& isset($languages[$source->id])
&& isset($languages[$target->id])
&& !isset($translations[$target->id])
&& $controller->getTranslationAccess($entity, $operation))
$source_language = $this->languageManager->getLanguage($source) ?: $entity->language();
$target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
return ($source_language->getId() != $target_language->getId()
&& isset($languages[$source_language->getId()])
&& isset($languages[$target_language->getId()])
&& !isset($translations[$target_language->getId()])
&& $handler->getTranslationAccess($entity, $operation))
? static::ALLOW : static::DENY;
case 'update':
case 'delete':
$language = language_load($language) ?: \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
return isset($languages[$language->id])
&& $language->id != $entity->getUntranslated()->language()->id
&& isset($translations[$language->id])
&& $controller->getTranslationAccess($entity, $operation)
$language = $this->languageManager->getLanguage($language) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
return isset($languages[$language->getId()])
&& $language->getId() != $entity->getUntranslated()->language()->getId()
&& isset($translations[$language->getId()])
&& $handler->getTranslationAccess($entity, $operation)
? static::ALLOW : static::DENY;
}
}
......
......@@ -46,7 +46,7 @@ public function __construct(EntityManagerInterface $manager) {
* A \Drupal\Core\Access\AccessInterface constant value.
*/
public function access(Request $request, AccountInterface $account) {
$entity_type = $request->attributes->get('_entity_type_id');
$entity_type = $request->attributes->get('entity_type_id');
if ($entity = $request->attributes->get($entity_type)) {
// Get entity base info.
$bundle = $entity->bundle();
......
......@@ -201,8 +201,12 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
// A new translation is not available in the translation metadata, hence
// it should count as one more.
$published = $new_translation;
foreach ($entity->translation as $translation) {
$published += $translation['status'];
// When creating a brand new translation, $entity->translation is not
// set.
if (!$new_translation) {
foreach ($entity->translation as $translation) {
$published += $translation['status'];
}
}
$enabled = $published > 1;
}
......@@ -467,7 +471,7 @@ function entityFormDeleteTranslation($form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$entity = $form_controller->getEntity();
$entity_type_id = $entity->getEntityTypeId();
$form_state->setRedirect('content_translation.delete_' . $entity_type_id, array(
$form_state->setRedirect('content_translation.translation_delete_' . $entity_type_id, array(
$entity_type_id => $entity->id(),
'language' => $form_controller->getFormLangcode($form_state),
));
......
......@@ -40,8 +40,8 @@ public function getFormId() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $_entity_type_id = NULL, $language = NULL) {
$this->entity = $this->getRequest()->attributes->get($_entity_type_id);
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $language = NULL) {
$this->entity = $this->getRequest()->attributes->get($entity_type_id);
$this->language = language_load($language);
return parent::buildForm($form, $form_state);
}
......
......@@ -57,7 +57,7 @@ protected function alterRoutes(RouteCollection $collection) {
$path,
array(
'_content' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
'_entity_type_id' => $entity_type_id,
'entity_type_id' => $entity_type_id,
),
array(
'_access_content_translation_overview' => $entity_type_id,
......@@ -66,9 +66,6 @@ protected function alterRoutes(RouteCollection $collection) {
array(
'_access_mode' => AccessManagerInterface::ACCESS_MODE_ANY,
'parameters' => array(
'entity' => array(
'type' => 'entity:' . $entity_type_id,
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
),
......@@ -85,7 +82,7 @@ protected function alterRoutes(RouteCollection $collection) {
'source' => NULL,
'target' => NULL,
'_title' => 'Add',
'_entity_type_id' => $entity_type_id,
'entity_type_id' => $entity_type_id,
),
array(
......@@ -95,8 +92,11 @@ protected function alterRoutes(RouteCollection $collection) {
array(
'_access_mode' => AccessManagerInterface::ACCESS_MODE_ANY,
'parameters' => array(
'entity' => array(
'type' => 'entity:' . $entity_type_id,
'source' => array(
'type' => 'language',
),
'target' => array(
'type' => 'language',
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
......@@ -113,7 +113,7 @@ protected function alterRoutes(RouteCollection $collection) {
'_content' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
'language' => NULL,
'_title' => 'Edit',
'_entity_type_id' => $entity_type_id,
'entity_type_id' => $entity_type_id,
),
array(
'_permission' => 'translate any entity',
......@@ -122,8 +122,8 @@ protected function alterRoutes(RouteCollection $collection) {
array(
'_access_mode' => AccessManagerInterface::ACCESS_MODE_ANY,
'parameters' => array(
'entity' => array(
'type' => 'entity:' . $entity_type_id,
'language' => array(
'type' => 'language',
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
......@@ -140,7 +140,7 @@ protected function alterRoutes(RouteCollection $collection) {
'_form' => '\Drupal\content_translation\Form\ContentTranslationDeleteForm',
'language' => NULL,
'_title' => 'Delete',
'_entity_type_id' => $entity_type_id,
'entity_type_id' => $entity_type_id,
),
array(
'_permission' => 'translate any entity',
......@@ -148,8 +148,8 @@ protected function alterRoutes(RouteCollection $collection) {
),
array(
'parameters' => array(
'entity' => array(
'type' => 'entity:' . $entity_type_id,
'language' => array(
'type' => 'language',
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
......@@ -159,7 +159,7 @@ protected function alterRoutes(RouteCollection $collection) {
'_admin_route' => $is_admin,
)
);
$collection->add("content_translation.delete_$entity_type_id", $route);
$collection->add("content_translation.translation_delete_$entity_type_id", $route);
}
}
......
<?php
/**
* @file
* Contains \Drupal\content_translation\Tests\Access\ContentTranslationManageAccessCheckTest.
*/
namespace Drupal\content_translation\Tests\Access;
use Drupal\content_translation\Access\ContentTranslationManageAccessCheck;
use Drupal\Core\Access\AccessInterface;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Tests for content translation manage check.
*
* @coversDefaultClass \Drupal\content_translation\Access\ContentTranslationManageAccessCheck
* @group content_translation
*/
class ContentTranslationManageAccessCheckTest extends UnitTestCase {
/**
* Tests the create access method.
*
* @covers ::access()
*/
public function testCreateAccess() {
// Set the mock translation handler.
$translation_handler = $this->getMock('\Drupal\content_translation\ContentTranslationHandlerInterface');
$translation_handler->expects($this->once())
->method('getTranslationAccess')
->will($this->returnValue(TRUE));
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->once())
->method('getController')
->withAnyParameters()
->will($this->returnValue($translation_handler));
// Set our source and target languages.
$source = 'en';
$target = 'it';
// Set the mock language manager.
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$language_manager->expects($this->at(0))
->method('getLanguages')
->will($this->returnValue(array('en' => array(), 'it' => array())));
$language_manager->expects($this->at(1))
->method('getLanguage')
->with($this->equalTo($source))
->will($this->returnValue(new Language(array('id' => 'en'))));
$language_manager->expects($this->at(2))
->method('getLanguage')
->with($this->equalTo($target))
->will($this->returnValue(new Language(array('id' => 'it'))));
// Set the mock entity. We need to use ContentEntityBase for mocking due to
// issues with phpunit and multiple interfaces.
$entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())
->method('getEntityTypeId');
$entity->expects($this->once())
->method('getTranslationLanguages')
->with()
->will($this->returnValue(array()));
// Set the route requirements.
$route = new Route('test_route');
$route->setRequirement('_access_content_translation_manage', 'create');
// Set the request attributes.
$request = Request::create('node/1');
$request->attributes->set('node', $entity);
// Set the mock account.
$account = $this->getMock('Drupal\Core\Session\AccountInterface');
// The access check under test.
$check = new ContentTranslationManageAccessCheck($entity_manager, $language_manager);
// The request params.
$language = 'en';
$entity_type_id = 'node';
$this->assertEquals($check->access($route, $request, $account, $source, $target, $language, $entity_type_id), AccessInterface::ALLOW, "The access check matches");
}
}
......@@ -17,3 +17,8 @@ services:
tags:
- { name: config.factory.override, priority: -254 }
- { name: event_subscriber }
language_converter:
class: Drupal\language\LanguageConverter
arguments: ['@language_manager']
tags:
- { name: paramconverter }
<?php
/**
* @file
* Contains \Drupal\language\LanguageConverter.
*/
namespace Drupal\language;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Converts parameters for upcasting entity IDs to full objects.
*/
class LanguageConverter implements ParamConverterInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new LanguageConverter.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(LanguageManagerInterface $language_manager) {
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function convert($value, $definition, $name, array $defaults, Request $request) {
if (!empty($value)) {
return $this->languageManager->getLanguage($value);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route) {
return (!empty($definition['type']) && $definition['type'] == 'language');
}
}
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