Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
269
core/modules/language/src/Entity/ConfigurableLanguage.php
Normal file
269
core/modules/language/src/Entity/ConfigurableLanguage.php
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Entity\ConfigurableLanguage.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Language\LanguageManager;
|
||||
use Drupal\language\ConfigurableLanguageManager;
|
||||
use Drupal\language\ConfigurableLanguageManagerInterface;
|
||||
use Drupal\language\Exception\DeleteDefaultLanguageException;
|
||||
use Drupal\language\ConfigurableLanguageInterface;
|
||||
|
||||
/**
|
||||
* Defines the ConfigurableLanguage entity.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "configurable_language",
|
||||
* label = @Translation("Language"),
|
||||
* handlers = {
|
||||
* "list_builder" = "Drupal\language\LanguageListBuilder",
|
||||
* "access" = "Drupal\language\LanguageAccessControlHandler",
|
||||
* "form" = {
|
||||
* "add" = "Drupal\language\Form\LanguageAddForm",
|
||||
* "edit" = "Drupal\language\Form\LanguageEditForm",
|
||||
* "delete" = "Drupal\language\Form\LanguageDeleteForm"
|
||||
* }
|
||||
* },
|
||||
* admin_permission = "administer languages",
|
||||
* config_prefix = "entity",
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "label" = "label",
|
||||
* "weight" = "weight"
|
||||
* },
|
||||
* links = {
|
||||
* "delete-form" = "/admin/config/regional/language/delete/{configurable_language}",
|
||||
* "edit-form" = "/admin/config/regional/language/edit/{configurable_language}",
|
||||
* "collection" = "/admin/config/regional/language",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLanguageInterface {
|
||||
|
||||
/**
|
||||
* The language ID (machine name).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The human-readable label for the language.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* The direction of language, either DIRECTION_LTR or DIRECTION_RTL.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $direction = self::DIRECTION_LTR;
|
||||
|
||||
/**
|
||||
* The weight of the language, used in lists of languages.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* Locked languages cannot be edited.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $locked = FALSE;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @see \Drupal\language\Entity\ConfigurableLanguage::preSave()
|
||||
* @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $preSaveMultilingual;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isDefault() {
|
||||
return static::getDefaultLangcode() == $this->id();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLocked() {
|
||||
return (bool) $this->locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function preSave(EntityStorageInterface $storage) {
|
||||
parent::preSave($storage);
|
||||
// Store whether or not the site is already multilingual so that we can
|
||||
// rebuild services if necessary during
|
||||
// \Drupal\language\Entity\ConfigurableLanguage::postSave().
|
||||
$this->preSaveMultilingual = \Drupal::languageManager()->isMultilingual();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
$language_manager = \Drupal::languageManager();
|
||||
$language_manager->reset();
|
||||
if (!$this->isLocked() && $language_manager instanceof ConfigurableLanguageManagerInterface && !$this->isSyncing()) {
|
||||
$language_manager->updateLockedLanguageWeights();
|
||||
}
|
||||
|
||||
// Update URL Prefixes for all languages after 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();
|
||||
}
|
||||
if (!$update) {
|
||||
// Install any available language configuration overrides for the language.
|
||||
\Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
||||
$default_langcode = static::getDefaultLangcode();
|
||||
foreach ($entities as $entity) {
|
||||
if ($entity->id() == $default_langcode && !$entity->isUninstalling()) {
|
||||
throw new DeleteDefaultLanguageException('Can not delete the default language');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
||||
parent::postDelete($storage, $entities);
|
||||
$language_manager = \Drupal::languageManager();
|
||||
$language_manager->reset();
|
||||
$entity = reset($entities);
|
||||
if ($language_manager instanceof ConfigurableLanguageManagerInterface && !$entity->isUninstalling() && !$entity->isSyncing()) {
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default langcode.
|
||||
*
|
||||
* @return string
|
||||
* The current default langcode.
|
||||
*/
|
||||
protected static function getDefaultLangcode() {
|
||||
$language = \Drupal::service('language.default')->get();
|
||||
return $language->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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 getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setWeight($weight) {
|
||||
$this->weight = $weight;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
210
core/modules/language/src/Entity/ContentLanguageSettings.php
Normal file
210
core/modules/language/src/Entity/ContentLanguageSettings.php
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Entity\ContentLanguageSettings.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Entity;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\language\ContentLanguageSettingsException;
|
||||
use Drupal\language\ContentLanguageSettingsInterface;
|
||||
|
||||
/**
|
||||
* Defines the ContentLanguageSettings entity.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "language_content_settings",
|
||||
* label = @Translation("Content Language Settings"),
|
||||
* admin_permission = "administer languages",
|
||||
* config_prefix = "content_settings",
|
||||
* entity_keys = {
|
||||
* "id" = "id"
|
||||
* },
|
||||
* )
|
||||
*/
|
||||
class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {
|
||||
|
||||
/**
|
||||
* The id. Combination of $target_entity_type_id.$target_bundle.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The entity type ID (machine name).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $target_entity_type_id;
|
||||
|
||||
/**
|
||||
* The bundle (machine name).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $target_bundle;
|
||||
|
||||
/**
|
||||
* The default language code.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;
|
||||
|
||||
/**
|
||||
* Indicates if the language is alterable or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $language_alterable = FALSE;
|
||||
|
||||
/**
|
||||
* Constructs a ContentLanguageSettings object.
|
||||
*
|
||||
* In most cases, Field entities are created via
|
||||
* entity_create('field_config', $values), where $values is the same
|
||||
* parameter as in this constructor.
|
||||
*
|
||||
* @param array $values
|
||||
* An array of the referring entity bundle with:
|
||||
* - target_entity_type_id: The entity type.
|
||||
* - target_bundle: The bundle.
|
||||
* Other array elements will be used to set the corresponding properties on
|
||||
* the class; see the class property documentation for details.
|
||||
*
|
||||
* @see entity_create()
|
||||
*/
|
||||
public function __construct(array $values, $entity_type = 'language_content_settings') {
|
||||
if (empty($values['target_entity_type_id'])) {
|
||||
throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.');
|
||||
}
|
||||
if (empty($values['target_bundle'])) {
|
||||
throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.');
|
||||
}
|
||||
parent::__construct($values, $entity_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function id() {
|
||||
return $this->target_entity_type_id . '.' . $this->target_bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTargetEntityTypeId() {
|
||||
return $this->target_entity_type_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTargetBundle() {
|
||||
return $this->target_bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setTargetBundle($target_bundle) {
|
||||
$this->target_bundle = $target_bundle;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefaultLangcode($default_langcode) {
|
||||
$this->default_langcode = $default_langcode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultLangcode() {
|
||||
return $this->default_langcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setLanguageAlterable($language_alterable) {
|
||||
$this->language_alterable = $language_alterable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLanguageAlterable() {
|
||||
return $this->language_alterable;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function preSave(EntityStorageInterface $storage) {
|
||||
$this->id = $this->id();
|
||||
parent::preSave($storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isDefaultConfiguration() {
|
||||
return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a content language config entity based on the entity type and bundle.
|
||||
*
|
||||
* @param string $entity_type_id
|
||||
* ID of the entity type.
|
||||
* @param string $bundle
|
||||
* Bundle name.
|
||||
*
|
||||
* @return $this
|
||||
* The content language config entity if one exists. Otherwise, returns
|
||||
* default values.
|
||||
*/
|
||||
public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
|
||||
if ($entity_type_id == NULL || $bundle == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
$config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
|
||||
if ($config == NULL) {
|
||||
$config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
parent::calculateDependencies();
|
||||
$bundle_entity_type_id = $this->entityManager()->getDefinition($this->target_entity_type_id)->getBundleEntityType();
|
||||
if ($bundle_entity_type_id != 'bundle') {
|
||||
// If the target entity type uses entities to manage its bundles then
|
||||
// depend on the bundle entity.
|
||||
if (!$bundle_entity = $this->entityManager()->getStorage($bundle_entity_type_id)->load($this->target_bundle)) {
|
||||
throw new \LogicException(SafeMarkup::format('Missing bundle entity, entity type %type, entity id %bundle.', array('%type' => $bundle_entity_type_id, '%bundle' => $this->target_bundle)));
|
||||
}
|
||||
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
|
||||
}
|
||||
return $this->dependencies;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue