You are here

class VersionByRel in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Revisions/VersionByRel.php \Drupal\jsonapi\Revisions\VersionByRel

Revision ID implementation for the default or latest revisions.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

Expanded class hierarchy of VersionByRel

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

2 files declare their use of VersionByRel
ResourceObject.php in core/modules/jsonapi/src/JsonApiResource/ResourceObject.php
VersionNegotiatorTest.php in core/modules/jsonapi/tests/src/Kernel/Revisions/VersionNegotiatorTest.php
1 string reference to 'VersionByRel'
jsonapi.services.yml in core/modules/jsonapi/jsonapi.services.yml
core/modules/jsonapi/jsonapi.services.yml
1 service uses VersionByRel
jsonapi.version_negotiator.rel in core/modules/jsonapi/jsonapi.services.yml
Drupal\jsonapi\Revisions\VersionByRel

File

core/modules/jsonapi/src/Revisions/VersionByRel.php, line 17

Namespace

Drupal\jsonapi\Revisions
View source
class VersionByRel extends NegotiatorBase {

  /**
   * Version argument which loads the revision known to be the "working copy".
   *
   * In Drupal terms, a "working copy" is the latest revision. It may or may not
   * be a "default" revision. This revision is the working copy because it is
   * the revision to which new work will be applied. In other words, it denotes
   * the most recent revision which might be considered a work-in-progress.
   *
   * @var string
   */
  const WORKING_COPY = 'working-copy';

  /**
   * Version argument which loads the revision known to be the "latest version".
   *
   * In Drupal terms, the "latest version" is the latest "default" revision. It
   * may or may not have later revisions after it, as long as none of them are
   * "default" revisions. This revision is the latest version because it is the
   * last revision where work was considered finished. Typically, this means
   * that it is the most recent "published" revision.
   *
   * @var string
   */
  const LATEST_VERSION = 'latest-version';

  /**
   * {@inheritdoc}
   */
  protected function getRevisionId(EntityInterface $entity, $version_argument) {
    assert($entity instanceof RevisionableInterface);
    switch ($version_argument) {
      case static::WORKING_COPY:

        /* @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
        $entity_storage = $this->entityTypeManager
          ->getStorage($entity
          ->getEntityTypeId());
        return static::ensureVersionExists($entity_storage
          ->getLatestRevisionId($entity
          ->id()));
      case static::LATEST_VERSION:

        // The already loaded revision will be the latest version by default.
        // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery().
        return $entity
          ->getLoadedRevisionId();
      default:
        $message = sprintf('The version specifier must be either `%s` or `%s`, `%s` given.', static::LATEST_VERSION, static::WORKING_COPY, $version_argument);
        throw new InvalidVersionIdentifierException($message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NegotiatorBase::$entityTypeManager protected property The entity type manager to load the revision.
NegotiatorBase::ensureVersionExists protected static function Helper method that ensures that a version exists.
NegotiatorBase::getRevision public function Gets the identified revision. Overrides VersionNegotiatorInterface::getRevision
NegotiatorBase::loadRevision protected function Loads an entity revision.
NegotiatorBase::__construct public function Constructs a version negotiator instance.
VersionByRel::getRevisionId protected function Gets the revision ID. Overrides NegotiatorBase::getRevisionId
VersionByRel::LATEST_VERSION constant Version argument which loads the revision known to be the "latest version".
VersionByRel::WORKING_COPY constant Version argument which loads the revision known to be the "working copy".