annotate core/modules/content_translation/src/Access/ContentTranslationDeleteAccess.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\content_translation\Access;
Chris@14 4
Chris@14 5 use Drupal\content_translation\ContentTranslationManager;
Chris@14 6 use Drupal\content_translation\ContentTranslationManagerInterface;
Chris@14 7 use Drupal\Core\Access\AccessResult;
Chris@14 8 use Drupal\Core\Entity\ContentEntityInterface;
Chris@14 9 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@14 10 use Drupal\Core\Routing\Access\AccessInterface;
Chris@14 11 use Drupal\Core\Routing\RouteMatchInterface;
Chris@14 12 use Drupal\Core\Session\AccountInterface;
Chris@14 13 use Drupal\language\Entity\ContentLanguageSettings;
Chris@14 14 use Drupal\workflows\Entity\Workflow;
Chris@14 15
Chris@14 16 /**
Chris@14 17 * Access check for entity translation deletion.
Chris@14 18 *
Chris@14 19 * @internal This additional access checker only aims to prevent deletions in
Chris@14 20 * pending revisions until we are able to flag revision translations as
Chris@14 21 * deleted.
Chris@14 22 *
Chris@14 23 * @todo Remove this in https://www.drupal.org/node/2945956.
Chris@14 24 */
Chris@14 25 class ContentTranslationDeleteAccess implements AccessInterface {
Chris@14 26
Chris@14 27 /**
Chris@14 28 * The entity type manager.
Chris@14 29 *
Chris@14 30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@14 31 */
Chris@14 32 protected $entityTypeManager;
Chris@14 33
Chris@14 34 /**
Chris@14 35 * The content translation manager.
Chris@14 36 *
Chris@14 37 * @var \Drupal\content_translation\ContentTranslationManagerInterface
Chris@14 38 */
Chris@14 39 protected $contentTranslationManager;
Chris@14 40
Chris@14 41 /**
Chris@14 42 * Constructs a ContentTranslationDeleteAccess object.
Chris@14 43 *
Chris@14 44 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
Chris@14 45 * The entity type manager.
Chris@14 46 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
Chris@14 47 * The content translation manager.
Chris@14 48 */
Chris@14 49 public function __construct(EntityTypeManagerInterface $manager, ContentTranslationManagerInterface $content_translation_manager) {
Chris@14 50 $this->entityTypeManager = $manager;
Chris@14 51 $this->contentTranslationManager = $content_translation_manager;
Chris@14 52 }
Chris@14 53
Chris@14 54 /**
Chris@14 55 * Checks access to translation deletion for the specified route match.
Chris@14 56 *
Chris@14 57 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@14 58 * The parameterized route.
Chris@14 59 * @param \Drupal\Core\Session\AccountInterface $account
Chris@14 60 * The currently logged in account.
Chris@14 61 *
Chris@14 62 * @return \Drupal\Core\Access\AccessResultInterface
Chris@14 63 * The access result.
Chris@14 64 */
Chris@14 65 public function access(RouteMatchInterface $route_match, AccountInterface $account) {
Chris@14 66 $requirement = $route_match->getRouteObject()->getRequirement('_access_content_translation_delete');
Chris@14 67 $entity_type_id = current(explode('.', $requirement));
Chris@14 68 $entity = $route_match->getParameter($entity_type_id);
Chris@14 69 return $this->checkAccess($entity);
Chris@14 70 }
Chris@14 71
Chris@14 72 /**
Chris@14 73 * Checks access to translation deletion for the specified entity.
Chris@14 74 *
Chris@14 75 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
Chris@14 76 * The entity translation to be deleted.
Chris@14 77 *
Chris@14 78 * @return \Drupal\Core\Access\AccessResultInterface
Chris@14 79 * The access result.
Chris@14 80 */
Chris@14 81 public function checkAccess(ContentEntityInterface $entity) {
Chris@14 82 $result = AccessResult::allowed();
Chris@14 83
Chris@14 84 $entity_type_id = $entity->getEntityTypeId();
Chris@14 85 $result->addCacheableDependency($entity);
Chris@14 86 // Add the cache dependencies used by
Chris@14 87 // ContentTranslationManager::isPendingRevisionSupportEnabled().
Chris@14 88 if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
Chris@14 89 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
Chris@14 90 $result->addCacheableDependency($workflow);
Chris@14 91 }
Chris@14 92 }
Chris@14 93 if (!ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle())) {
Chris@14 94 return $result;
Chris@14 95 }
Chris@14 96
Chris@14 97 if ($entity->isDefaultTranslation()) {
Chris@14 98 return $result;
Chris@14 99 }
Chris@14 100
Chris@14 101 $config = ContentLanguageSettings::load($entity_type_id . '.' . $entity->bundle());
Chris@14 102 $result->addCacheableDependency($config);
Chris@14 103 if (!$this->contentTranslationManager->isEnabled($entity_type_id, $entity->bundle())) {
Chris@14 104 return $result;
Chris@14 105 }
Chris@14 106
Chris@14 107 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
Chris@14 108 $storage = $this->entityTypeManager->getStorage($entity_type_id);
Chris@14 109 $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId());
Chris@14 110 if (!$revision_id) {
Chris@14 111 return $result;
Chris@14 112 }
Chris@14 113
Chris@14 114 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
Chris@14 115 $revision = $storage->loadRevision($revision_id);
Chris@14 116 if ($revision->wasDefaultRevision()) {
Chris@14 117 return $result;
Chris@14 118 }
Chris@14 119
Chris@14 120 $result = $result->andIf(AccessResult::forbidden());
Chris@14 121 return $result;
Chris@14 122 }
Chris@14 123
Chris@14 124 }