ContentLanguageSettings.php in Zircon Profile 8
File
core/modules/language/src/Entity/ContentLanguageSettings.php
View source
<?php
namespace Drupal\language\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\ContentLanguageSettingsException;
use Drupal\language\ContentLanguageSettingsInterface;
class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {
protected $id;
protected $target_entity_type_id;
protected $target_bundle;
protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;
protected $language_alterable = FALSE;
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);
}
public function id() {
return $this->target_entity_type_id . '.' . $this->target_bundle;
}
public function getTargetEntityTypeId() {
return $this->target_entity_type_id;
}
public function getTargetBundle() {
return $this->target_bundle;
}
public function setTargetBundle($target_bundle) {
$this->target_bundle = $target_bundle;
return $this;
}
public function setDefaultLangcode($default_langcode) {
$this->default_langcode = $default_langcode;
return $this;
}
public function getDefaultLangcode() {
return $this->default_langcode;
}
public function setLanguageAlterable($language_alterable) {
$this->language_alterable = $language_alterable;
return $this;
}
public function isLanguageAlterable() {
return $this->language_alterable;
}
public function preSave(EntityStorageInterface $storage) {
$this->id = $this
->id();
parent::preSave($storage);
}
public function isDefaultConfiguration() {
return !$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT;
}
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;
}
public function calculateDependencies() {
parent::calculateDependencies();
$entity_type = \Drupal::entityManager()
->getDefinition($this->target_entity_type_id);
$bundle_config_dependency = $entity_type
->getBundleConfigDependency($this->target_bundle);
$this
->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
return $this;
}
}