X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcontent_translation%2Fsrc%2FAccess%2FContentTranslationDeleteAccess.php;fp=web%2Fcore%2Fmodules%2Fcontent_translation%2Fsrc%2FAccess%2FContentTranslationDeleteAccess.php;h=5f1933714bcabe7561c849bb4a0a83e132edd32f;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php b/web/core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php new file mode 100644 index 000000000..5f1933714 --- /dev/null +++ b/web/core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php @@ -0,0 +1,124 @@ +entityTypeManager = $manager; + $this->contentTranslationManager = $content_translation_manager; + } + + /** + * Checks access to translation deletion for the specified route match. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The parameterized route. + * @param \Drupal\Core\Session\AccountInterface $account + * The currently logged in account. + * + * @return \Drupal\Core\Access\AccessResultInterface + * The access result. + */ + public function access(RouteMatchInterface $route_match, AccountInterface $account) { + $requirement = $route_match->getRouteObject()->getRequirement('_access_content_translation_delete'); + $entity_type_id = current(explode('.', $requirement)); + $entity = $route_match->getParameter($entity_type_id); + return $this->checkAccess($entity); + } + + /** + * Checks access to translation deletion for the specified entity. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity translation to be deleted. + * + * @return \Drupal\Core\Access\AccessResultInterface + * The access result. + */ + public function checkAccess(ContentEntityInterface $entity) { + $result = AccessResult::allowed(); + + $entity_type_id = $entity->getEntityTypeId(); + $result->addCacheableDependency($entity); + // Add the cache dependencies used by + // ContentTranslationManager::isPendingRevisionSupportEnabled(). + if (\Drupal::moduleHandler()->moduleExists('content_moderation')) { + foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) { + $result->addCacheableDependency($workflow); + } + } + if (!ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle())) { + return $result; + } + + if ($entity->isDefaultTranslation()) { + return $result; + } + + $config = ContentLanguageSettings::load($entity_type_id . '.' . $entity->bundle()); + $result->addCacheableDependency($config); + if (!$this->contentTranslationManager->isEnabled($entity_type_id, $entity->bundle())) { + return $result; + } + + /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ + $storage = $this->entityTypeManager->getStorage($entity_type_id); + $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId()); + if (!$revision_id) { + return $result; + } + + /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */ + $revision = $storage->loadRevision($revision_id); + if ($revision->wasDefaultRevision()) { + return $result; + } + + $result = $result->andIf(AccessResult::forbidden()); + return $result; + } + +}