You are here

public function FieldNormalizer::normalize in Commerce Cart API 8

Overrides ListNormalizer::normalize

File

src/Normalizer/FieldNormalizer.php, line 46

Class

FieldNormalizer
Field normalizer which flattens output.

Namespace

Drupal\commerce_cart_api\Normalizer

Code

public function normalize($field_item, $format = NULL, array $context = []) {

  /** @var \Drupal\Core\Field\FieldItemListInterface $field_item */
  $cardinality = $field_item
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getCardinality();
  $data = parent::normalize($field_item, $format, $context);
  if ($cardinality > 1 || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
    return $data;
  }
  if (empty($data)) {
    return NULL;
  }
  return reset($data);
}