You are here

public function WebformTranslationLingotekManager::configEntityTranslationPresave in Webform 6.x

Implements hook_lingotek_config_entity_translation_presave().

Parameters

\Drupal\Core\Config\Entity\ConfigEntityInterface &$translation: The config entity that is going to be saved.

string $langcode: Drupal language code that has been downloaded.

array &$data: Data returned from the Lingotek service when asking for the translation.

Overrides WebformTranslationLingotekManagerInterface::configEntityTranslationPresave

See also

hook_lingotek_config_entity_translation_presave().

File

src/WebformTranslationLingotekManager.php, line 91

Class

WebformTranslationLingotekManager
Defines a class to translate webform Lingotek integration.

Namespace

Drupal\webform

Code

public function configEntityTranslationPresave(ConfigEntityInterface &$translation, $langcode, &$data) {
  switch ($translation
    ->getEntityTypeId()) {
    case 'field_config':

      // Convert webform default data associative array back to YAML string.

      /** @var \Drupal\field\Entity\FieldConfig $translation */
      if ($translation
        ->getFieldStorageDefinition()
        ->getType() === 'webform') {
        foreach ($data as &$field_settings) {
          $this
            ->encodeTokens($field_settings);
          foreach ($field_settings as $setting_name => $setting_value) {
            if (preg_match('/\\.default_data$/', $setting_name)) {
              $field_settings[$setting_name] = $field_settings[$setting_name] ? Yaml::encode($field_settings[$setting_name]) : '';
            }
          }
        }
      }
      break;
    case 'webform':
      $this
        ->decodeTokens($data);

      /** @var \Drupal\webform\WebformInterface $translation */
      $translation
        ->setElements($data['elements']);
      $data['elements'] = Yaml::encode($data['elements']);
      break;
    case 'webform_image_select_images':

      /** @var \Drupal\webform_image_select\WebformImageSelectImagesInterface $translation */

      // Convert images associative array back to YAML string.
      $translation
        ->setImages($data['images']);
      $data['images'] = Yaml::encode($data['images']);
      break;
    case 'webform_options':
    case 'webform_options_custom':
      $options = $data['options'];

      // If '_optgroups_' are defined we need to translate the optgroups.
      if (isset($options['_optgroups_'])) {

        // Get optgroup from options.
        $optgroups = $options['_optgroups_'];
        unset($options['_optgroups_']);

        // Build translated optgroup options.
        $optgroups_options = [];
        foreach ($options as $option_value => $option_text) {
          if (is_array($option_text)) {
            $optgroups_options[$optgroups[$option_value]] = $option_text;
          }
          else {
            $optgroup_options[$option_value] = $option_text;
          }
        }

        // Replace options with optgroup options.
        $options = $optgroups_options;
      }

      /** @var \Drupal\webform\WebformOptionsInterface $translation */

      // Convert options associative array back to YAML string.
      $translation
        ->setOptions($options);
      $data['options'] = Yaml::encode($options);
      break;
  }
}