You are here

public function FieldNormalizer::normalize in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::normalize()

Normalizes an object into a set of arrays/scalars.

Parameters

object $object object to normalize:

string $format format the normalization result will be encoded as:

array $context Context options for the normalizer:

Return value

array|string|bool|int|float|null

Overrides NormalizerInterface::normalize

File

core/modules/hal/src/Normalizer/FieldNormalizer.php, line 28
Contains \Drupal\hal\Normalizer\FieldNormalizer.

Class

FieldNormalizer
Converts the Drupal field structure to HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

public function normalize($field, $format = NULL, array $context = array()) {
  $normalized_field_items = array();

  // Get the field definition.
  $entity = $field
    ->getEntity();
  $field_name = $field
    ->getName();
  $field_definition = $field
    ->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, $format, $context);
  }
  else {
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $context['langcode'] = $language
        ->getId();
      $translation = $entity
        ->getTranslation($language
        ->getId());
      $translated_field = $translation
        ->get($field_name);
      $normalized_field_items = array_merge($normalized_field_items, $this
        ->normalizeFieldItems($translated_field, $format, $context));
    }
  }

  // Merge deep so that links set in entity reference normalizers are merged
  // into the links property.
  $normalized = NestedArray::mergeDeepArray($normalized_field_items);
  return $normalized;
}