You are here

public static function Relationship::createFromEntityReferenceField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Relationship.php \Drupal\jsonapi\JsonApiResource\Relationship::createFromEntityReferenceField()
  2. 9 core/modules/jsonapi/src/JsonApiResource/Relationship.php \Drupal\jsonapi\JsonApiResource\Relationship::createFromEntityReferenceField()

Creates a new Relationship from an entity reference field.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObject $context: The context resource object of the relationship to be created.

\Drupal\Core\Field\EntityReferenceFieldItemListInterface $field: The entity reference field from which to create the relationship.

\Drupal\jsonapi\JsonApiResource\LinkCollection $links: (optional) Any extra links for the Relationship, if a `self` link is not provided, one will be automatically added if the context resource is locatable and is not internal.

array $meta: (optional) Any relationship metadata.

Return value

static An instantiated relationship object.

1 call to Relationship::createFromEntityReferenceField()
EntityResource::getRelationship in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the relationship of an entity.

File

core/modules/jsonapi/src/JsonApiResource/Relationship.php, line 118

Class

Relationship
Represents references from one resource object to other resource object(s).

Namespace

Drupal\jsonapi\JsonApiResource

Code

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);
}