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