ConfigurableLanguage.php 8.24 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\language\Entity\ConfigurableLanguage.
6 7
 */

8
namespace Drupal\language\Entity;
9

10
use Drupal\Core\Language\Language as LanguageObject;
11
use Drupal\Core\Config\Entity\ConfigEntityBase;
12
use Drupal\Core\Entity\EntityStorageInterface;
13
use Drupal\language\ConfigurableLanguageManagerInterface;
14
use Drupal\language\Exception\DeleteDefaultLanguageException;
15
use Drupal\language\ConfigurableLanguageInterface;
16 17

/**
18
 * Defines the ConfigurableLanguage entity.
19
 *
20
 * @ConfigEntityType(
21
 *   id = "configurable_language",
22
 *   label = @Translation("Language"),
23
 *   handlers = {
24
 *     "list_builder" = "Drupal\language\LanguageListBuilder",
25
 *     "access" = "Drupal\language\LanguageAccessControlHandler",
26
 *     "form" = {
27 28
 *       "add" = "Drupal\language\Form\LanguageAddForm",
 *       "edit" = "Drupal\language\Form\LanguageEditForm",
29 30
 *       "delete" = "Drupal\language\Form\LanguageDeleteForm"
 *     }
31
 *   },
32
 *   admin_permission = "administer languages",
33
 *   config_prefix = "entity",
34 35 36
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
37
 *     "weight" = "weight"
38 39
 *   },
 *   links = {
40 41
 *     "delete-form" = "entity.configurable_language.delete_form",
 *     "edit-form" = "entity.configurable_language.edit_form"
42 43 44
 *   }
 * )
 */
45
class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLanguageInterface {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  /**
   * The language ID (machine name).
   *
   * @var string
   */
  public $id;

  /**
   * The human-readable label for the language.
   *
   * @var string
   */
  public $label;

  /**
   * The direction of language, either DIRECTION_LTR or DIRECTION_RTL.
   *
   * @var integer
   */
  public $direction = '';

  /**
   * The weight of the language, used in lists of languages.
   *
   * @var integer
   */
  public $weight = 0;

  /**
   * Locked languages cannot be edited.
   *
   * @var bool
   */
  public $locked = FALSE;

82 83 84 85 86 87 88 89
  /**
   * Flag to indicate if the language entity is the default site language.
   *
   * This property is not saved to the language entity since there can be only
   * one default language. It is saved to system.site:langcode and set on the
   * container using the language.default service in when the entity is saved.
   * The value is set correctly when a language entity is created or loaded.
   *
90 91 92
   * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
   * @see \Drupal\language\Entity\ConfigurableLanguage::isDefault()
   * @see \Drupal\language\Entity\ConfigurableLanguage::setDefault()
93 94 95 96 97 98 99 100 101 102 103
   *
   * @var bool
   */
  protected $default;

  /**
   * Used during saving to detect when the site becomes multilingual.
   *
   * This property is not saved to the language entity, but is needed for
   * detecting when to rebuild the services.
   *
104 105
   * @see \Drupal\language\Entity\ConfigurableLanguage::preSave()
   * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
106 107 108 109 110
   *
   * @var bool
   */
  protected $preSaveMultilingual;

111 112 113 114 115 116 117 118 119 120
  /**
   * The language negotiation method used when a language was detected.
   *
   * The method ID, for example
   * \Drupal\language\LanguageNegotiatorInterface::METHOD_ID.
   *
   * @var string
   */
  public $methodId;

121 122 123 124
  /**
   * Sets the default flag on the language entity.
   *
   * @param bool $default
125
   *   TRUE if the language entity is the site default language, FALSE if not.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
   */
  public function setDefault($default) {
    $this->default = $default;
  }

  /**
   * Checks if the language entity is the site default language.
   *
   * @return bool
   *   TRUE if the language entity is the site default language, FALSE if not.
   */
  public function isDefault() {
    if (!isset($this->default)) {
      return static::getDefaultLangcode() == $this->id();
    }
    return $this->default;
  }

144 145 146
  /**
   * {@inheritdoc}
   */
147 148
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
149 150
    // Store whether or not the site is already multilingual so that we can
    // rebuild services if necessary during
151
    // \Drupal\language\Entity\ConfigurableLanguage::postSave().
152
    $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
153 154 155 156 157 158
    // Languages are picked from a predefined list which is given in English.
    // For the uncommon case of custom languages the label should be given in
    // English.
    $this->langcode = 'en';
  }

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    // Only set the default language and save it to system.site configuration if
    // it needs to updated.
    if ($this->isDefault() && static::getDefaultLangcode() != $this->id()) {
      // Update the config. Saving the configuration fires and event that causes
      // the container to be rebuilt.
      \Drupal::config('system.site')->set('langcode', $this->id())->save();
      \Drupal::service('language.default')->set($this->toLanguageObject());
    }

    $language_manager = \Drupal::languageManager();
    $language_manager->reset();
    if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
      $language_manager->updateLockedLanguageWeights();
    }

    // Update URL Prefixes for all languages after the new default language is
    // propagated and the LanguageManagerInterface::getLanguages() cache is
    // flushed.
    language_negotiation_url_prefixes_update();

    // If after adding this language the site will become multilingual, we need
    // to rebuild language services.
    if (!$this->preSaveMultilingual && !$update && $language_manager instanceof ConfigurableLanguageManagerInterface) {
      $language_manager::rebuildServices();
    }
  }

  /**
193
   * Converts the ConfigurableLanguage entity to a Core Language value object.
194 195 196 197
   *
   * @todo fix return type hint after https://drupal.org/node/2246665 and
   *   https://drupal.org/node/2246679.
   *
198
   * @return \Drupal\Core\Language\LanguageInterface
199 200 201 202 203 204 205 206 207 208 209 210 211
   *   The language configuration entity expressed as a Language value object.
   */
  protected function toLanguageObject() {
    return new LanguageObject(array(
      'id' => $this->id(),
      'name' => $this->label(),
      'direction' => $this->direction,
      'weight' => $this->weight,
      'locked' => $this->locked,
      'default' => $this->default,
    ));
  }

212 213 214
  /**
   * {@inheritdoc}
   *
215 216 217
   * @throws \DeleteDefaultLanguageException
   *   Exception thrown if we're trying to delete the default language entity.
   *   This is not allowed as a site must have a default language.
218
   */
219
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
220
    $default_langcode = static::getDefaultLangcode();
221
    foreach ($entities as $entity) {
222
      if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
223 224 225 226
        throw new DeleteDefaultLanguageException('Can not delete the default language');
      }
    }
  }
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

  /**
   * {@inheritdoc}
   */
  public function get($property_name) {
    if ($property_name == 'default') {
      return $this->isDefault();
    }
    else {
      return parent::get($property_name);
    }
  }

  /**
   * Gets the default langcode.
   *
   * @return string
   *   The current default langcode.
   */
  protected static function getDefaultLangcode() {
    $language = \Drupal::service('language.default')->get();
    return $language->getId();
  }

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->label();
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->label = $name;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->id();
  }

  /**
   * {@inheritdoc}
   */
  public function getDirection() {
    return $this->direction;
  }

  /**
   * {@inheritdoc}
   */
  public function setDirection($direction) {
    $this->direction = $direction;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return $this->weight;
  }

  /**
   * {@inheritdoc}
   */
  public function setWeight($weight) {
    $this->weight = $weight;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getNegotiationMethodId() {
    return $this->methodId;
  }

  /**
   * {@inheritdoc}
   */
  public function setNegotiationMethodId($method_id) {
    $this->methodId = $method_id;

    return $this;
  }

322
}