3 namespace Drupal\language\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\language\ContentLanguageSettingsException;
9 use Drupal\language\ContentLanguageSettingsInterface;
12 * Defines the ContentLanguageSettings entity.
15 * id = "language_content_settings",
16 * label = @Translation("Content Language Settings"),
17 * admin_permission = "administer languages",
18 * config_prefix = "content_settings",
22 * list_cache_tags = { "rendered" }
25 class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {
28 * The id. Combination of $target_entity_type_id.$target_bundle.
35 * The entity type ID (machine name).
39 protected $target_entity_type_id;
42 * The bundle (machine name).
46 protected $target_bundle;
49 * The default language code.
53 protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;
56 * Indicates if the language is alterable or not.
60 protected $language_alterable = FALSE;
63 * Constructs a ContentLanguageSettings object.
65 * In most cases, Field entities are created via
66 * FieldConfig::create($values), where $values is the same
67 * parameter as in this constructor.
69 * @param array $values
70 * An array of the referring entity bundle with:
71 * - target_entity_type_id: The entity type.
72 * - target_bundle: The bundle.
73 * Other array elements will be used to set the corresponding properties on
74 * the class; see the class property documentation for details.
76 * @see entity_create()
78 public function __construct(array $values, $entity_type = 'language_content_settings') {
79 if (empty($values['target_entity_type_id'])) {
80 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.');
82 if (empty($values['target_bundle'])) {
83 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.');
85 parent::__construct($values, $entity_type);
91 public function id() {
92 return $this->target_entity_type_id . '.' . $this->target_bundle;
98 public function getTargetEntityTypeId() {
99 return $this->target_entity_type_id;
105 public function getTargetBundle() {
106 return $this->target_bundle;
112 public function setTargetBundle($target_bundle) {
113 $this->target_bundle = $target_bundle;
121 public function setDefaultLangcode($default_langcode) {
122 $this->default_langcode = $default_langcode;
130 public function getDefaultLangcode() {
131 return $this->default_langcode;
137 public function setLanguageAlterable($language_alterable) {
138 $this->language_alterable = $language_alterable;
146 public function isLanguageAlterable() {
147 return $this->language_alterable;
153 public function preSave(EntityStorageInterface $storage) {
154 $this->id = $this->id();
155 parent::preSave($storage);
161 public function isDefaultConfiguration() {
162 return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
166 * Loads a content language config entity based on the entity type and bundle.
168 * @param string $entity_type_id
169 * ID of the entity type.
170 * @param string $bundle
174 * The content language config entity if one exists. Otherwise, returns
177 public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
178 if ($entity_type_id == NULL || $bundle == NULL) {
181 $config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
182 if ($config == NULL) {
183 $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
191 public function calculateDependencies() {
192 parent::calculateDependencies();
194 // Create dependency on the bundle.
195 $entity_type = \Drupal::entityManager()->getDefinition($this->target_entity_type_id);
196 $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle);
197 $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);