You are here

protected function LayoutParagraphsBuilder::initTranslations in Layout Paragraphs 2.0.x

Initialize translations for item list.

Makes sure all components have a translation for the current language and creates them if necessary.

1 call to LayoutParagraphsBuilder::initTranslations()
LayoutParagraphsBuilder::preRender in src/Element/LayoutParagraphsBuilder.php
Pre-render callback: Renders the UI.

File

src/Element/LayoutParagraphsBuilder.php, line 612

Class

LayoutParagraphsBuilder
Defines a render element for building the Layout Builder UI.

Namespace

Drupal\layout_paragraphs\Element

Code

protected function initTranslations() {
  $items = $this->layoutParagraphsLayout
    ->getParagraphsReferenceField();

  /** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem $item */
  foreach ($items as $delta => $item) {
    if (!empty($item->entity) && $item->entity instanceof ParagraphInterface) {
      if (!$this
        ->isTranslating()) {

        // Set the langcode if we are not translating.
        $langcode_key = $item->entity
          ->getEntityType()
          ->getKey('langcode');
        if ($item->entity
          ->get($langcode_key)->value != $this->langcode) {

          // If a translation in the given language already exists,
          // switch to that. If there is none yet, update the language.
          if ($item->entity
            ->hasTranslation($this->langcode)) {
            $item->entity = $item->entity
              ->getTranslation($this->langcode);
          }
          else {
            $item->entity
              ->set($langcode_key, $this->langcode);
          }
        }
      }
      else {

        // Add translation if missing for the target language.
        if (!$item->entity
          ->hasTranslation($this->langcode)) {

          // Get the selected translation of the paragraph entity.
          $entity_langcode = $item->entity
            ->language()
            ->getId();
          $source_langcode = $this->sourceLangcode ?? $entity_langcode;

          // Make sure the source language version is used if available.
          // Fetching the translation without this check could lead valid
          // scenario to have no paragraphs items in the source version of
          // to an exception.
          if ($item->entity
            ->hasTranslation($source_langcode)) {
            $entity = $item->entity
              ->getTranslation($source_langcode);
          }

          // The paragraphs entity has no content translation source field
          // if no paragraph entity field is translatable,
          // even if the host is.
          if ($item->entity
            ->hasField('content_translation_source')) {

            // Initialise the translation with source language values.
            $item->entity
              ->addTranslation($this->langcode, $entity
              ->toArray());
            $translation = $item->entity
              ->getTranslation($this->langcode);
            $manager = \Drupal::service('content_translation.manager');
            $manager
              ->getTranslationMetadata($translation)
              ->setSource($item->entity
              ->language()
              ->getId());
          }
        }

        // If any paragraphs type is translatable do not switch.
        if ($item->entity
          ->hasField('content_translation_source')) {

          // Switch the paragraph to the translation.
          $item->entity = $item->entity
            ->getTranslation($this->langcode);
        }
      }
      $items[$delta]->entity = $item->entity;
    }
  }
  $this->tempstore
    ->set($this->layoutParagraphsLayout);
}