WebformTranslationLingotekManager.php in Webform 6.x
File
src/WebformTranslationLingotekManager.php
View source
<?php
namespace Drupal\webform;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Serialization\Yaml;
class WebformTranslationLingotekManager implements WebformTranslationLingotekManagerInterface {
protected $translationManager;
public function __construct(WebformTranslationManagerInterface $translation_manager) {
$this->translationManager = $translation_manager;
}
public function configEntityDocumentUpload(array &$source_data, ConfigEntityInterface &$entity, &$url) {
switch ($entity
->getEntityTypeId()) {
case 'field_config':
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':
$translation_elements = $this->translationManager
->getTranslationElements($entity, $entity
->language()
->getId());
$source_data['elements'] = $translation_elements;
$this
->encodeTokens($source_data);
break;
case 'webform_image_select_images':
$source_data['images'] = Yaml::decode($source_data['images']);
break;
case 'webform_options':
case 'webform_options_custom':
$options = Yaml::decode($source_data['options']);
$optgroups = [];
foreach ($options as $option_value => $option_text) {
if (is_array($option_text)) {
$optgroups[$option_value] = $option_value;
}
}
if ($optgroups) {
$options['_optgroups_'] = $optgroups;
}
$source_data['options'] = $options;
break;
}
}
public function configEntityTranslationPresave(ConfigEntityInterface &$translation, $langcode, &$data) {
switch ($translation
->getEntityTypeId()) {
case 'field_config':
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);
$translation
->setElements($data['elements']);
$data['elements'] = Yaml::encode($data['elements']);
break;
case 'webform_image_select_images':
$translation
->setImages($data['images']);
$data['images'] = Yaml::encode($data['images']);
break;
case 'webform_options':
case 'webform_options_custom':
$options = $data['options'];
if (isset($options['_optgroups_'])) {
$optgroups = $options['_optgroups_'];
unset($options['_optgroups_']);
$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;
}
}
$options = $optgroups_options;
}
$translation
->setOptions($options);
$data['options'] = Yaml::encode($options);
break;
}
}
public function configObjectDocumentUpload(array &$data, $config_name) {
if ($config_name !== 'webform.settings') {
return;
}
$data['webform.settings']['test.types'] = Yaml::decode($data['webform.settings']['test.types']);
$data['webform.settings']['test.names'] = Yaml::decode($data['webform.settings']['test.names']);
$this
->encodeTokens($data);
}
public function configObjectTranslationPresave(array &$data, $config_name) {
if ($config_name !== 'webform.settings') {
return;
}
$this
->decodeTokens($data);
$data['webform.settings']['test.types'] = Yaml::encode($data['webform.settings']['test.types']);
$data['webform.settings']['test.names'] = Yaml::encode($data['webform.settings']['test.names']);
}
protected function encodeTokens(array &$data) {
$yaml = Yaml::encode($data);
$yaml = preg_replace_callback('/\\[([a-z][^]]+)\\]/', function ($matches) {
$replacement = mb_encode_numericentity($matches[1], [
0x0,
0x10ffff,
0,
0xffffff,
], 'UTF-8');
return "[{$replacement}]";
}, $yaml);
$data = Yaml::decode($yaml);
}
protected function decodeTokens(array &$data) {
$yaml = Yaml::encode($data);
$yaml = preg_replace_callback('/\\[([^]]+?)\\]/', function ($matches) {
$token = mb_decode_numericentity($matches[1], [
0x0,
0x10ffff,
0,
0xffffff,
], 'UTF-8');
return "[{$token}]";
}, $yaml);
$data = Yaml::decode($yaml);
}
}