function FieldNormalizer::normalize

Same name in this branch
  1. 9 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
  2. 8.9.x core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::normalize()
  3. 10 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
  4. 11.x core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()

Overrides ListNormalizer::normalize

File

core/modules/hal/src/Normalizer/FieldNormalizer.php, line 21

Class

FieldNormalizer
Converts the Drupal field structure to HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

public function normalize($field_items, $format = NULL, array $context = []) {
    $normalized_field_items = [];
    // Get the field definition.
    $entity = $field_items->getEntity();
    $field_name = $field_items->getName();
    $field_definition = $field_items->getFieldDefinition();
    // If this field is not translatable, it can simply be normalized without
    // separating it into different translations.
    if (!$field_definition->isTranslatable()) {
        $normalized_field_items = $this->normalizeFieldItems($field_items, $format, $context);
    }
    else {
        foreach ($entity->getTranslationLanguages() as $language) {
            $context['langcode'] = $language->getId();
            $translation = $entity->getTranslation($language->getId());
            $translated_field_items = $translation->get($field_name);
            $normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field_items, $format, $context));
        }
    }
    // Merge deep so that links set in entity reference normalizers are merged
    // into the links property.
    return NestedArray::mergeDeepArray($normalized_field_items);
}

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