annotate core/modules/jsonapi/src/Revisions/VersionByRel.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\jsonapi\Revisions;
Chris@5 4
Chris@5 5 use Drupal\Core\Entity\EntityInterface;
Chris@5 6 use Drupal\Core\Entity\RevisionableInterface;
Chris@5 7
Chris@5 8 /**
Chris@5 9 * Revision ID implementation for the default or latest revisions.
Chris@5 10 *
Chris@5 11 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
Chris@5 12 * class may change at any time and this will break any dependencies on it.
Chris@5 13 *
Chris@5 14 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@5 15 * @see jsonapi.api.php
Chris@5 16 */
Chris@5 17 class VersionByRel extends NegotiatorBase {
Chris@5 18
Chris@5 19 /**
Chris@5 20 * Version argument which loads the revision known to be the "working copy".
Chris@5 21 *
Chris@5 22 * In Drupal terms, a "working copy" is the latest revision. It may or may not
Chris@5 23 * be a "default" revision. This revision is the working copy because it is
Chris@5 24 * the revision to which new work will be applied. In other words, it denotes
Chris@5 25 * the most recent revision which might be considered a work-in-progress.
Chris@5 26 *
Chris@5 27 * @var string
Chris@5 28 */
Chris@5 29 const WORKING_COPY = 'working-copy';
Chris@5 30
Chris@5 31 /**
Chris@5 32 * Version argument which loads the revision known to be the "latest version".
Chris@5 33 *
Chris@5 34 * In Drupal terms, the "latest version" is the latest "default" revision. It
Chris@5 35 * may or may not have later revisions after it, as long as none of them are
Chris@5 36 * "default" revisions. This revision is the latest version because it is the
Chris@5 37 * last revision where work was considered finished. Typically, this means
Chris@5 38 * that it is the most recent "published" revision.
Chris@5 39 *
Chris@5 40 * @var string
Chris@5 41 */
Chris@5 42 const LATEST_VERSION = 'latest-version';
Chris@5 43
Chris@5 44 /**
Chris@5 45 * {@inheritdoc}
Chris@5 46 */
Chris@5 47 protected function getRevisionId(EntityInterface $entity, $version_argument) {
Chris@5 48 assert($entity instanceof RevisionableInterface);
Chris@5 49 switch ($version_argument) {
Chris@5 50 case static::WORKING_COPY:
Chris@5 51 /* @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
Chris@5 52 $entity_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
Chris@5 53 return static::ensureVersionExists($entity_storage->getLatestRevisionId($entity->id()));
Chris@5 54
Chris@5 55 case static::LATEST_VERSION:
Chris@5 56 // The already loaded revision will be the latest version by default.
Chris@5 57 // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery().
Chris@5 58 return $entity->getLoadedRevisionId();
Chris@5 59
Chris@5 60 default:
Chris@5 61 $message = sprintf('The version specifier must be either `%s` or `%s`, `%s` given.', static::LATEST_VERSION, static::WORKING_COPY, $version_argument);
Chris@5 62 throw new InvalidVersionIdentifierException($message);
Chris@5 63 }
Chris@5 64 }
Chris@5 65
Chris@5 66 }