CacheableNormalization example

$normalization = array_merge(['href' => $link->getHref()], !empty($attributes) ? ['meta' => $attributes] : []);
        // Checking access on links is not about access to the link itself;         // it is about whether the current user has access to the route that is         // *targeted* by the link. This is done on a "best effort" basis. That         // is, some links target routes that depend on a request to determine if         // they're accessible or not. Some other links might target routes to         // which the current user will clearly not have access, in that case         // this code proactively removes those links from the response.         $access = $link->getUri()->access($this->currentUser, TRUE);
        $cacheability = CacheableMetadata::createFromObject($link)->addCacheableDependency($access);
        $normalized[$link_key] = $access->isAllowed()
          ? new CacheableNormalization($cacheability$normalization)
          : new CacheableOmission($cacheability);
      }
    }
    return CacheableNormalization::aggregate($normalized);
  }

  /** * Hashes a link using its href and its target attributes, if any. * * This method generates an unpredictable, but deterministic, 7 character * alphanumeric hash for a given link. * * The hash is unpredictable because a random hash salt will be used for every * request. The hash is deterministic because, within a single request, links * with the same href and target attributes (i.o.w. duplicates) will generate * equivalent hash values. * * @param \Drupal\jsonapi\JsonApiResource\Link $link * A link to be hashed. * * @return string * A 7 character alphanumeric hash. */

    else {
      // Add data.       $document['data'] = $this->serializer->normalize($data$format$context);
      // Add includes.       $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. */
      $field_properties = TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item);
      foreach ($field_properties as $property_name => $property) {
        $values[$property_name] = $this->serializer->normalize($property$format$context);
      }
      // Flatten if there is only a single property to normalize.       $flatten = count($field_properties) === 1 && $field_item::mainPropertyName() !== NULL;
      $values = static::rasterizeValueRecursive($flatten ? reset($values) : $values);
    }
    else {
      $values = $field_item->getValue();
    }
    $normalization = new CacheableNormalization(
      $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY],
      $values
    );
    unset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
    return $normalization;
  }

  /** * {@inheritdoc} */
  public function denormalize($data$class$format = NULL, array $context = []): mixed {
    
 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. */
Home | Imprint | This part of the site doesn't use cookies.