getResourceType example

/** * Returns a copy of the given ResourceIdentifier with the given arity. * * @param int $arity * The new arity; must be a non-negative integer. * * @return static * A newly created ResourceIdentifier with the given arity, otherwise * the same. */
  public function withArity($arity) {
    return new static($this->getResourceType()$this->getId()[static::ARITY_KEY => $arity] + $this->getMeta());
  }

  /** * Gets the resource identifier objects metadata. * * @return array * The metadata. */
  public function getMeta() {
    return $this->meta;
  }

  

class FieldNormalizer extends NormalizerBase implements DenormalizerInterface {

  /** * {@inheritdoc} */
  public function normalize($field$format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    /** @var \Drupal\Core\Field\FieldItemListInterface $field */
    $normalized_items = $this->normalizeFieldItems($field$format$context);
    assert($context['resource_object'] instanceof ResourceObject);
    return $context['resource_object']->getResourceType()->getFieldByInternalName($field->getName())->hasOne()
      ? array_shift($normalized_items) ?: CacheableNormalization::permanent(NULL)
      : CacheableNormalization::aggregate($normalized_items);
  }

  /** * {@inheritdoc} */
  public function denormalize($data$class$format = NULL, array $context = []): mixed {
    $field_definition = $context['field_definition'];
    assert($field_definition instanceof FieldDefinitionInterface);
    $resource_type = $context['resource_type'];
    

  public function getTypeName() {
    return $this->resourceIdentifier->getTypeName();
  }

  /** * {@inheritdoc} */
  public function getResourceType() {
    if (!isset($this->resourceType)) {
      $this->resourceType = $this->resourceIdentifier->getResourceType();
    }
    return $this->resourceType;
  }

}

  public static function createFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links = NULL, array $meta = []) {
    $context_resource_type = $context->getResourceType();
    $resource_field = $context_resource_type->getFieldByInternalName($field->getName());
    return new static(
      $resource_field->getPublicName(),
      new RelationshipData(ResourceIdentifier::toResourceIdentifiers($field)$resource_field->hasOne() ? 1 : -1),
      static::buildLinkCollectionFromEntityReferenceField($context$field$links ?: new LinkCollection([])),
      $meta,
      $context
    );
  }

  /** * Gets context resource object of the relationship. * * @return \Drupal\jsonapi\JsonApiResource\ResourceObject * The context ResourceObject. * * @see \Drupal\jsonapi\JsonApiResource\Relationship::$context */

  public function saveOnTerminate(ResourceObject $object, array $normalization_parts) {
    assert(
      array_keys($normalization_parts) === [
        static::RESOURCE_CACHE_SUBSET_BASE,
        static::RESOURCE_CACHE_SUBSET_FIELDS,
      ]
    );
    $resource_type = $object->getResourceType();
    $key = $resource_type->getTypeName() . ':' . $object->getId();
    $this->toCache[$key] = [$object$normalization_parts];
  }

  /** * Writes normalizations of entities to cache, if any were created. * * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event * The Event to process. */
  public function onTerminate(TerminateEvent $event) {
    
public function supportsDenormalization($data, string $type, string $format = NULL, array $context = []): bool {
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function normalize($object$format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    assert($object instanceof ResourceObject);
    // If the fields to use were specified, only output those field values.     $context['resource_object'] = $object;
    $resource_type = $object->getResourceType();
    $resource_type_name = $resource_type->getTypeName();
    $fields = $object->getFields();
    // Get the bundle ID of the requested resource. This is used to determine if     // this is a bundle level resource or an entity level resource.     if (!empty($context['sparse_fieldset'][$resource_type_name])) {
      $field_names = $context['sparse_fieldset'][$resource_type_name];
    }
    else {
      $field_names = array_keys($fields);
    }

    

  protected function resolveIncludeTree(array $include_tree, Data $data, Data $includes = NULL) {
    $includes = is_null($includes) ? new IncludedData([]) : $includes;
    foreach ($include_tree as $field_name => $children) {
      $references = [];
      foreach ($data as $resource_object) {
        // Some objects in the collection may be LabelOnlyResourceObjects or         // EntityAccessDeniedHttpException objects.         assert($resource_object instanceof ResourceIdentifierInterface);
        $public_field_name = $resource_object->getResourceType()->getPublicName($field_name);

        if ($resource_object instanceof LabelOnlyResourceObject) {
          $message = "The current user is not allowed to view this relationship.";
          $exception = new EntityAccessDeniedHttpException($resource_object->getEntity(), AccessResult::forbidden("The user only has authorization for the 'view label' operation."), '', $message$public_field_name);
          $includes = IncludedData::merge($includesnew IncludedData([$exception]));
          continue;
        }
        elseif (!$resource_object instanceof ResourceObject) {
          continue;
        }

        

class EntityReferenceFieldNormalizer extends FieldNormalizer {

  /** * {@inheritdoc} */
  public function normalize($field$format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    assert($field instanceof EntityReferenceFieldItemListInterface);
    // Build the relationship object based on the Entity Reference and normalize     // that object instead.     $resource_identifiers = array_filter(ResourceIdentifier::toResourceIdentifiers($field->filterEmptyItems())function DResourceIdentifierInterface $resource_identifier) {
      return !$resource_identifier->getResourceType()->isInternal();
    });
    $normalized_items = CacheableNormalization::aggregate($this->serializer->normalize($resource_identifiers$format$context));
    assert($context['resource_object'] instanceof ResourceObject);
    $resource_relationship = $context['resource_object']->getResourceType()->getFieldByInternalName($field->getName());
    assert($resource_relationship instanceof ResourceTypeRelationship);
    $link_cacheability = new CacheableMetadata();
    $links = array_map(function DUrl $link) use ($link_cacheability) {
      $href = $link->setAbsolute()->toString(TRUE);
      $link_cacheability->addCacheableDependency($href);
      return ['href' => $href->getGeneratedUrl()];
    }static::getRelationshipLinks($context['resource_object']$resource_relationship));
    
Home | Imprint | This part of the site doesn't use cookies.