You are here

function webform_lingotek_config_entity_translation_presave in Webform 8.5

Same name and namespace in other branches
  1. 6.x includes/webform.translation.inc \webform_lingotek_config_entity_translation_presave()

Implements hook_lingotek_config_entity_translation_presave().

File

includes/webform.translation.inc, line 310
Webform module translation hooks.

Code

function webform_lingotek_config_entity_translation_presave(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) {
          _webform_lingotek_encode_tokens($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':
      _webform_lingotek_decode_tokens($data);

      /** @var \Drupal\webform\WebformInterface $translation */
      $translation
        ->setElements($data['elements']);
      $data['elements'] = Yaml::encode($data['elements']);
      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;
  }
}