ConfigurableLanguage.php 6.8 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
   *
   * @var bool
   */
  protected $preSaveMultilingual;

  /**
   * Sets the default flag on the language entity.
   *
   * @param bool $default
   */
  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;
  }

133 134 135
  /**
   * {@inheritdoc}
   */
136 137
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
138 139
    // Store whether or not the site is already multilingual so that we can
    // rebuild services if necessary during
140
    // \Drupal\language\Entity\ConfigurableLanguage::postSave().
141
    $this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
142 143 144 145 146 147
    // 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';
  }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  /**
   * {@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();
    }
  }

  /**
182
   * Converts the ConfigurableLanguage entity to a Core Language value object.
183 184 185 186
   *
   * @todo fix return type hint after https://drupal.org/node/2246665 and
   *   https://drupal.org/node/2246679.
   *
187
   * @return \Drupal\Core\Language\LanguageInterface
188 189 190 191 192 193 194 195 196 197 198 199 200
   *   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,
    ));
  }

201 202 203 204 205
  /**
   * {@inheritdoc}
   *
   * @throws \RuntimeException
   */
206
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
207
    $default_langcode = static::getDefaultLangcode();
208
    foreach ($entities as $entity) {
209
      if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
210 211 212 213
        throw new DeleteDefaultLanguageException('Can not delete the default language');
      }
    }
  }
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  /**
   * {@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();
  }

238
}