function ResourceTestBase::getExpectedGetRelationshipDocumentData

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipDocumentData()
  2. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipDocumentData()
  3. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipDocumentData()

Gets the expected document data for the given relationship.

Parameters

string $relationship_field_name: The relationship for which to get an expected response.

\Drupal\Core\Entity\EntityInterface|null $entity: (optional) The entity for which to get expected relationship data.

Return value

mixed The expected document data.

3 calls to ResourceTestBase::getExpectedGetRelationshipDocumentData()
MediaTest::getExpectedGetRelationshipDocumentData in core/modules/jsonapi/tests/src/Functional/MediaTest.php
Gets the expected document data for the given relationship.
ResourceTestBase::getExpectedGetRelationshipDocument in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Gets an expected document for the given relationship.
TermTest::getExpectedGetRelationshipDocumentData in core/modules/jsonapi/tests/src/Functional/TermTest.php
Gets the expected document data for the given relationship.
2 methods override ResourceTestBase::getExpectedGetRelationshipDocumentData()
MediaTest::getExpectedGetRelationshipDocumentData in core/modules/jsonapi/tests/src/Functional/MediaTest.php
Gets the expected document data for the given relationship.
TermTest::getExpectedGetRelationshipDocumentData in core/modules/jsonapi/tests/src/Functional/TermTest.php
Gets the expected document data for the given relationship.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 1747

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedGetRelationshipDocumentData($relationship_field_name, EntityInterface $entity = NULL) {
    $entity = $entity ?: $this->entity;
    $internal_field_name = $this->resourceType
        ->getInternalName($relationship_field_name);
    
    /* @var \Drupal\Core\Field\FieldItemListInterface $field */
    $field = $entity->{$internal_field_name};
    $is_multiple = $field->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getCardinality() !== 1;
    if ($field->isEmpty()) {
        return $is_multiple ? [] : NULL;
    }
    if (!$is_multiple) {
        $target_entity = $field->entity;
        return is_null($target_entity) ? NULL : static::toResourceIdentifier($target_entity);
    }
    else {
        return array_filter(array_map(function ($item) {
            $target_entity = $item->entity;
            return is_null($target_entity) ? NULL : static::toResourceIdentifier($target_entity);
        }, iterator_to_array($field)));
    }
}

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