You are here

function webform_lingotek_config_entity_document_upload in Webform 8.5

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

Implements hook_lingotek_config_entity_document_upload().

File

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

Code

function webform_lingotek_config_entity_document_upload(array &$source_data, ConfigEntityInterface &$entity, &$url) {
  switch ($entity
    ->getEntityTypeId()) {
    case 'field_config':

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

      /** @var \Drupal\field\Entity\FieldConfig $entity */
      if ($entity
        ->getFieldStorageDefinition()
        ->getType() === 'webform') {
        foreach ($source_data as &$field_settings) {
          foreach ($field_settings as $setting_name => $setting_value) {
            if (preg_match('/\\.default_data$/', $setting_name)) {
              $field_settings[$setting_name] = Yaml::decode($field_settings[$setting_name]);
            }
          }
        }
        _webform_lingotek_encode_tokens($field_settings);
      }
      break;
    case 'webform':

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

      // Replace elements with just the translatable properties
      // (i.e. #title, #description, #options, etc…) so that Lingotek's
      // translation services can correctly translate each element.
      $translation_elements = $translation_manager
        ->getTranslationElements($entity, $entity
        ->language()
        ->getId());
      $source_data['elements'] = $translation_elements;
      _webform_lingotek_encode_tokens($source_data);
      break;
    case 'webform_options':
    case 'webform_options_custom':

      // Convert options YAML string to an associative array.
      $options = Yaml::decode($source_data['options']);

      // Extract optgroups from the options and append them as '_optgroups_'
      // to the options so that the optgroups can be translated.
      $optgroups = [];
      foreach ($options as $option_value => $option_text) {
        if (is_array($option_text)) {
          $optgroups[$option_value] = $option_value;
        }
      }
      if ($optgroups) {
        $options['_optgroups_'] = $optgroups;
      }

      // Update source data's options.
      $source_data['options'] = $options;
      break;
  }
}