WebformTranslationManager.php in Webform 6.x
File
src/WebformTranslationManager.php
View source
<?php
namespace Drupal\webform;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\webform\Plugin\WebformElement\WebformCompositeBase;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Drupal\webform\Utility\WebformArrayHelper;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformYaml;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\webform\Plugin\WebformElement\WebformCustomComposite;
class WebformTranslationManager implements WebformTranslationManagerInterface {
protected $routeMatch;
protected $languageManager;
protected $configFactory;
protected $elementManager;
protected $translatableProperties;
protected $messenger;
public function __construct(RouteMatchInterface $route_match, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, WebformElementManagerInterface $element_manager) {
$this->routeMatch = $route_match;
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
$this->messenger = $messenger;
$this->elementManager = $element_manager;
}
public function isAdminRoute() {
$route_name = $this->routeMatch
->getRouteName();
if (preg_match('/^entity\\.webform\\.(?:edit_form|duplicate_form|delete_form)$/', $route_name)) {
return TRUE;
}
if (strpos($route_name, 'entity.webform_ui.') === 0) {
return TRUE;
}
return FALSE;
}
public function getElements(WebformInterface $webform, $langcode = NULL, $reset = FALSE) {
$config_override_language = $this->languageManager
->getConfigOverrideLanguage();
$config_name = 'webform.webform.' . $webform
->id();
if (!$langcode) {
$langcode = $this
->getOriginalLangcode($webform);
}
if ($reset) {
$this->configFactory
->reset($config_name);
}
$this->languageManager
->setConfigOverrideLanguage($this->languageManager
->getLanguage($langcode));
$elements = $this->configFactory
->get($config_name)
->get('elements');
$this->languageManager
->setConfigOverrideLanguage($config_override_language);
if (!$elements) {
return [];
}
elseif ($error = WebformYaml::validate($elements)) {
$this->messenger
->addError($error);
return [];
}
else {
return Yaml::decode($elements);
}
}
public function getBaseElements(WebformInterface $webform) {
$default_langcode = $this
->getOriginalLangcode($webform) ?: $this->languageManager
->getDefaultLanguage()
->getId();
$config_elements = $this
->getElements($webform, $default_langcode);
$elements = WebformElementHelper::getFlattened($config_elements);
foreach ($elements as $element_key => &$element) {
$element_plugin = $this->elementManager
->getElementInstance($element);
if ($element_plugin instanceof WebformCompositeBase) {
$composite_elements = $element_plugin
->getCompositeElements();
foreach ($composite_elements as $composite_key => $composite_element) {
$property_key = $composite_key . '__title';
if (empty($element["#{$property_key}"])) {
$element["#{$property_key}"] = $element_plugin
->getDefaultProperty($property_key);
}
}
}
$this
->removeUnTranslatablePropertiesFromElement($element);
if (empty($element)) {
unset($elements[$element_key]);
}
}
return $elements;
}
public function getSourceElements(WebformInterface $webform) {
return $this
->getBaseElements($webform);
}
public function getTranslationElements(WebformInterface $webform, $langcode) {
$elements = $this
->getSourceElements($webform);
$translation_elements = $this
->getElements($webform, $langcode);
if ($elements === $translation_elements) {
return $elements;
}
WebformElementHelper::merge($elements, $translation_elements);
return $elements;
}
public function getOriginalLangcode(WebformInterface $webform) {
$mapper = \Drupal::service('plugin.manager.config_translation.mapper')
->createInstance('webform');
$mapper
->addConfigName('webform.webform.' . $webform
->id());
return $mapper
->getLangcode();
}
protected function removeUnTranslatablePropertiesFromElement(array &$element) {
$element_type = isset($element['#type']) ? $element['#type'] : NULL;
$translatable_properties = $this
->getTranslatableProperties($element_type);
$element_plugin = $this->elementManager
->getElementInstance($element);
foreach ($element as $property_key => $property_value) {
$translatable_property_key = $property_key;
if (preg_match('/^.*__(.*)$/', $translatable_property_key, $match)) {
$translatable_property_key = '#' . $match[1];
}
if (in_array($translatable_property_key, [
'#options',
'#answers',
]) && is_string($property_value)) {
unset($element[$property_key]);
}
elseif ($translatable_property_key === '#element' && $element_plugin instanceof WebformCustomComposite) {
foreach ($element[$property_key] as &$composite_element_value) {
$this
->removeUnTranslatablePropertiesFromElement($composite_element_value);
}
}
elseif (!isset($translatable_properties[$translatable_property_key])) {
unset($element[$property_key]);
}
}
}
protected function getTranslatableProperties($type) {
if (isset($this->translatableProperties[$type])) {
return $this->translatableProperties[$type];
}
$element_plugin = $this->elementManager
->createInstance($type);
$translatable_properties = $element_plugin
->getTranslatableProperties();
$translatable_properties = array_combine($translatable_properties, $translatable_properties);
$this->translatableProperties[$type] = WebformArrayHelper::addPrefix($translatable_properties);
return $this->translatableProperties[$type];
}
}