You are here

protected function WebformTranslationConfigManager::alterTextareaElement in Webform 6.x

Alter text area element and convert it to a Codemirror editor.

Parameters

array $element: A element containing 'source' and 'translation'.

string $mode.: Codemirror editor mode. Default to 'yaml'.

3 calls to WebformTranslationConfigManager::alterTextareaElement()
WebformTranslationConfigManager::alterConfigFieldForm in src/WebformTranslationConfigManager.php
Alter the webform field configuration form.
WebformTranslationConfigManager::alterConfigWebformFormHandlers in src/WebformTranslationConfigManager.php
Alter the webform configuration form handlers.
WebformTranslationConfigManager::alterSchemaElementsRecursive in src/WebformTranslationConfigManager.php
Alter schema elements.

File

src/WebformTranslationConfigManager.php, line 901

Class

WebformTranslationConfigManager
Defines a class to translate webform config.

Namespace

Drupal\webform

Code

protected function alterTextareaElement(array &$element, $mode = 'yaml') {

  // Source.
  $source_value = trim((string) $element['source']['#markup']);
  $source_value = preg_replace('#^<span lang="[^"]+">(.*)</span>#ims', '\\1', $source_value);

  // Translation.
  $translation_value = $element['translation']['#default_value'];

  // Alter source and translation values based on the mode.
  switch ($mode) {
    case 'twig':
      $source_value = preg_replace('#<br />#s', '', $source_value);
      break;
    case 'yaml':
      $source_value = strip_tags($source_value);
      $source_value = $source_value ? trim(WebformYaml::tidy($source_value)) : '';
      $translation_value = $translation_value ? trim(WebformYaml::tidy($translation_value)) : '';
      break;
  }

  // Source.
  $element['source']['#wrapper_attributes']['class'][] = 'webform-translation-source';
  $element['source']['value'] = [
    '#type' => 'webform_codemirror',
    '#mode' => $mode,
    '#value' => $source_value,
    '#disabled' => TRUE,
    '#attributes' => [
      'readonly' => TRUE,
    ],
  ];
  unset($element['source']['#markup']);

  // Translation.
  $element['translation']['#type'] = 'webform_codemirror';
  $element['translation']['#mode'] = $mode;
  $element['translation']['#default_value'] = $translation_value;
  $element['#attached']['library'][] = 'webform/webform.admin.translation';
}