hasLinkWithKey example


  public function getMergedLinks(LinkCollection $top_level_links) {
    // When directly fetching a relationship object, the relationship object's     // links become the top-level object's links unless they've been     // overridden. Overrides are especially important for the `self` link, which     // must match the link that generated the response. For example, the     // top-level `self` link might have an `include` query parameter that would     // be lost otherwise.     // See https://jsonapi.org/format/#fetching-relationships-responses-200 and     // https://jsonapi.org/format/#document-top-level.     return LinkCollection::merge($top_level_links$this->getLinks()->filter(function D$key) use ($top_level_links) {
      return !$top_level_links->hasLinkWithKey($key);
    })->withContext($top_level_links->getContext()));
  }

  /** * {@inheritdoc} */
  public function getMergedMeta(array $top_level_meta) {
    return NestedArray::mergeDeep($top_level_meta$this->getMeta());
  }

  /** * Builds a LinkCollection for the given entity reference field. * * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $context * The context resource object of the relationship object. * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field * The entity reference field from which to create the links. * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links * Any extra links for the Relationship, if a `self` link is not provided, * one will be automatically added if the context resource is locatable and * is not internal. * * @return \Drupal\jsonapi\JsonApiResource\LinkCollection * The built links. */

  protected function buildWrappedResponse(TopLevelDataInterface $data, Request $request, IncludedData $includes$response_code = 200, array $headers = [], LinkCollection $links = NULL, array $meta = []) {
    $links = ($links ?: new LinkCollection([]));
    if (!$links->hasLinkWithKey('self')) {
      $self_link = new Link(new CacheableMetadata(), self::getRequestLink($request), 'self');
      $links = $links->withLink('self', $self_link);
    }
    $document = new JsonApiDocumentTopLevel($data$includes$links$meta);
    if (!$request->isMethodCacheable()) {
      return new ResourceResponse($document$response_code$headers);
    }
    $response = new CacheableResourceResponse($document$response_code$headers);
    $cacheability = (new CacheableMetadata())->addCacheContexts([
      // Make sure that different sparse fieldsets are cached differently.       'url.query_args:fields',
      

  protected static function buildLinksFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links) {
    if ($resource_type->isLocatable() && !$resource_type->isInternal()) {
      $self_url = Url::fromRoute(Routes::getRouteName($resource_type, 'individual')['entity' => $entity->uuid()]);
      if ($resource_type->isVersionable()) {
        assert($entity instanceof RevisionableInterface);
        if (!$links->hasLinkWithKey('self')) {
          // If the resource is versionable, the `self` link should be the exact           // link for the represented version. This helps a client track           // revision changes and to disambiguate resource objects with the same           // `type` and `id` in a `version-history` collection.           $self_with_version_url = $self_url->setOption('query', [JsonApiSpec::VERSION_QUERY_PARAMETER => 'id:' . $entity->getRevisionId()]);
          $links = $links->withLink('self', new Link(new CacheableMetadata()$self_with_version_url, 'self'));
        }
        if (!$entity->isDefaultRevision()) {
          $latest_version_url = $self_url->setOption('query', [JsonApiSpec::VERSION_QUERY_PARAMETER => 'rel:' . VersionByRel::LATEST_VERSION]);
          $links = $links->withLink(VersionByRel::LATEST_VERSION, new Link(new CacheableMetadata()$latest_version_url, VersionByRel::LATEST_VERSION));
        }
        
Home | Imprint | This part of the site doesn't use cookies.