3 namespace Drupal\content_translation\Access;
5 use Drupal\content_translation\ContentTranslationManager;
6 use Drupal\content_translation\ContentTranslationManagerInterface;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Entity\ContentEntityInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Routing\Access\AccessInterface;
11 use Drupal\Core\Routing\RouteMatchInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\language\Entity\ContentLanguageSettings;
14 use Drupal\workflows\Entity\Workflow;
17 * Access check for entity translation deletion.
19 * @internal This additional access checker only aims to prevent deletions in
20 * pending revisions until we are able to flag revision translations as
23 * @todo Remove this in https://www.drupal.org/node/2945956.
25 class ContentTranslationDeleteAccess implements AccessInterface {
28 * The entity type manager.
30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32 protected $entityTypeManager;
35 * The content translation manager.
37 * @var \Drupal\content_translation\ContentTranslationManagerInterface
39 protected $contentTranslationManager;
42 * Constructs a ContentTranslationDeleteAccess object.
44 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
45 * The entity type manager.
46 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
47 * The content translation manager.
49 public function __construct(EntityTypeManagerInterface $manager, ContentTranslationManagerInterface $content_translation_manager) {
50 $this->entityTypeManager = $manager;
51 $this->contentTranslationManager = $content_translation_manager;
55 * Checks access to translation deletion for the specified route match.
57 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
58 * The parameterized route.
59 * @param \Drupal\Core\Session\AccountInterface $account
60 * The currently logged in account.
62 * @return \Drupal\Core\Access\AccessResultInterface
65 public function access(RouteMatchInterface $route_match, AccountInterface $account) {
66 $requirement = $route_match->getRouteObject()->getRequirement('_access_content_translation_delete');
67 $entity_type_id = current(explode('.', $requirement));
68 $entity = $route_match->getParameter($entity_type_id);
69 return $this->checkAccess($entity);
73 * Checks access to translation deletion for the specified entity.
75 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
76 * The entity translation to be deleted.
78 * @return \Drupal\Core\Access\AccessResultInterface
81 public function checkAccess(ContentEntityInterface $entity) {
82 $result = AccessResult::allowed();
84 $entity_type_id = $entity->getEntityTypeId();
85 $result->addCacheableDependency($entity);
86 // Add the cache dependencies used by
87 // ContentTranslationManager::isPendingRevisionSupportEnabled().
88 if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
89 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
90 $result->addCacheableDependency($workflow);
93 if (!ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle())) {
97 if ($entity->isDefaultTranslation()) {
101 $config = ContentLanguageSettings::load($entity_type_id . '.' . $entity->bundle());
102 $result->addCacheableDependency($config);
103 if (!$this->contentTranslationManager->isEnabled($entity_type_id, $entity->bundle())) {
107 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
108 $storage = $this->entityTypeManager->getStorage($entity_type_id);
109 $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
114 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
115 $revision = $storage->loadRevision($revision_id);
116 if ($revision->wasDefaultRevision()) {
120 $result = $result->andIf(AccessResult::forbidden());