You are here

protected function EntityNormalizer::getFieldItemInstance in JSON:API 8

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.

string $field_name: The name of the field to get a field item instance for.

Return value

\Drupal\Core\Field\FieldItemInterface The requested field item instance.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to EntityNormalizer::getFieldItemInstance()
EntityNormalizer::prepareInput in src/Normalizer/EntityNormalizer.php
Prepares the input data to create the entity.

File

src/Normalizer/EntityNormalizer.php, line 306

Class

EntityNormalizer
Converts the Drupal entity object to a JSON API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getFieldItemInstance(ResourceType $resource_type, $field_name) {
  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($field_name);
  assert($field instanceof FieldItemListInterface);
  $field_item = $field
    ->appendItem();
  assert($field_item instanceof FieldItemInterface);
  return $field_item;
}