ConfigurableLanguage.php 10.2 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 14
use Drupal\Core\Language\LanguageManager;
use Drupal\language\ConfigurableLanguageManager;
15
use Drupal\language\ConfigurableLanguageManagerInterface;
16
use Drupal\language\Exception\DeleteDefaultLanguageException;
17
use Drupal\language\ConfigurableLanguageInterface;
18 19

/**
20
 * Defines the ConfigurableLanguage entity.
21
 *
22
 * @ConfigEntityType(
23
 *   id = "configurable_language",
24
 *   label = @Translation("Language"),
25
 *   handlers = {
26
 *     "list_builder" = "Drupal\language\LanguageListBuilder",
27
 *     "access" = "Drupal\language\LanguageAccessControlHandler",
28
 *     "form" = {
29 30
 *       "add" = "Drupal\language\Form\LanguageAddForm",
 *       "edit" = "Drupal\language\Form\LanguageEditForm",
31 32
 *       "delete" = "Drupal\language\Form\LanguageDeleteForm"
 *     }
33
 *   },
34
 *   admin_permission = "administer languages",
35
 *   config_prefix = "entity",
36 37 38
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
39
 *     "weight" = "weight"
40 41
 *   },
 *   links = {
42 43
 *     "delete-form" = "entity.configurable_language.delete_form",
 *     "edit-form" = "entity.configurable_language.edit_form"
44 45 46
 *   }
 * )
 */
47
class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLanguageInterface {
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 82 83

  /**
   * 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;

84 85 86 87 88 89 90 91
  /**
   * 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.
   *
92 93 94
   * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
   * @see \Drupal\language\Entity\ConfigurableLanguage::isDefault()
   * @see \Drupal\language\Entity\ConfigurableLanguage::setDefault()
95 96 97 98 99 100 101 102 103 104 105
   *
   * @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.
   *
106 107
   * @see \Drupal\language\Entity\ConfigurableLanguage::preSave()
   * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
108 109 110 111 112
   *
   * @var bool
   */
  protected $preSaveMultilingual;

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

123 124 125 126
  /**
   * Sets the default flag on the language entity.
   *
   * @param bool $default
127
   *   TRUE if the language entity is the site default language, FALSE if not.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
   */
  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;
  }

146 147 148
  /**
   * {@inheritdoc}
   */
149 150
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
151 152
    // Store whether or not the site is already multilingual so that we can
    // rebuild services if necessary during
153
    // \Drupal\language\Entity\ConfigurableLanguage::postSave().
154
    $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
155 156 157 158 159 160
    // 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';
  }

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
  /**
   * {@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();
    }
192 193 194 195
    if (!$update) {
      // Install any available language configuration overrides for the language.
      \Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id());
    }
196 197 198
  }

  /**
199
   * Converts the ConfigurableLanguage entity to a Core Language value object.
200 201 202 203
   *
   * @todo fix return type hint after https://drupal.org/node/2246665 and
   *   https://drupal.org/node/2246679.
   *
204
   * @return \Drupal\Core\Language\LanguageInterface
205 206 207 208 209 210 211 212 213 214 215 216 217
   *   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,
    ));
  }

218 219 220
  /**
   * {@inheritdoc}
   *
221 222 223
   * @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.
224
   */
225
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
226
    $default_langcode = static::getDefaultLangcode();
227
    foreach ($entities as $entity) {
228
      if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
229 230 231 232
        throw new DeleteDefaultLanguageException('Can not delete the default language');
      }
    }
  }
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);
    $language_manager = \Drupal::languageManager();
    $language_manager->reset();
    if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
      $language_manager->updateLockedLanguageWeights();
    }
    // If after deleting this language the site will become monolingual, we need
    // to rebuild language services.
    if (!\Drupal::languageManager()->isMultilingual()) {
      ConfigurableLanguageManager::rebuildServices();
    }
  }

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  /**
   * {@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();
  }

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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  /**
   * {@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;
  }

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  /**
   * Creates a configurable language object from a langcode.
   *
   * @param string $langcode
   *   The language code to use to create the object.
   *
   * @return $this
   *
   * @see \Drupal\Core\Language\LanguageManager::getStandardLanguageList()
   */
  public static function createFromLangcode($langcode) {
    $standard_languages = LanguageManager::getStandardLanguageList();
    if (!isset($standard_languages[$langcode])) {
      // Drupal does not know about this language, so we set its values with the
      // best guess. The user will be able to edit afterwards.
      return static::create(array(
        'id' => $langcode,
        'label' => $langcode,
      ));
    }
    else {
      // A known predefined language, details will be filled in properly.
      return static::create(array(
        'id' => $langcode,
        'label' => $standard_languages[$langcode][0],
        'direction' => isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : static::DIRECTION_LTR,
      ));
    }
  }

375
}