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. 8.9.x 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 1765

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;
        if (is_null($target_entity)) {
            return NULL;
        }
        $resource_identifier = static::toResourceIdentifier($target_entity);
        $resource_identifier = static::decorateResourceIdentifierWithDrupalInternalTargetId($field, $resource_identifier);
        return $resource_identifier;
    }
    else {
        $arity_counter = [];
        $relation_list = array_filter(array_map(function ($item) use (&$arity_counter) {
            $target_entity = $item->entity;
            if (is_null($target_entity)) {
                return NULL;
            }
            $resource_identifier = static::toResourceIdentifier($target_entity);
            $resource_identifier = static::decorateResourceIdentifierWithDrupalInternalTargetId($item, $resource_identifier);
            $type = $resource_identifier['type'];
            $id = $resource_identifier['id'];
            // Start the count of identifiers sharing a single type and ID at 1.
            if (!isset($arity_counter[$type][$id])) {
                $arity_counter[$type][$id] = 1;
            }
            else {
                $arity_counter[$type][$id] += 1;
            }
            return $resource_identifier;
        }, iterator_to_array($field)));
        $arity_map = [];
        $relation_list = array_map(function ($identifier) use ($arity_counter, &$arity_map) {
            $type = $identifier['type'];
            $id = $identifier['id'];
            // Only add an arity value if there are two or more resource identifiers
            // with the same type and ID.
            if (($arity_counter[$type][$id] ?? 0) > 1) {
                // Arity is indexed from 0. If the array key isn't set, 1 + (-1) = 0.
                if (!isset($arity_map[$type][$id])) {
                    $arity_map[$type][$id] = 0;
                }
                else {
                    $arity_map[$type][$id] += 1;
                }
                $identifier['meta']['arity'] = $arity_map[$type][$id];
            }
            return $identifier;
        }, $relation_list);
        return $relation_list;
    }
}

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