ensureVersionExists example

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);
    }
  }

}

  protected function loadRevision(EntityInterface $entity$revision_id) {
    $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
    $revision = static::ensureVersionExists($storage->loadRevision($revision_id));
    if ($revision->id() !== $entity->id()) {
      throw new VersionNotFoundException(sprintf('The requested resource does not have a version with ID %s.', $revision_id));
    }
    return $revision;
  }

  /** * Helper method that ensures that a version exists. * * @param int|\Drupal\Core\Entity\EntityInterface $revision * A revision ID, or NULL if one was not found. * * @return int|\Drupal\Core\Entity\EntityInterface * A revision or revision ID, if one was found. * * @throws \Drupal\jsonapi\Revisions\VersionNotFoundException * Thrown if the given value is NULL, meaning the requested version was not * found. */
Home | Imprint | This part of the site doesn't use cookies.