Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Relationship.php

function 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.

2 calls to Relationship::createFromEntityReferenceField()
EntityResource::getRelationship in core/modules/jsonapi/src/Controller/EntityResource.php
Gets the relationship of an entity.
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);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal