You are here

public function WebformTranslationLingotekManager::configEntityDocumentUpload in Webform 6.x

Implements hook_lingotek_config_entity_document_upload().

Parameters

array &$source_data: The data that will be uploaded, as an associative array.

\Drupal\Core\Config\Entity\ConfigEntityInterface &$entity: The config entity where the data is extracted from and will be associated to the Lingotek document.

string &$url: The url which will be associated to this document, e.g. for context review.

Overrides WebformTranslationLingotekManagerInterface::configEntityDocumentUpload

See also

hook_lingotek_config_entity_document_upload().

File

src/WebformTranslationLingotekManager.php, line 33

Class

WebformTranslationLingotekManager
Defines a class to translate webform Lingotek integration.

Namespace

Drupal\webform

Code

public function configEntityDocumentUpload(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]);
            }
          }
        }
        $this
          ->encodeTokens($field_settings);
      }
      break;
    case 'webform':

      // 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 = $this->translationManager
        ->getTranslationElements($entity, $entity
        ->language()
        ->getId());
      $source_data['elements'] = $translation_elements;
      $this
        ->encodeTokens($source_data);
      break;
    case 'webform_image_select_images':

      // Convert images YAML string to an associative array.
      $source_data['images'] = Yaml::decode($source_data['images']);
      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;
  }
}