You are here

protected function ContentEntityNormalizer::normalizeTranslation in Default Content for D8 2.0.x

Normalizes an entity (translation).

Parameters

\Drupal\Core\Entity\ContentEntityInterface $translation: The entity to be normalized with its currently active language.

string[] $field_names: List of fields to normalize.

Return value

array The normalized field values.

1 call to ContentEntityNormalizer::normalizeTranslation()
ContentEntityNormalizer::normalize in src/Normalizer/ContentEntityNormalizer.php
Normalizes the entity into an array structure.

File

src/Normalizer/ContentEntityNormalizer.php, line 333

Class

ContentEntityNormalizer
Normalizes and denormalizes content entities.

Namespace

Drupal\default_content\Normalizer

Code

protected function normalizeTranslation(ContentEntityInterface $translation, array $field_names) {
  $translation_normalization = [];
  foreach ($field_names as $field_name) {
    if ($translation
      ->getFieldDefinition($field_name)
      ->getType() == 'changed') {

      // Ignore the changed field.
      continue;
    }
    if ($translation
      ->isDefaultTranslation() || $translation
      ->getFieldDefinition($field_name)
      ->isTranslatable()) {
      foreach ($translation
        ->get($field_name) as $delta => $field_item) {

        // Ignore empty field items.
        if ($field_item
          ->isEmpty()) {
          continue;
        }
        $serialized_property_names = $this
          ->getCustomSerializedPropertyNames($field_item);

        /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
        foreach ($field_item
          ->getProperties(TRUE) as $property_name => $property) {
          $value = $this
            ->getValueFromProperty($property, $field_item, $translation_normalization[$field_name][$delta]);
          if ($value !== NULL) {
            if (is_string($value) && in_array($property_name, $serialized_property_names)) {
              $value = unserialize($value);
            }
            $translation_normalization[$field_name][$delta][$property_name] = $value;
          }
        }
      }
    }
  }
  return $translation_normalization;
}