function FieldItemNormalizer::getFieldItemInstance

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::getFieldItemInstance()
  2. 10 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::getFieldItemInstance()
  3. 11.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::getFieldItemInstance()

Gets a field item instance for use with SerializedColumnNormalizerTrait.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type of the entity being denormalized.

\Drupal\Core\Field\TypedData\FieldItemDataDefinitionInterface $item_definition: The field item definition of the instance to get.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to FieldItemNormalizer::getFieldItemInstance()
FieldItemNormalizer::denormalize in core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php

File

core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php, line 227

Class

FieldItemNormalizer
Converts the Drupal field item object to a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getFieldItemInstance(ResourceType $resource_type, FieldItemDataDefinitionInterface $item_definition) {
    if ($bundle_key = $this->entityTypeManager
        ->getDefinition($resource_type->getEntityTypeId())
        ->getKey('bundle')) {
        $create_values = [
            $bundle_key => $resource_type->getBundle(),
        ];
    }
    else {
        $create_values = [];
    }
    $entity = $this->entityTypeManager
        ->getStorage($resource_type->getEntityTypeId())
        ->create($create_values);
    $field = $entity->get($item_definition->getFieldDefinition()
        ->getName());
    assert($field instanceof FieldItemListInterface);
    $field_item = $field->appendItem();
    assert($field_item instanceof FieldItemInterface);
    return $field_item;
}

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