Chris@18: entityTypeManager = $entity_type_manager; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the revision ID. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity. Chris@18: * @param string $version_argument Chris@18: * A value used to derive a revision ID for the given entity. Chris@18: * Chris@18: * @return int Chris@18: * The revision ID. Chris@18: * Chris@18: * @throws \Drupal\jsonapi\Revisions\VersionNotFoundException Chris@18: * When the revision does not exist. Chris@18: * @throws \Drupal\jsonapi\Revisions\InvalidVersionIdentifierException Chris@18: * When the revision ID is not valid. Chris@18: */ Chris@18: abstract protected function getRevisionId(EntityInterface $entity, $version_argument); Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getRevision(EntityInterface $entity, $version_argument) { Chris@18: return $this->loadRevision($entity, $this->getRevisionId($entity, $version_argument)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Loads an entity revision. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity for which to load a revision. Chris@18: * @param int $revision_id Chris@18: * The revision ID to be loaded. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityInterface|null Chris@18: * The revision or NULL if the revision does not exists. Chris@18: * Chris@18: * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException Chris@18: * Thrown if the entity type doesn't exist. Chris@18: * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Chris@18: * Thrown if the storage handler couldn't be loaded. Chris@18: */ Chris@18: protected function loadRevision(EntityInterface $entity, $revision_id) { Chris@18: $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); Chris@18: $revision = static::ensureVersionExists($storage->loadRevision($revision_id)); Chris@18: if ($revision->id() !== $entity->id()) { Chris@18: throw new VersionNotFoundException(sprintf('The requested resource does not have a version with ID %s.', $revision_id)); Chris@18: } Chris@18: return $revision; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Helper method that ensures that a version exists. Chris@18: * Chris@18: * @param int|\Drupal\Core\Entity\EntityInterface $revision Chris@18: * A revision ID, or NULL if one was not found. Chris@18: * Chris@18: * @return int|\Drupal\Core\Entity\EntityInterface Chris@18: * A revision or revision ID, if one was found. Chris@18: * Chris@18: * @throws \Drupal\jsonapi\Revisions\VersionNotFoundException Chris@18: * Thrown if the given value is NULL, meaning the requested version was not Chris@18: * found. Chris@18: */ Chris@18: protected static function ensureVersionExists($revision) { Chris@18: if (is_null($revision)) { Chris@18: throw new VersionNotFoundException(); Chris@18: } Chris@18: return $revision; Chris@18: } Chris@18: Chris@18: }