function Relationship::createFromEntityReferenceField

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/JsonApiResource/Relationship.php \Drupal\jsonapi\JsonApiResource\Relationship::createFromEntityReferenceField()
  2. 10 core/modules/jsonapi/src/JsonApiResource/Relationship.php \Drupal\jsonapi\JsonApiResource\Relationship::createFromEntityReferenceField()
  3. 11.x 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.

3 calls to Relationship::createFromEntityReferenceField()
EntityResource::getRelationship in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the relationship of an entity.
RelationshipNormalizerTest::testNormalize in core/modules/jsonapi/tests/src/Kernel/Normalizer/RelationshipNormalizerTest.php
@covers ::normalize @dataProvider normalizeProvider
ResourceObjectNormalizer::serializeField in core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php
Serializes a given field.

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.