You are here

protected function Webform::initElementsTranslation in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Entity/Webform.php \Drupal\webform\Entity\Webform::initElementsTranslation()

Initialize elements translation.

Parameters

bool $elements_translated: Set elements translated flag so that translated elements can be alter by variants. This parameter is only used before applying variants.

See also

\Drupal\webform\Entity\Webform::applyVariants

2 calls to Webform::initElementsTranslation()
Webform::applyVariants in src/Entity/Webform.php
Apply webform variants based on a webform submission or parameter.
Webform::initElements in src/Entity/Webform.php
Initialize and parse webform elements.

File

src/Entity/Webform.php, line 1515

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

protected function initElementsTranslation($elements_translated = FALSE) {

  // If config translation is not enabled then return.
  if (!\Drupal::moduleHandler()
    ->moduleExists('config_translation')) {
    return;
  }

  // If the webform elements have been translated, then we no longer want
  // to get and apply element translations.
  // This allows variants to alter translated elements.
  // @see \Drupal\webform\Entity\Webform::applyVariants
  if ($this->elementsTranslated) {
    return;
  }

  // If the webform elements are being updated, do not alter them.
  if ($this->updating) {
    return;
  }

  // Get the current langcode.
  $current_langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();

  // If the current langcode is the same as this webform's langcode
  // then return.
  if ($this->langcode === $current_langcode) {
    return;
  }

  /** @var \Drupal\webform\WebformTranslationManagerInterface $translation_manager */
  $translation_manager = \Drupal::service('webform.translation_manager');

  // For admin routes we need to get the original untranslated elements.
  if ($translation_manager
    ->isAdminRoute()) {
    $this->elements = WebformYaml::encode($translation_manager
      ->getElements($this));
    return;
  }

  // Get the webform's decoded elements and translations.
  $elements = WebformYaml::decode($this->elements);
  $elementsTranslations = $translation_manager
    ->getElements($this, $current_langcode);

  // If the elements are empty or they are equal to the translated elements
  // then, we need to load the elements in the original language.
  if (empty($elementsTranslations) || $elementsTranslations === $elements) {
    $elements = $translation_manager
      ->getElements($this);
    $this->elements = WebformYaml::encode($elements);
  }

  // Set the element's translations.
  $this->elementsTranslations = $elementsTranslations;

  // Ensure that translated webform elements are set and can be altered
  // by a variant.
  if ($elements_translated && $this->elementsTranslations) {

    // Init elements and reset them with the translations.
    $this
      ->initElementsTranslationsRecursive($elements);
    $this
      ->setElements($elements);

    // Clear the elements translations and stop the elements from
    // being translated.
    $this->elementsTranslations = [];
    $this->elementsTranslated = TRUE;
  }
}