toResourceIdentifiers example


  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 */

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()];
    },

  public static function toResourceIdentifiersWithArityRequired(EntityReferenceFieldItemListInterface $items) {
    return array_map(function DResourceIdentifier $identifier) {
      return $identifier->hasArity() ? $identifier : $identifier->withArity(0);
    }static::toResourceIdentifiers($items));
  }

  /** * Creates a ResourceIdentifier object. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity from which to create the resource identifier. * * @return self * A new ResourceIdentifier object. */
  
Home | Imprint | This part of the site doesn't use cookies.