Chris@0: id(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isLocked() { Chris@0: return (bool) $this->locked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function preSave(EntityStorageInterface $storage) { Chris@0: parent::preSave($storage); Chris@0: // Store whether or not the site is already multilingual so that we can Chris@0: // rebuild services if necessary during Chris@0: // \Drupal\language\Entity\ConfigurableLanguage::postSave(). Chris@0: $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function postSave(EntityStorageInterface $storage, $update = TRUE) { Chris@0: parent::postSave($storage, $update); Chris@0: Chris@0: $language_manager = \Drupal::languageManager(); Chris@0: $language_manager->reset(); Chris@0: if (!$this->isLocked() && $language_manager instanceof ConfigurableLanguageManagerInterface && !$this->isSyncing()) { Chris@0: $language_manager->updateLockedLanguageWeights(); Chris@0: } Chris@0: Chris@0: // Update URL Prefixes for all languages after the Chris@0: // LanguageManagerInterface::getLanguages() cache is flushed. Chris@0: language_negotiation_url_prefixes_update(); Chris@0: Chris@0: // If after adding this language the site will become multilingual, we need Chris@0: // to rebuild language services. Chris@0: if (!$this->preSaveMultilingual && !$update && $language_manager instanceof ConfigurableLanguageManagerInterface) { Chris@0: $language_manager::rebuildServices(); Chris@0: } Chris@0: if (!$update) { Chris@0: // Install any available language configuration overrides for the language. Chris@0: \Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id()); Chris@0: } Chris@17: Chris@17: if (!$this->isLocked() && !$update) { Chris@17: // Add language to the list of language domains. Chris@17: $config = \Drupal::configFactory()->getEditable('language.negotiation'); Chris@17: $domains = $config->get('url.domains'); Chris@17: $domains[$this->id()] = ''; Chris@18: $config->set('url.domains', $domains)->save(TRUE); Chris@17: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @throws \DeleteDefaultLanguageException Chris@0: * Exception thrown if we're trying to delete the default language entity. Chris@0: * This is not allowed as a site must have a default language. Chris@0: */ Chris@0: public static function preDelete(EntityStorageInterface $storage, array $entities) { Chris@0: $default_langcode = static::getDefaultLangcode(); Chris@0: foreach ($entities as $entity) { Chris@0: if ($entity->id() == $default_langcode && !$entity->isUninstalling()) { Chris@0: throw new DeleteDefaultLanguageException('Can not delete the default language'); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function postDelete(EntityStorageInterface $storage, array $entities) { Chris@0: parent::postDelete($storage, $entities); Chris@0: $language_manager = \Drupal::languageManager(); Chris@0: $language_manager->reset(); Chris@0: $entity = reset($entities); Chris@0: if ($language_manager instanceof ConfigurableLanguageManagerInterface && !$entity->isUninstalling() && !$entity->isSyncing()) { Chris@0: $language_manager->updateLockedLanguageWeights(); Chris@0: } Chris@0: // If after deleting this language the site will become monolingual, we need Chris@0: // to rebuild language services. Chris@0: if (!\Drupal::languageManager()->isMultilingual()) { Chris@0: ConfigurableLanguageManager::rebuildServices(); Chris@0: } Chris@17: Chris@17: // Remove language from language prefix and domain list. Chris@17: $config = \Drupal::configFactory()->getEditable('language.negotiation'); Chris@17: $config->clear('url.prefixes.' . $entity->id()); Chris@17: $config->clear('url.domains.' . $entity->id()); Chris@18: $config->save(TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the default langcode. Chris@0: * Chris@0: * @return string Chris@0: * The current default langcode. Chris@0: */ Chris@0: protected static function getDefaultLangcode() { Chris@0: $language = \Drupal::service('language.default')->get(); Chris@0: return $language->getId(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() { Chris@0: return $this->label(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setName($name) { Chris@0: $this->label = $name; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getId() { Chris@0: return $this->id(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDirection() { Chris@0: return $this->direction; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getWeight() { Chris@0: return $this->weight; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setWeight($weight) { Chris@0: $this->weight = $weight; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a configurable language object from a langcode. Chris@0: * Chris@0: * @param string $langcode Chris@0: * The language code to use to create the object. Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @see \Drupal\Core\Language\LanguageManager::getStandardLanguageList() Chris@0: */ Chris@0: public static function createFromLangcode($langcode) { Chris@0: $standard_languages = LanguageManager::getStandardLanguageList(); Chris@0: if (!isset($standard_languages[$langcode])) { Chris@0: // Drupal does not know about this language, so we set its values with the Chris@0: // best guess. The user will be able to edit afterwards. Chris@0: return static::create([ Chris@0: 'id' => $langcode, Chris@0: 'label' => $langcode, Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: // A known predefined language, details will be filled in properly. Chris@0: return static::create([ Chris@0: 'id' => $langcode, Chris@0: 'label' => $standard_languages[$langcode][0], Chris@0: 'direction' => isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : static::DIRECTION_LTR, Chris@0: ]); Chris@0: } Chris@0: } Chris@0: Chris@0: }