withCacheableDependency example

      $document['included'] = $this->serializer->normalize($object->getIncludes()$format$context)->omitIfEmpty();
      // Add omissions and metadata.       $normalized_omissions = $this->normalizeOmissionsLinks($object->getOmissions()$format$context);
      $meta = !$normalized_omissions instanceof CacheableOmission
        ? array_merge($object->getMeta()['omitted' => $normalized_omissions->getNormalization()])
        : $object->getMeta();
      $document['meta'] = (new CacheableNormalization($normalized_omissions$meta))->omitIfEmpty();
    }
    // Add document links.     $document['links'] = $this->serializer->normalize($object->getLinks()$format$context)->omitIfEmpty();
    // Every JSON:API document contains absolute URLs.     return CacheableNormalization::aggregate($document)->withCacheableDependency((new CacheableMetadata())->addCacheContexts(['url.site']));
  }

  /** * Normalizes an error collection. * * @param \Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel $document * The document to normalize. * @param string $format * The normalization format. * @param array $context * The normalization context. * * @return \Drupal\jsonapi\Normalizer\Value\CacheableNormalization * The normalized document. * * @todo: refactor this to use CacheableNormalization::aggregate in https://www.drupal.org/project/drupal/issues/3036284. */
 static::getRelationshipLinks($context['resource_object']$resource_relationship));
    $data_normalization = $normalized_items->getNormalization();
    $normalization = [
      // Empty 'to-one' relationships must be NULL.       // Empty 'to-many' relationships must be an empty array.       // @link http://jsonapi.org/format/#document-resource-object-linkage       'data' => $resource_relationship->hasOne() ? array_shift($data_normalization) : $data_normalization,
    ];
    if (!empty($links)) {
      $normalization['links'] = $links;
    }
    return (new CacheableNormalization($normalized_items$normalization))->withCacheableDependency($link_cacheability);
  }

  /** * Gets the links for the relationship. * * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $relationship_context * The JSON:API resource object context of the relationship. * @param \Drupal\jsonapi\ResourceType\ResourceTypeRelationship $resource_relationship * The resource type relationship field. * * @return array * The relationship's links. */
$field_normalizations = array_intersect_key($fieldsarray_flip($field_names));

    $relationship_field_names = array_keys($resource_type->getRelatableResourceTypes());
    $attributes = array_diff_key($field_normalizationsarray_flip($relationship_field_names));
    $relationships = array_intersect_key($field_normalizationsarray_flip($relationship_field_names));
    $entity_normalization = array_filter(
      $normalization_parts[ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_BASE] + [
        'attributes' => CacheableNormalization::aggregate($attributes)->omitIfEmpty(),
        'relationships' => CacheableNormalization::aggregate($relationships)->omitIfEmpty(),
      ]
    );
    return CacheableNormalization::aggregate($entity_normalization)->withCacheableDependency($object);
  }

  /** * Normalizes an entity using the given fieldset. * * @param string[] $field_names * The field names to normalize (the sparse fieldset, if any). * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $object * The resource object to partially normalize. * @param string $format * The format in which the normalization will be encoded. * @param array $context * Context options for the normalizer. * * @return array * An array with two key-value pairs: * - 'base': array, the base normalization of the entity, that does not * depend on which sparse fieldset was requested. * - 'fields': CacheableNormalization for all requested fields. * * @see ::normalize() */
public function testLinkNormalizationCacheability() {
    $user = User::create([
      'name' => $this->randomMachineName(),
      'pass' => $this->randomString(),
    ]);
    $user->save();
    $resource_type = $this->resourceTypeRepository->get($user->getEntityTypeId()$user->bundle());
    $resource_object = ResourceObject::createFromEntity($resource_type$user);
    $cache_tag_to_invalidate = 'link_normalization';
    $normalized_links = $this->serializer
      ->normalize($resource_object->getLinks(), 'api_json')
      ->withCacheableDependency((new CacheableMetadata())->addCacheTags([$cache_tag_to_invalidate]));
    assert($normalized_links instanceof CacheableNormalization);
    $normalization_parts = [
      ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_BASE => [
        'type' => CacheableNormalization::permanent($resource_object->getTypeName()),
        'id' => CacheableNormalization::permanent($resource_object->getId()),
        'links' => $normalized_links,
      ],
      ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_FIELDS => [],
    ];
    $this->cacher->saveOnTerminate($resource_object$normalization_parts);

    
Home | Imprint | This part of the site doesn't use cookies.