CacheableOmission example


  protected function serializeField($field, array $context$format) {
    // Only content entities contain FieldItemListInterface fields. Since config     // entities do not have "real" fields and therefore do not have field access     // restrictions.     if ($field instanceof FieldItemListInterface) {
      $field_access_result = $field->access('view', $context['account'], TRUE);
      if (!$field_access_result->isAllowed()) {
        return new CacheableOmission(CacheableMetadata::createFromObject($field_access_result));
      }
      if ($field instanceof EntityReferenceFieldItemListInterface) {
        // Build the relationship object based on the entity reference and         // normalize that object instead.         assert(!empty($context['resource_object']) && $context['resource_object'] instanceof ResourceObject);
        $resource_object = $context['resource_object'];
        $relationship = Relationship::createFromEntityReferenceField($resource_object$field);
        $normalized_field = $this->serializer->normalize($relationship$format$context);
      }
      else {
        $normalized_field = $this->serializer->normalize($field$format$context);
      }
return $this->normalization;
  }

  /** * Converts the object to a CacheableOmission if the normalization is empty. * * @return self|\Drupal\jsonapi\Normalizer\Value\CacheableOmission * A CacheableOmission if the normalization is considered empty, self * otherwise. */
  public function omitIfEmpty() {
    return empty($this->normalization) ? new CacheableOmission($this) : $this;
  }

  /** * Gets a new CacheableNormalization with an additional dependency. * * @param \Drupal\Core\Cache\CacheableDependencyInterface $dependency * The new cacheable dependency. * * @return static * A new object based on the current value with an additional cacheable * dependency. */
// 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. */

  protected function normalizeOmissionsLinks(OmittedData $omissions$format, array $context = []) {
    $normalized_omissions = array_map(function DHttpExceptionInterface $exception) use ($format$context) {
      return $this->serializer->normalize($exception$format$context);
    }$omissions->toArray());
    $cacheability = CacheableMetadata::createFromObject(CacheableNormalization::aggregate($normalized_omissions));
    if (empty($normalized_omissions)) {
      return new CacheableOmission($cacheability);
    }
    $omission_links = [
      'detail' => 'Some resources have been omitted because of insufficient authorization.',
      'links' => [
        'help' => [
          'href' => 'https://www.drupal.org/docs/8/modules/json-api/filtering#filters-access-control',
        ],
      ],
    ];
    $link_hash_salt = Crypt::randomBytesBase64();
    foreach ($normalized_omissions as $omission) {
      
Home | Imprint | This part of the site doesn't use cookies.