You are here

public static function WebformElementHelper::applyTranslation in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformElementHelper.php \Drupal\webform\Utility\WebformElementHelper::applyTranslation()

Apply translation to element.

IMPORTANT: This basically a modified version WebformElementHelper::merge() that initially only merge element properties and ignores sub-element.

Parameters

array $element: An element.

array $translation: An associative array of translated element properties.

2 calls to WebformElementHelper::applyTranslation()
Webform::initElementsRecursive in src/Entity/Webform.php
Initialize webform elements into a flatten array.
Webform::initElementsTranslationsRecursive in src/Entity/Webform.php
Init elements translations before variants are applied.

File

src/Utility/WebformElementHelper.php, line 551

Class

WebformElementHelper
Helper class webform element methods.

Namespace

Drupal\webform\Utility

Code

public static function applyTranslation(array &$element, array $translation) {

  // Apply all translated properties to the element.
  // This allows default properties to be translated, which includes
  // composite element titles.
  $element += $translation;
  foreach ($element as $key => &$value) {

    // Make sure to only merge properties.
    if (!static::property($key) || empty($translation[$key])) {
      continue;
    }
    $translation_value = $translation[$key];
    if (gettype($value) !== gettype($translation_value)) {
      continue;
    }
    if (is_array($value)) {
      self::merge($value, $translation_value);
    }
    elseif (is_scalar($value)) {
      $element[$key] = $translation_value;
    }
  }
}