Chris@0: entityManager = $manager; Chris@0: $this->languageManager = $language_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks translation access for the entity and operation on the given route. Chris@0: * Chris@0: * @param \Symfony\Component\Routing\Route $route Chris@0: * The route to check against. Chris@0: * @param \Drupal\Core\Routing\RouteMatchInterface $route_match Chris@0: * The parametrized route. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The currently logged in account. Chris@0: * @param string $source Chris@0: * (optional) For a create operation, the language code of the source. Chris@0: * @param string $target Chris@0: * (optional) For a create operation, the language code of the translation. Chris@0: * @param string $language Chris@0: * (optional) For an update or delete operation, the language code of the Chris@0: * translation being updated or deleted. Chris@0: * @param string $entity_type_id Chris@0: * (optional) The entity type ID. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * The access result. Chris@0: */ Chris@0: public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) { Chris@0: /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ Chris@0: if ($entity = $route_match->getParameter($entity_type_id)) { Chris@0: $operation = $route->getRequirement('_access_content_translation_manage'); Chris@0: $language = $this->languageManager->getLanguage($language) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); Chris@0: $entity_type = $this->entityManager->getDefinition($entity_type_id); Chris@0: Chris@0: if (in_array($operation, ['update', 'delete'])) { Chris@0: // Translation operations cannot be performed on the default Chris@0: // translation. Chris@0: if ($language->getId() == $entity->getUntranslated()->language()->getId()) { Chris@0: return AccessResult::forbidden()->addCacheableDependency($entity); Chris@0: } Chris@0: // Editors have no access to the translation operations, as entity Chris@0: // access already grants them an equal or greater access level. Chris@0: $templates = ['update' => 'edit-form', 'delete' => 'delete-form']; Chris@0: if ($entity->access($operation) && $entity_type->hasLinkTemplate($templates[$operation])) { Chris@0: return AccessResult::forbidden()->cachePerPermissions(); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($account->hasPermission('translate any entity')) { Chris@0: return AccessResult::allowed()->cachePerPermissions(); Chris@0: } Chris@0: Chris@0: /* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */ Chris@0: $handler = $this->entityManager->getHandler($entity->getEntityTypeId(), 'translation'); Chris@0: Chris@0: // Load translation. Chris@0: $translations = $entity->getTranslationLanguages(); Chris@0: $languages = $this->languageManager->getLanguages(); Chris@0: Chris@0: switch ($operation) { Chris@0: case 'create': Chris@0: $source_language = $this->languageManager->getLanguage($source) ?: $entity->language(); Chris@0: $target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); Chris@0: $is_new_translation = ($source_language->getId() != $target_language->getId() Chris@0: && isset($languages[$source_language->getId()]) Chris@0: && isset($languages[$target_language->getId()]) Chris@0: && !isset($translations[$target_language->getId()])); Chris@0: return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->addCacheableDependency($entity) Chris@0: ->andIf($handler->getTranslationAccess($entity, $operation)); Chris@0: Chris@0: case 'delete': Chris@0: case 'update': Chris@0: $has_translation = isset($languages[$language->getId()]) Chris@0: && $language->getId() != $entity->getUntranslated()->language()->getId() Chris@0: && isset($translations[$language->getId()]); Chris@0: return AccessResult::allowedIf($has_translation)->cachePerPermissions()->addCacheableDependency($entity) Chris@0: ->andIf($handler->getTranslationAccess($entity, $operation)); Chris@0: } Chris@0: } Chris@0: Chris@0: // No opinion. Chris@0: return AccessResult::neutral(); Chris@0: } Chris@0: Chris@0: }