public function ParagraphsConversionManager::addTranslation

A helper that adds given translation values and sets the source language.

Parameters

\Drupal\Core\TypedData\TranslatableInterface $entity: The translatable entity.

string $lang_code: The language code.

array $values: An array of translation values.

File

paragraphs/src/ParagraphsConversionManager.php, line 186

Class

ParagraphsConversionManager
Plugin type manager for paragraphs type conversion plugins.

Namespace

Drupal\paragraphs

Code

public function addTranslation(TranslatableInterface $entity, $lang_code, array $values) {

  // Add a new translation to the translatable object.
  $entity
    ->addTranslation($lang_code, $values);

  // Set the source language for this translation.
  $translation = $entity
    ->getTranslation($lang_code);
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $content_translation_manager
    ->getTranslationMetadata($translation)
    ->setSource($entity
    ->language()
    ->getId());
}