You are here

class YamlFormTranslationManager in YAML Form 8

Defines a class to translate form elements.

Hierarchy

Expanded class hierarchy of YamlFormTranslationManager

1 string reference to 'YamlFormTranslationManager'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormTranslationManager
yamlform.translation_manager in ./yamlform.services.yml
Drupal\yamlform\YamlFormTranslationManager

File

src/YamlFormTranslationManager.php, line 14

Namespace

Drupal\yamlform
View source
class YamlFormTranslationManager implements YamlFormTranslationManagerInterface {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The configuration object factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Form element manager.
   *
   * @var \Drupal\yamlform\YamlFormElementManagerInterface
   */
  protected $elementManager;

  /**
   * Constructs a YamlFormTranslationManager object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration object factory.
   * @param \Drupal\yamlform\YamlFormElementManagerInterface $element_manager
   *   The form element manager.
   */
  public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, YamlFormElementManagerInterface $element_manager) {
    $this->languageManager = $language_manager;
    $this->configFactory = $config_factory;
    $this->elementManager = $element_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigElements(YamlFormInterface $yamlform, $langcode, $reset = FALSE) {

    // Note: Below code return the default languages elements for missing
    // translations.
    $config_override_language = $this->languageManager
      ->getConfigOverrideLanguage();
    $config_name = 'yamlform.yamlform.' . $yamlform
      ->id();

    // Reset cached config.
    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);
    return $elements ? Yaml::decode($elements) : [];
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseElements(YamlFormInterface $yamlform) {
    $default_langcode = $this->languageManager
      ->getDefaultLanguage()
      ->getId();
    $config_elements = $this
      ->getConfigElements($yamlform, $default_langcode);
    $elements = YamlFormElementHelper::getFlattened($config_elements);
    $translatable_properties = YamlFormArrayHelper::addPrefix($this->elementManager
      ->getTranslatableProperties());
    foreach ($elements as $element_key => &$element) {
      foreach ($element as $property_key => $property_value) {
        $translatable_property_key = $property_key;

        // If translatable property key is a sub element (ex: subelement__title)
        // get the sub element's translatable 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 options and answers that are form option ids.
          unset($element[$property_key]);
        }
        elseif (!isset($translatable_properties[$translatable_property_key])) {

          // Unset none translatble properties.
          unset($element[$property_key]);
        }
      }
      if (empty($element)) {
        unset($elements[$element_key]);
      }
    }
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function getSourceElements(YamlFormInterface $yamlform) {
    $elements = $this
      ->getBaseElements($yamlform);
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function getTranslationElements(YamlFormInterface $yamlform, $langcode) {
    $elements = $this
      ->getSourceElements($yamlform);
    $translation_elements = $this
      ->getConfigElements($yamlform, $langcode);
    if ($elements == $translation_elements) {
      return $elements;
    }
    YamlFormElementHelper::merge($elements, $translation_elements);
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlFormTranslationManager::$configFactory protected property The configuration object factory.
YamlFormTranslationManager::$elementManager protected property Form element manager.
YamlFormTranslationManager::$languageManager protected property The language manager.
YamlFormTranslationManager::getBaseElements public function Get base form elements from the site's default language. Overrides YamlFormTranslationManagerInterface::getBaseElements
YamlFormTranslationManager::getConfigElements public function Get form elements for specific language. Overrides YamlFormTranslationManagerInterface::getConfigElements
YamlFormTranslationManager::getSourceElements public function Get flattened associative array of translated element properties. Overrides YamlFormTranslationManagerInterface::getSourceElements
YamlFormTranslationManager::getTranslationElements public function Get flattened associative array of translated element properties. Overrides YamlFormTranslationManagerInterface::getTranslationElements
YamlFormTranslationManager::__construct public function Constructs a YamlFormTranslationManager object.