annotate core/modules/language/src/Entity/ConfigurableLanguage.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\language\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\Entity\ConfigEntityBase;
Chris@0 6 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 7 use Drupal\Core\Language\LanguageManager;
Chris@0 8 use Drupal\language\ConfigurableLanguageManager;
Chris@0 9 use Drupal\language\ConfigurableLanguageManagerInterface;
Chris@0 10 use Drupal\language\Exception\DeleteDefaultLanguageException;
Chris@0 11 use Drupal\language\ConfigurableLanguageInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Defines the ConfigurableLanguage entity.
Chris@0 15 *
Chris@0 16 * @ConfigEntityType(
Chris@0 17 * id = "configurable_language",
Chris@0 18 * label = @Translation("Language"),
Chris@17 19 * label_collection = @Translation("Languages"),
Chris@17 20 * label_singular = @Translation("language"),
Chris@17 21 * label_plural = @Translation("languages"),
Chris@17 22 * label_count = @PluralTranslation(
Chris@17 23 * singular = "@count language",
Chris@17 24 * plural = "@count languages",
Chris@17 25 * ),
Chris@0 26 * handlers = {
Chris@0 27 * "list_builder" = "Drupal\language\LanguageListBuilder",
Chris@0 28 * "access" = "Drupal\language\LanguageAccessControlHandler",
Chris@0 29 * "form" = {
Chris@0 30 * "add" = "Drupal\language\Form\LanguageAddForm",
Chris@0 31 * "edit" = "Drupal\language\Form\LanguageEditForm",
Chris@0 32 * "delete" = "Drupal\language\Form\LanguageDeleteForm"
Chris@0 33 * }
Chris@0 34 * },
Chris@0 35 * admin_permission = "administer languages",
Chris@0 36 * config_prefix = "entity",
Chris@0 37 * entity_keys = {
Chris@0 38 * "id" = "id",
Chris@0 39 * "label" = "label",
Chris@0 40 * "weight" = "weight"
Chris@0 41 * },
Chris@0 42 * links = {
Chris@0 43 * "delete-form" = "/admin/config/regional/language/delete/{configurable_language}",
Chris@0 44 * "edit-form" = "/admin/config/regional/language/edit/{configurable_language}",
Chris@0 45 * "collection" = "/admin/config/regional/language",
Chris@0 46 * }
Chris@0 47 * )
Chris@0 48 */
Chris@0 49 class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLanguageInterface {
Chris@0 50
Chris@0 51 /**
Chris@0 52 * The language ID (machine name).
Chris@0 53 *
Chris@0 54 * @var string
Chris@0 55 */
Chris@0 56 protected $id;
Chris@0 57
Chris@0 58 /**
Chris@0 59 * The human-readable label for the language.
Chris@0 60 *
Chris@0 61 * @var string
Chris@0 62 */
Chris@0 63 protected $label;
Chris@0 64
Chris@0 65 /**
Chris@0 66 * The direction of language, either DIRECTION_LTR or DIRECTION_RTL.
Chris@0 67 *
Chris@0 68 * @var int
Chris@0 69 */
Chris@0 70 protected $direction = self::DIRECTION_LTR;
Chris@0 71
Chris@0 72 /**
Chris@0 73 * The weight of the language, used in lists of languages.
Chris@0 74 *
Chris@0 75 * @var int
Chris@0 76 */
Chris@0 77 protected $weight = 0;
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Locked languages cannot be edited.
Chris@0 81 *
Chris@0 82 * @var bool
Chris@0 83 */
Chris@0 84 protected $locked = FALSE;
Chris@0 85
Chris@0 86 /**
Chris@0 87 * Used during saving to detect when the site becomes multilingual.
Chris@0 88 *
Chris@0 89 * This property is not saved to the language entity, but is needed for
Chris@0 90 * detecting when to rebuild the services.
Chris@0 91 *
Chris@0 92 * @see \Drupal\language\Entity\ConfigurableLanguage::preSave()
Chris@0 93 * @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
Chris@0 94 *
Chris@0 95 * @var bool
Chris@0 96 */
Chris@0 97 protected $preSaveMultilingual;
Chris@0 98
Chris@0 99 /**
Chris@0 100 * {@inheritdoc}
Chris@0 101 */
Chris@0 102 public function isDefault() {
Chris@0 103 return static::getDefaultLangcode() == $this->id();
Chris@0 104 }
Chris@0 105
Chris@0 106 /**
Chris@0 107 * {@inheritdoc}
Chris@0 108 */
Chris@0 109 public function isLocked() {
Chris@0 110 return (bool) $this->locked;
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * {@inheritdoc}
Chris@0 115 */
Chris@0 116 public function preSave(EntityStorageInterface $storage) {
Chris@0 117 parent::preSave($storage);
Chris@0 118 // Store whether or not the site is already multilingual so that we can
Chris@0 119 // rebuild services if necessary during
Chris@0 120 // \Drupal\language\Entity\ConfigurableLanguage::postSave().
Chris@0 121 $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * {@inheritdoc}
Chris@0 126 */
Chris@0 127 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
Chris@0 128 parent::postSave($storage, $update);
Chris@0 129
Chris@0 130 $language_manager = \Drupal::languageManager();
Chris@0 131 $language_manager->reset();
Chris@0 132 if (!$this->isLocked() && $language_manager instanceof ConfigurableLanguageManagerInterface && !$this->isSyncing()) {
Chris@0 133 $language_manager->updateLockedLanguageWeights();
Chris@0 134 }
Chris@0 135
Chris@0 136 // Update URL Prefixes for all languages after the
Chris@0 137 // LanguageManagerInterface::getLanguages() cache is flushed.
Chris@0 138 language_negotiation_url_prefixes_update();
Chris@0 139
Chris@0 140 // If after adding this language the site will become multilingual, we need
Chris@0 141 // to rebuild language services.
Chris@0 142 if (!$this->preSaveMultilingual && !$update && $language_manager instanceof ConfigurableLanguageManagerInterface) {
Chris@0 143 $language_manager::rebuildServices();
Chris@0 144 }
Chris@0 145 if (!$update) {
Chris@0 146 // Install any available language configuration overrides for the language.
Chris@0 147 \Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id());
Chris@0 148 }
Chris@17 149
Chris@17 150 if (!$this->isLocked() && !$update) {
Chris@17 151 // Add language to the list of language domains.
Chris@17 152 $config = \Drupal::configFactory()->getEditable('language.negotiation');
Chris@17 153 $domains = $config->get('url.domains');
Chris@17 154 $domains[$this->id()] = '';
Chris@17 155 $config->set('url.domains', $domains)->save();
Chris@17 156 }
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * {@inheritdoc}
Chris@0 161 *
Chris@0 162 * @throws \DeleteDefaultLanguageException
Chris@0 163 * Exception thrown if we're trying to delete the default language entity.
Chris@0 164 * This is not allowed as a site must have a default language.
Chris@0 165 */
Chris@0 166 public static function preDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 167 $default_langcode = static::getDefaultLangcode();
Chris@0 168 foreach ($entities as $entity) {
Chris@0 169 if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
Chris@0 170 throw new DeleteDefaultLanguageException('Can not delete the default language');
Chris@0 171 }
Chris@0 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * {@inheritdoc}
Chris@0 177 */
Chris@0 178 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 179 parent::postDelete($storage, $entities);
Chris@0 180 $language_manager = \Drupal::languageManager();
Chris@0 181 $language_manager->reset();
Chris@0 182 $entity = reset($entities);
Chris@0 183 if ($language_manager instanceof ConfigurableLanguageManagerInterface && !$entity->isUninstalling() && !$entity->isSyncing()) {
Chris@0 184 $language_manager->updateLockedLanguageWeights();
Chris@0 185 }
Chris@0 186 // If after deleting this language the site will become monolingual, we need
Chris@0 187 // to rebuild language services.
Chris@0 188 if (!\Drupal::languageManager()->isMultilingual()) {
Chris@0 189 ConfigurableLanguageManager::rebuildServices();
Chris@0 190 }
Chris@17 191
Chris@17 192 // Remove language from language prefix and domain list.
Chris@17 193 $config = \Drupal::configFactory()->getEditable('language.negotiation');
Chris@17 194 $config->clear('url.prefixes.' . $entity->id());
Chris@17 195 $config->clear('url.domains.' . $entity->id());
Chris@17 196 $config->save();
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Gets the default langcode.
Chris@0 201 *
Chris@0 202 * @return string
Chris@0 203 * The current default langcode.
Chris@0 204 */
Chris@0 205 protected static function getDefaultLangcode() {
Chris@0 206 $language = \Drupal::service('language.default')->get();
Chris@0 207 return $language->getId();
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * {@inheritdoc}
Chris@0 212 */
Chris@0 213 public function getName() {
Chris@0 214 return $this->label();
Chris@0 215 }
Chris@0 216
Chris@0 217 /**
Chris@0 218 * {@inheritdoc}
Chris@0 219 */
Chris@0 220 public function setName($name) {
Chris@0 221 $this->label = $name;
Chris@0 222
Chris@0 223 return $this;
Chris@0 224 }
Chris@0 225
Chris@0 226 /**
Chris@0 227 * {@inheritdoc}
Chris@0 228 */
Chris@0 229 public function getId() {
Chris@0 230 return $this->id();
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * {@inheritdoc}
Chris@0 235 */
Chris@0 236 public function getDirection() {
Chris@0 237 return $this->direction;
Chris@0 238 }
Chris@0 239
Chris@0 240 /**
Chris@0 241 * {@inheritdoc}
Chris@0 242 */
Chris@0 243 public function getWeight() {
Chris@0 244 return $this->weight;
Chris@0 245 }
Chris@0 246
Chris@0 247 /**
Chris@0 248 * {@inheritdoc}
Chris@0 249 */
Chris@0 250 public function setWeight($weight) {
Chris@0 251 $this->weight = $weight;
Chris@0 252 return $this;
Chris@0 253 }
Chris@0 254
Chris@0 255 /**
Chris@0 256 * Creates a configurable language object from a langcode.
Chris@0 257 *
Chris@0 258 * @param string $langcode
Chris@0 259 * The language code to use to create the object.
Chris@0 260 *
Chris@0 261 * @return $this
Chris@0 262 *
Chris@0 263 * @see \Drupal\Core\Language\LanguageManager::getStandardLanguageList()
Chris@0 264 */
Chris@0 265 public static function createFromLangcode($langcode) {
Chris@0 266 $standard_languages = LanguageManager::getStandardLanguageList();
Chris@0 267 if (!isset($standard_languages[$langcode])) {
Chris@0 268 // Drupal does not know about this language, so we set its values with the
Chris@0 269 // best guess. The user will be able to edit afterwards.
Chris@0 270 return static::create([
Chris@0 271 'id' => $langcode,
Chris@0 272 'label' => $langcode,
Chris@0 273 ]);
Chris@0 274 }
Chris@0 275 else {
Chris@0 276 // A known predefined language, details will be filled in properly.
Chris@0 277 return static::create([
Chris@0 278 'id' => $langcode,
Chris@0 279 'label' => $standard_languages[$langcode][0],
Chris@0 280 'direction' => isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : static::DIRECTION_LTR,
Chris@0 281 ]);
Chris@0 282 }
Chris@0 283 }
Chris@0 284
Chris@0 285 }