You are here

protected function FieldItemNormalizer::getFieldItemInstance in Drupal 8

Same name and namespace in other branches
  1. 9 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
Denormalizes data back into an object of the given class.

File

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

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;
}