class ContentTranslationDeleteAccess

Same name in other branches
  1. 9 core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php \Drupal\content_translation\Access\ContentTranslationDeleteAccess
  2. 8.9.x core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php \Drupal\content_translation\Access\ContentTranslationDeleteAccess
  3. 10 core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php \Drupal\content_translation\Access\ContentTranslationDeleteAccess

Access check for entity translation deletion.

@internal This additional access checker only aims to prevent deletions in pending revisions until we are able to flag revision translations as deleted.

@todo Remove this in https://www.drupal.org/node/2945956.

Hierarchy

Expanded class hierarchy of ContentTranslationDeleteAccess

1 string reference to 'ContentTranslationDeleteAccess'
content_translation.services.yml in core/modules/content_translation/content_translation.services.yml
core/modules/content_translation/content_translation.services.yml
1 service uses ContentTranslationDeleteAccess
content_translation.delete_access in core/modules/content_translation/content_translation.services.yml
Drupal\content_translation\Access\ContentTranslationDeleteAccess

File

core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php, line 25

Namespace

Drupal\content_translation\Access
View source
class ContentTranslationDeleteAccess implements AccessInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The content translation manager.
     *
     * @var \Drupal\content_translation\ContentTranslationManagerInterface
     */
    protected $contentTranslationManager;
    
    /**
     * Constructs a ContentTranslationDeleteAccess object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
     *   The entity type manager.
     * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
     *   The content translation manager.
     */
    public function __construct(EntityTypeManagerInterface $manager, ContentTranslationManagerInterface $content_translation_manager) {
        $this->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;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ContentTranslationDeleteAccess::$contentTranslationManager protected property The content translation manager.
ContentTranslationDeleteAccess::$entityTypeManager protected property The entity type manager.
ContentTranslationDeleteAccess::access public function Checks access to translation deletion for the specified route match.
ContentTranslationDeleteAccess::checkAccess public function Checks access to translation deletion for the specified entity.
ContentTranslationDeleteAccess::__construct public function Constructs a ContentTranslationDeleteAccess object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.