You are here

class LatestRevisionCheck in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Access/LatestRevisionCheck.php \Drupal\workbench_moderation\Access\LatestRevisionCheck

Define class for revision checks.

Hierarchy

Expanded class hierarchy of LatestRevisionCheck

1 file declares its use of LatestRevisionCheck
LatestRevisionCheckTest.php in tests/src/Unit/LatestRevisionCheckTest.php
1 string reference to 'LatestRevisionCheck'
workbench_moderation.services.yml in ./workbench_moderation.services.yml
workbench_moderation.services.yml
1 service uses LatestRevisionCheck
access_check.latest_revision in ./workbench_moderation.services.yml
Drupal\workbench_moderation\Access\LatestRevisionCheck

File

src/Access/LatestRevisionCheck.php, line 15

Namespace

Drupal\workbench_moderation\Access
View source
class LatestRevisionCheck implements AccessInterface {

  /**
   * Moderation information.
   *
   * @var \Drupal\workbench_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * Constructs a new LatestRevisionCheck.
   *
   * @param \Drupal\workbench_moderation\ModerationInformationInterface $moderation_information
   *   The moderation information service.
   */
  public function __construct(ModerationInformationInterface $moderation_information) {
    $this->moderationInfo = $moderation_information;
  }

  /**
   * Checks that there is a forward revision available.
   *
   * This checker assumes the presence of an '_entity_access' requirement key
   * in the same form as used by EntityAccessCheck.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   *
   * @see EntityAccessCheck
   */
  public function access(Route $route, RouteMatchInterface $route_match) {

    // This tab should not show up period unless there's a reason to show it.
    // @todo Do we need any extra cache tags here?
    $entity = $this
      ->loadEntity($route, $route_match);
    return $this->moderationInfo
      ->hasForwardRevision($entity) ? AccessResult::allowed()
      ->addCacheableDependency($entity) : AccessResult::forbidden()
      ->addCacheableDependency($entity);
  }

  /**
   * Returns the default revision of the entity this route is for.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface
   *   returns the Entity in question.
   *
   * @throws \Exception
   *   A generic exception is thrown if the entity couldn't be loaded. This
   *   almost always implies a developer error, so it should get turned into
   *   an HTTP 500.
   */
  protected function loadEntity(Route $route, RouteMatchInterface $route_match) {
    $entity_type = $route
      ->getOption('_workbench_moderation_entity_type');
    if ($entity = $route_match
      ->getParameter($entity_type)) {
      if ($entity instanceof EntityInterface) {
        return $entity;
      }
    }
    throw new \Exception(sprintf('%s is not a valid entity route. The LatestRevisionCheck access checker may only be used with a route that has a single entity parameter.', $route_match
      ->getRouteName()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LatestRevisionCheck::$moderationInfo protected property Moderation information.
LatestRevisionCheck::access public function Checks that there is a forward revision available.
LatestRevisionCheck::loadEntity protected function Returns the default revision of the entity this route is for.
LatestRevisionCheck::__construct public function Constructs a new LatestRevisionCheck.