You are here

protected static function ResourceTestBase::decorateResourceIdentifierWithDrupalInternalTargetId in Drupal 9

Adds drupal_internal__target_id to the meta of a resource identifier.

Parameters

\Drupal\Core\Field\FieldItemInterface|\Drupal\Core\Field\FieldItemListInterface $field: The field containing the entity that is described by the resource_identifier.

array $resource_identifier: A resource identifier for an entity.

Return value

array A resource identifier for an entity with drupal_internal__target_id set if appropriate.

1 call to ResourceTestBase::decorateResourceIdentifierWithDrupalInternalTargetId()
ResourceTestBase::getExpectedGetRelationshipDocumentData in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Gets the expected document data for the given relationship.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function decorateResourceIdentifierWithDrupalInternalTargetId($field, array $resource_identifier) : array {
  $property_definitions = $field
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getPropertyDefinitions();
  if (!isset($property_definitions['target_id'])) {
    return $resource_identifier;
  }
  $is_data_reference_definition = $property_definitions['target_id'] instanceof DataReferenceTargetDefinition;
  if ($is_data_reference_definition) {

    // Numeric target IDs usually reference content entities, which use an
    // auto-incrementing integer ID. Non-numeric target IDs usually reference
    // config entities, which use a machine-name as an ID.
    $resource_identifier['meta']['drupal_internal__target_id'] = is_numeric($field->target_id) ? (int) $field->target_id : $field->target_id;
  }
  return $resource_identifier;
}