You are here

public function FieldItemNormalizer::normalize in Commerce Cart API 8

Overrides ComplexDataNormalizer::normalize

File

src/Normalizer/FieldItemNormalizer.php, line 51

Class

FieldItemNormalizer
Field item normalizer which flattens output.

Namespace

Drupal\commerce_cart_api\Normalizer

Code

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

  // This will always be true, but here for type hinting for IDE.
  if (!$field_item instanceof FieldItemInterface) {
    return $data;
  }
  $main_property = $field_item::mainPropertyName();
  if (count($data) == 1) {
    return reset($data);
  }
  elseif (isset($data[$main_property])) {
    return $data[$main_property];
  }
  return $data;
}