isVersionable example


  protected $entity;

  /** * {@inheritdoc} */
  public static function createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL) {
    $resource_object = new static(
      $entity,
      $resource_type,
      $entity->uuid(),
      $resource_type->isVersionable() && $entity instanceof RevisionableInterface ? $entity->getRevisionId() : NULL,
      static::extractFieldsFromEntity($resource_type$entity),
      static::buildLinksFromEntity($resource_type$entity$links ?: new LinkCollection([]))
    );
    $resource_object->setEntity($entity);
    return $resource_object;
  }

  /** * Gets the decorated entity. * * @return \Drupal\Core\Entity\EntityInterface * The label for which to only normalize its label. */
return [];
    }
    $public_field_name = $resource_relationship->getPublicName();
    $relationship_route_name = Routes::getRouteName($resource_type, "$public_field_name.relationship.get");
    $links = [
      'self' => Url::fromRoute($relationship_route_name['entity' => $relationship_context->getId()]),
    ];
    if (static::hasNonInternalResourceType($resource_type->getRelatableResourceTypesByField($public_field_name))) {
      $related_route_name = Routes::getRouteName($resource_type, "$public_field_name.related");
      $links['related'] = Url::fromRoute($related_route_name['entity' => $relationship_context->getId()]);
    }
    if ($resource_type->isVersionable()) {
      $version_query_parameter = [JsonApiSpec::VERSION_QUERY_PARAMETER => $relationship_context->getVersionIdentifier()];
      $links['self']->setOption('query', $version_query_parameter);
      if (isset($links['related'])) {
        $links['related']->setOption('query', $version_query_parameter);
      }
    }
    return $links;
  }

  /** * Determines if a given list of resource types contains a non-internal type. * * @param \Drupal\jsonapi\ResourceType\ResourceType[] $resource_types * The JSON:API resource types to evaluate. * * @return bool * FALSE if every resource type is internal, TRUE otherwise. */

  public function enhance(array $defaults, Request $request) {
    if (!Routes::isJsonApiRequest($defaults) || !($resource_type = Routes::getResourceTypeNameFromParameters($defaults))) {
      return $defaults;
    }

    $has_version_param = $request->query->has(static::RESOURCE_VERSION_QUERY_PARAMETER);

    // If the resource type is not versionable, then nothing needs to be     // enhanced.     if (!$resource_type->isVersionable()) {
      // If the query parameter was provided but the resource type is not       // versionable, provide a helpful error.       if ($has_version_param) {
        $cacheability = (new CacheableMetadata())->addCacheContexts(['url.path', static::CACHE_CONTEXT]);
        throw new CacheableHttpException($cacheability, 501, 'Resource versioning is not yet supported for this resource type.');
      }
      return $defaults;
    }

    // Since the resource type is versionable, responses must always vary by the     // requested version, without regard for whether a version query parameter
$response = $this->respondWithCollection($primary_data$this->getIncludes($request$primary_data)$request$resource_type$params[OffsetPage::KEY_NAME]);

    $response->addCacheableDependency($query_cacheability);
    $response->addCacheableDependency($count_query_cacheability);
    $response->addCacheableDependency((new CacheableMetadata())
      ->addCacheContexts([
        'url.query_args:filter',
        'url.query_args:sort',
        'url.query_args:page',
      ]));

    if ($resource_type->isVersionable()) {
      $response->addCacheableDependency((new CacheableMetadata())->addCacheContexts([ResourceVersionRouteEnhancer::CACHE_CONTEXT]));
    }

    return $response;
  }

  /** * Executes the query in a render context, to catch bubbled cacheability. * * @param \Drupal\Core\Entity\Query\QueryInterface $query * The query to execute to get the return results. * @param \Drupal\Core\Cache\CacheableMetadata $query_cacheability * The value object to carry the query cacheability. * * @return int|array * Returns an integer for count queries or an array of IDs. The values of * the array are always entity IDs. The keys will be revision IDs if the * entity supports revision and entity IDs if not. * * @see node_query_node_access_alter() * @see https://www.drupal.org/project/drupal/issues/2557815 * @see https://www.drupal.org/project/drupal/issues/2794385 * @todo Remove this after https://www.drupal.org/project/drupal/issues/3028976 is fixed. */

  protected function createResourceType(EntityTypeInterface $entity_type$bundle) {
    $resource_type = parent::createResourceType($entity_type$bundle);
    return new CountableResourceType(
      $resource_type->getEntityTypeId(),
      $resource_type->getBundle(),
      $resource_type->getDeserializationTargetClass(),
      $resource_type->isInternal(),
      $resource_type->isLocatable(),
      $resource_type->isMutable(),
      $resource_type->isVersionable(),
      $resource_type->getFields(),
      $resource_type->getTypeName()
    );
  }

}

  public function __construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type$id$revision_id, array $fields, LinkCollection $links, LanguageInterface $language = NULL) {
    assert(is_null($revision_id) || $resource_type->isVersionable());
    $this->setCacheability($cacheability);
    $this->resourceType = $resource_type;
    $this->resourceIdentifier = new ResourceIdentifier($resource_type$id);
    $this->versionIdentifier = $revision_id ? 'id:' . $revision_id : NULL;
    $this->fields = $fields;
    $this->links = $links->withContext($this);

    // If the specified language empty it falls back the same way as in the entity system     // @see \Drupal\Core\Entity\EntityBase::language()     $this->language = $language ?: new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
  }

  

  protected static function buildLinkCollectionFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links) {
    $context_resource_type = $context->getResourceType();
    $public_field_name = $context_resource_type->getPublicName($field->getName());
    if ($context_resource_type->isLocatable() && !$context_resource_type->isInternal()) {
      $context_is_versionable = $context_resource_type->isVersionable();
      if (!$links->hasLinkWithKey('self')) {
        $route_name = Routes::getRouteName($context_resource_type, "$public_field_name.relationship.get");
        $self_link = Url::fromRoute($route_name['entity' => $context->getId()]);
        if ($context_is_versionable) {
          $self_link->setOption('query', [JsonApiSpec::VERSION_QUERY_PARAMETER => $context->getVersionIdentifier()]);
        }
        $links = $links->withLink('self', new Link(new CacheableMetadata()$self_link, 'self'));
      }
      $has_non_internal_resource_type = array_reduce($context_resource_type->getRelatableResourceTypesByField($public_field_name)function D$carry, ResourceType $target) {
        return $carry ?: !$target->isInternal();
      }, FALSE);
      
Home | Imprint | This part of the site doesn't use cookies.