ContentTranslationManageAccessCheck.php in Drupal 8
File
core/modules/content_translation/src/Access/ContentTranslationManageAccessCheck.php
View source
<?php
namespace Drupal\content_translation\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
class ContentTranslationManageAccessCheck implements AccessInterface {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $entityTypeManager;
protected $languageManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) {
if ($entity = $route_match
->getParameter($entity_type_id)) {
$operation = $route
->getRequirement('_access_content_translation_manage');
$language = $this->languageManager
->getLanguage($language) ?: $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if (in_array($operation, [
'update',
'delete',
])) {
if ($language
->getId() == $entity
->getUntranslated()
->language()
->getId()) {
return AccessResult::forbidden()
->addCacheableDependency($entity);
}
$templates = [
'update' => 'edit-form',
'delete' => 'delete-form',
];
if ($entity
->access($operation) && $entity_type
->hasLinkTemplate($templates[$operation])) {
return AccessResult::forbidden()
->cachePerPermissions();
}
}
if ($account
->hasPermission('translate any entity')) {
return AccessResult::allowed()
->cachePerPermissions();
}
switch ($operation) {
case 'create':
$handler = $this->entityTypeManager
->getHandler($entity
->getEntityTypeId(), 'translation');
$translations = $entity
->getTranslationLanguages();
$languages = $this->languageManager
->getLanguages();
$source_language = $this->languageManager
->getLanguage($source) ?: $entity
->language();
$target_language = $this->languageManager
->getLanguage($target) ?: $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$is_new_translation = $source_language
->getId() != $target_language
->getId() && isset($languages[$source_language
->getId()]) && isset($languages[$target_language
->getId()]) && !isset($translations[$target_language
->getId()]);
return AccessResult::allowedIf($is_new_translation)
->cachePerPermissions()
->addCacheableDependency($entity)
->andIf($handler
->getTranslationAccess($entity, $operation));
case 'delete':
$delete_access = \Drupal::service('content_translation.delete_access')
->checkAccess($entity);
$access = $this
->checkAccess($entity, $language, $operation);
return $delete_access
->andIf($access);
case 'update':
return $this
->checkAccess($entity, $language, $operation);
}
}
return AccessResult::neutral();
}
protected function checkAccess(ContentEntityInterface $entity, LanguageInterface $language, $operation) {
$handler = $this->entityTypeManager
->getHandler($entity
->getEntityTypeId(), 'translation');
$translations = $entity
->getTranslationLanguages();
$languages = $this->languageManager
->getLanguages();
$has_translation = isset($languages[$language
->getId()]) && $language
->getId() != $entity
->getUntranslated()
->language()
->getId() && isset($translations[$language
->getId()]);
return AccessResult::allowedIf($has_translation)
->cachePerPermissions()
->addCacheableDependency($entity)
->andIf($handler
->getTranslationAccess($entity, $operation));
}
}