LocaleTypedConfig.php 5.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\locale\LocaleTypedConfig.
 */

namespace Drupal\locale;

use Drupal\Core\TypedData\ContextAwareInterface;
11
use Drupal\Core\TypedData\DataDefinitionInterface;
12
use Drupal\Core\Config\Schema\Element;
13
use Drupal\Core\Config\TypedConfigManagerInterface;
14 15
use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\Core\TypedData\TypedDataInterface;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * Defines the locale configuration wrapper object.
 */
class LocaleTypedConfig extends Element {

  /**
   * The typed configuration data.
   *
   * @var \Drupal\Core\Config\Schema\Element
   */
  protected $typedConfig;

  /**
   * The language code for which this is a translation.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The locale configuration manager object.
   *
   * @var \Drupal\locale\LocaleConfigManager
   */
  protected $localeConfig;

43 44 45 46 47 48 49
  /**
   * The typed config manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

50 51 52
  /**
   * Constructs a configuration wrapper object.
   *
53
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
54 55 56 57 58
   *   The data definition.
   * @param string $name
   *   The configuration object name.
   * @param string $langcode
   *   Language code for the source configuration data.
59
   * @param \Drupal\locale\LocaleConfigManager $locale_config
60
   *   The locale configuration manager object.
61 62
   * @param \Drupal\locale\TypedConfigManagerInterface $typed_config;
   *   The typed configuration manager interface.
63
   */
64
  public function __construct(DataDefinitionInterface $definition, $name, $langcode, LocaleConfigManager $locale_config, TypedConfigManagerInterface $typed_config) {
65 66
    parent::__construct($definition, $name);
    $this->langcode = $langcode;
67
    $this->localeConfig = $locale_config;
68
    $this->typedConfigManager = $typed_config;
69 70 71 72 73 74
  }

  /**
   * Gets wrapped typed config object.
   */
  public function getTypedConfig() {
75
    return $this->typedConfigManager->create($this->definition, $this->value);
76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * {@inheritdoc}
   */
  public function getTranslation($langcode) {
    $options = array(
      'source' => $this->langcode,
      'target' => $langcode,
    );
    $data = $this->getElementTranslation($this->getTypedConfig(), $options);
87
    return $this->typedConfigManager->create($this->definition, $data);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  }

  /**
   * {@inheritdoc}
   */
  public function language() {
    return language_load($this->langcode);
  }

  /**
   * Checks whether we can translate these languages.
   *
   * @param string $from_langcode
   *   Source language code.
   * @param string $to_langcode
   *   Destination language code.
   *
   * @return bool
   *   TRUE if this translator supports translations for these languages.
   */
  protected function canTranslate($from_langcode, $to_langcode) {
    if ($from_langcode == 'en') {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Gets translated configuration data for a typed configuration element.
   *
118 119
   * @param \Drupal\Core\TypedData\TypedDataInterface $element
   *   Typed configuration element.
120 121
   * @param array $options
   *   Array with translation options that must contain the keys defined in
122
   *   \Drupal\locale\LocaleTypedConfig::translateElement().
123 124 125 126 127
   *
   * @return array
   *   Configuration data translated to the requested language if available,
   *   an empty array otherwise.
   */
128
  protected function getElementTranslation(TypedDataInterface $element, array $options) {
129
    $translation = array();
130
    if ($element instanceof TraversableTypedDataInterface) {
131 132 133 134 135 136 137 138 139
      $translation = $this->getArrayTranslation($element, $options);
    }
    elseif ($this->translateElement($element, $options)) {
      $translation = $element->getValue();
    }
    return $translation;
  }

  /**
140
   * Gets translated configuration data for a traversable element.
141
   *
142
   * @param \Drupal\Core\TypedData\TraversableTypedDataInterface $element
143 144 145
   *   Typed configuration array element.
   * @param array $options
   *   Array with translation options that must contain the keys defined in
146
   *   \Drupal\locale\LocaleTypedConfig::translateElement().
147 148 149 150
   *
   * @return array
   *   Configuration data translated to the requested language.
   */
151
  protected function getArrayTranslation(TraversableTypedDataInterface $element, array $options) {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    $translation = array();
    foreach ($element as $key => $property) {
      $value = $this->getElementTranslation($property, $options);
      if (!empty($value)) {
        $translation[$key] = $value;
      }
    }
    return $translation;
  }

  /**
   * Translates element's value if it fits our translation criteria.
   *
   * For an element to be translatable by locale module it needs to be of base
   * type 'string' and have 'translatable = TRUE' in the element's definition.
   * Translatable elements may use these additional keys in their data
   * definition:
   * - 'translatable', FALSE to opt out of translation.
170
   * - 'translation context', to define the string context.
171 172 173 174 175 176 177 178 179 180 181
   *
   * @param \Drupal\Core\TypedData\TypedDataInterface $element
   *   Configuration element.
   * @param array $options
   *   Array with translation options that must contain the following keys:
   *   - 'source', Source language code.
   *   - 'target', Target language code.
   *
   * @return bool
   *   Whether the element fits the translation criteria.
   */
182
  protected function translateElement(TypedDataInterface $element, array $options) {
183
    if ($this->canTranslate($options['source'], $options['target'])) {
184
      $definition = $element->getDataDefinition();
185 186
      $value = $element->getValue();
      if ($value && !empty($definition['translatable'])) {
187
        $context = isset($definition['translation context']) ? $definition['translation context'] : '';
188 189 190 191 192 193 194 195 196 197 198
        if ($translation = $this->localeConfig->translateString($this->name, $options['target'], $value, $context)) {
          $element->setValue($translation);
          return TRUE;
        }
      }
    }
    // The element does not have a translation.
    return FALSE;
  }

}