ConfigurableLanguage.php 7.08 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\Config\Entity\ConfigEntityBase;
11
use Drupal\Core\Entity\EntityStorageInterface;
12 13
use Drupal\Core\Language\LanguageManager;
use Drupal\language\ConfigurableLanguageManager;
14
use Drupal\language\ConfigurableLanguageManagerInterface;
15
use Drupal\language\Exception\DeleteDefaultLanguageException;
16
use Drupal\language\ConfigurableLanguageInterface;
17 18

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

  /**
   * The language ID (machine name).
   *
   * @var string
   */
53
  protected $id;
54 55 56 57 58 59 60 61 62 63 64 65 66

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

  /**
   * The direction of language, either DIRECTION_LTR or DIRECTION_RTL.
   *
   * @var integer
   */
67
  protected $direction = self::DIRECTION_LTR;
68 69 70 71 72 73

  /**
   * The weight of the language, used in lists of languages.
   *
   * @var integer
   */
74
  protected $weight = 0;
75 76 77 78 79 80

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

83 84 85 86 87 88
  /**
   * 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.
   *
89 90
   * @see \Drupal\language\Entity\ConfigurableLanguage::preSave()
   * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
91 92 93 94 95 96
   *
   * @var bool
   */
  protected $preSaveMultilingual;

  /**
97
   * {@inheritdoc}
98 99
   */
  public function isDefault() {
100
    return static::getDefaultLangcode() == $this->id();
101 102
  }

103 104 105 106 107 108 109
  /**
   * {@inheritdoc}
   */
  public function isLocked() {
    return (bool) $this->locked;
  }

110 111 112
  /**
   * {@inheritdoc}
   */
113 114
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
115 116
    // Store whether or not the site is already multilingual so that we can
    // rebuild services if necessary during
117
    // \Drupal\language\Entity\ConfigurableLanguage::postSave().
118
    $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
119 120 121 122 123 124
    // 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';
  }

125 126 127 128 129 130 131 132 133 134 135 136
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

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

137 138
    // Update URL Prefixes for all languages after the
    // LanguageManagerInterface::getLanguages() cache is flushed.
139 140 141 142 143 144 145
    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();
    }
146 147 148 149
    if (!$update) {
      // Install any available language configuration overrides for the language.
      \Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id());
    }
150 151
  }

152 153 154
  /**
   * {@inheritdoc}
   *
155 156 157
   * @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.
158
   */
159
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
160
    $default_langcode = static::getDefaultLangcode();
161
    foreach ($entities as $entity) {
162
      if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
163 164 165 166
        throw new DeleteDefaultLanguageException('Can not delete the default language');
      }
    }
  }
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  /**
   * {@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();
    }
  }

185 186 187 188 189 190 191 192 193 194 195
  /**
   * Gets the default langcode.
   *
   * @return string
   *   The current default langcode.
   */
  protected static function getDefaultLangcode() {
    $language = \Drupal::service('language.default')->get();
    return $language->getId();
  }

196 197 198 199 200 201 202
  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->label();
  }

203 204 205 206 207 208 209 210 211
  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->label = $name;

    return $this;
  }

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->id();
  }

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

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

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  /**
   * 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,
      ));
    }
  }

263
}