public function DefaultFieldProcessor::setTranslations

Process the translated data for this field back into a structure that can be saved by the content entity.

Parameters

array $field_data: The translated data for this field.

\Drupal\Core\Field\FieldItemListInterface $field: The field object.

Overrides FieldProcessorInterface::setTranslations

See also

\Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource::doSaveTranslations()

1 method overrides DefaultFieldProcessor::setTranslations()
MetatagsFieldProcessor::setTranslations in sources/content/src/MetatagsFieldProcessor.php
Process the translated data for this field back into a structure that can be saved by the content entity.

File

sources/content/src/DefaultFieldProcessor.php, line 100

Class

DefaultFieldProcessor
Default field processor.

Namespace

Drupal\tmgmt_content

Code

public function setTranslations($field_data, FieldItemListInterface $field) {
  foreach (Element::children($field_data) as $delta) {
    $field_item = $field_data[$delta];
    foreach (Element::children($field_item) as $property) {
      $property_data = $field_item[$property];

      // If there is translation data for the field property, save it.
      if ((isset($property_data['#translation']['#text']) || isset($property_data['#translation']['#file'])) && $property_data['#translate']) {
        $translation_key = isset($property_data['#translation']['#text']) ? '#text' : '#file';

        // If the offset does not exist, populate it with the current value
        // from the source content, so that the translated field offset can be
        // saved.
        if (!$field
          ->offsetExists($delta)) {
          $translation = $field
            ->getEntity();
          $source = $translation
            ->getUntranslated();
          $source_field = $source
            ->get($field
            ->getName());
          $source_offset = $source_field
            ->offsetGet($delta);

          // Note that the source language value will be immediately
          // overwritten.
          $field
            ->offsetSet($delta, $source_offset);
        }
        $field
          ->offsetGet($delta)
          ->set($property, $property_data['#translation'][$translation_key]);
      }
    }
  }
}