You are here

public function WebformTranslationManager::getElements in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformTranslationManager.php \Drupal\webform\WebformTranslationManager::getElements()

Get webform elements for specific language.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

string $langcode: The language code for the webform elements.

bool $reset: (optional) Whether to reset the translated config cache. Defaults to FALSE.

Return value

array A webform's translated elements.

Overrides WebformTranslationManagerInterface::getElements

2 calls to WebformTranslationManager::getElements()
WebformTranslationManager::getBaseElements in src/WebformTranslationManager.php
Get base webform elements for the site's default language.
WebformTranslationManager::getTranslationElements in src/WebformTranslationManager.php
Get flattened associative array of translated element properties.

File

src/WebformTranslationManager.php, line 108

Class

WebformTranslationManager
Defines a class to translate webform elements.

Namespace

Drupal\webform

Code

public function getElements(WebformInterface $webform, $langcode = NULL, $reset = FALSE) {

  // Note: Below code return the default languages elements for missing
  // translations.
  $config_override_language = $this->languageManager
    ->getConfigOverrideLanguage();
  $config_name = 'webform.webform.' . $webform
    ->id();

  // Set langcode from original langcode.
  if (!$langcode) {
    $langcode = $this
      ->getOriginalLangcode($webform);
  }

  // Reset cached config.
  if ($reset) {
    $this->configFactory
      ->reset($config_name);
  }
  $this->languageManager
    ->setConfigOverrideLanguage($this->languageManager
    ->getLanguage($langcode));
  $elements = $this->configFactory
    ->get($config_name)
    ->get('elements');
  $this->languageManager
    ->setConfigOverrideLanguage($config_override_language);
  if (!$elements) {
    return [];
  }
  elseif ($error = WebformYaml::validate($elements)) {
    $this->messenger
      ->addError($error);
    return [];
  }
  else {
    return Yaml::decode($elements);
  }
}