You are here

protected function ContentEntityConflictHandler::autoMergeNonEditedTranslations in Conflict 8.2

Merges translatable fields of non-edited translations.

Additionally deleted translations will be removed and new translations will be added.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity_local_edited: The locally edited entity.

\Drupal\Core\Entity\ContentEntityInterface $entity_server: The unchanged entity loaded from the storage.

1 call to ContentEntityConflictHandler::autoMergeNonEditedTranslations()
ContentEntityConflictHandler::entityFormEntityBuilder in src/Entity/ContentEntityConflictHandler.php
Entity builder method.

File

src/Entity/ContentEntityConflictHandler.php, line 398

Class

ContentEntityConflictHandler

Namespace

Drupal\conflict\Entity

Code

protected function autoMergeNonEditedTranslations(ContentEntityInterface $entity_local_edited, ContentEntityInterface $entity_server) {
  $edited_langcode = $entity_local_edited
    ->language()
    ->getId();
  $entity_server_langcodes = array_keys($entity_server
    ->getTranslationLanguages());
  $entity_local_edited_langcodes = array_keys($entity_local_edited
    ->getTranslationLanguages());
  foreach ($entity_server_langcodes as $langcode) {
    if ($langcode == $edited_langcode) {
      continue;
    }

    // @todo we should set that the translation is not new.
    $entity_server_translation = $entity_server
      ->getTranslation($langcode);
    $entity_local_edited_translation = $entity_local_edited
      ->hasTranslation($langcode) ? $entity_local_edited
      ->getTranslation($langcode) : $entity_local_edited
      ->addTranslation($langcode);

    // @todo If the entity implements the EntityChangedInterface then first
    // check the changed timestamps as a shortcut to skip updating fields of
    // translations that haven't changed, but for that we have to ensure
    if ($entity_server_translation
      ->getChangedTime() != $entity_local_edited_translation
      ->getChangedTime()) {
      foreach ($entity_server_translation
        ->getTranslatableFields() as $field_name => $field_item_list) {
        $entity_local_edited_translation
          ->set($field_name, $field_item_list
          ->getValue());
      }
    }
  }
  foreach (array_diff($entity_local_edited_langcodes, $entity_server_langcodes) as $langcode) {
    $entity_local_edited
      ->removeTranslation($langcode);
  }
}