Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
8 use Drupal\language\ContentLanguageSettingsException;
|
Chris@0
|
9 use Drupal\language\ContentLanguageSettingsInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Defines the ContentLanguageSettings entity.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @ConfigEntityType(
|
Chris@0
|
15 * id = "language_content_settings",
|
Chris@0
|
16 * label = @Translation("Content Language Settings"),
|
Chris@17
|
17 * label_collection = @Translation("Content Language Settings"),
|
Chris@17
|
18 * label_singular = @Translation("content language setting"),
|
Chris@17
|
19 * label_plural = @Translation("content languages settings"),
|
Chris@17
|
20 * label_count = @PluralTranslation(
|
Chris@17
|
21 * singular = "@count content language setting",
|
Chris@17
|
22 * plural = "@count content languages settings",
|
Chris@17
|
23 * ),
|
Chris@0
|
24 * admin_permission = "administer languages",
|
Chris@0
|
25 * config_prefix = "content_settings",
|
Chris@0
|
26 * entity_keys = {
|
Chris@0
|
27 * "id" = "id"
|
Chris@0
|
28 * },
|
Chris@0
|
29 * list_cache_tags = { "rendered" }
|
Chris@0
|
30 * )
|
Chris@0
|
31 */
|
Chris@0
|
32 class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface {
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The id. Combination of $target_entity_type_id.$target_bundle.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $id;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The entity type ID (machine name).
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var string
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $target_entity_type_id;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * The bundle (machine name).
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var string
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $target_bundle;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * The default language code.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var string
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Indicates if the language is alterable or not.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var bool
|
Chris@0
|
66 */
|
Chris@0
|
67 protected $language_alterable = FALSE;
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Constructs a ContentLanguageSettings object.
|
Chris@0
|
71 *
|
Chris@0
|
72 * In most cases, Field entities are created via
|
Chris@0
|
73 * FieldConfig::create($values), where $values is the same
|
Chris@0
|
74 * parameter as in this constructor.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @param array $values
|
Chris@0
|
77 * An array of the referring entity bundle with:
|
Chris@0
|
78 * - target_entity_type_id: The entity type.
|
Chris@0
|
79 * - target_bundle: The bundle.
|
Chris@0
|
80 * Other array elements will be used to set the corresponding properties on
|
Chris@0
|
81 * the class; see the class property documentation for details.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @see entity_create()
|
Chris@0
|
84 */
|
Chris@0
|
85 public function __construct(array $values, $entity_type = 'language_content_settings') {
|
Chris@0
|
86 if (empty($values['target_entity_type_id'])) {
|
Chris@0
|
87 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.');
|
Chris@0
|
88 }
|
Chris@0
|
89 if (empty($values['target_bundle'])) {
|
Chris@0
|
90 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.');
|
Chris@0
|
91 }
|
Chris@0
|
92 parent::__construct($values, $entity_type);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * {@inheritdoc}
|
Chris@0
|
97 */
|
Chris@0
|
98 public function id() {
|
Chris@0
|
99 return $this->target_entity_type_id . '.' . $this->target_bundle;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function getTargetEntityTypeId() {
|
Chris@0
|
106 return $this->target_entity_type_id;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * {@inheritdoc}
|
Chris@0
|
111 */
|
Chris@0
|
112 public function getTargetBundle() {
|
Chris@0
|
113 return $this->target_bundle;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function setTargetBundle($target_bundle) {
|
Chris@0
|
120 $this->target_bundle = $target_bundle;
|
Chris@0
|
121
|
Chris@0
|
122 return $this;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function setDefaultLangcode($default_langcode) {
|
Chris@0
|
129 $this->default_langcode = $default_langcode;
|
Chris@0
|
130
|
Chris@0
|
131 return $this;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * {@inheritdoc}
|
Chris@0
|
136 */
|
Chris@0
|
137 public function getDefaultLangcode() {
|
Chris@0
|
138 return $this->default_langcode;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * {@inheritdoc}
|
Chris@0
|
143 */
|
Chris@0
|
144 public function setLanguageAlterable($language_alterable) {
|
Chris@0
|
145 $this->language_alterable = $language_alterable;
|
Chris@0
|
146
|
Chris@0
|
147 return $this;
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * {@inheritdoc}
|
Chris@0
|
152 */
|
Chris@0
|
153 public function isLanguageAlterable() {
|
Chris@0
|
154 return $this->language_alterable;
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * {@inheritdoc}
|
Chris@0
|
159 */
|
Chris@0
|
160 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
161 $this->id = $this->id();
|
Chris@0
|
162 parent::preSave($storage);
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * {@inheritdoc}
|
Chris@0
|
167 */
|
Chris@0
|
168 public function isDefaultConfiguration() {
|
Chris@0
|
169 return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Loads a content language config entity based on the entity type and bundle.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @param string $entity_type_id
|
Chris@0
|
176 * ID of the entity type.
|
Chris@0
|
177 * @param string $bundle
|
Chris@0
|
178 * Bundle name.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @return $this
|
Chris@0
|
181 * The content language config entity if one exists. Otherwise, returns
|
Chris@0
|
182 * default values.
|
Chris@0
|
183 */
|
Chris@0
|
184 public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
|
Chris@0
|
185 if ($entity_type_id == NULL || $bundle == NULL) {
|
Chris@0
|
186 return NULL;
|
Chris@0
|
187 }
|
Chris@0
|
188 $config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
|
Chris@0
|
189 if ($config == NULL) {
|
Chris@0
|
190 $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
|
Chris@0
|
191 }
|
Chris@0
|
192 return $config;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * {@inheritdoc}
|
Chris@0
|
197 */
|
Chris@0
|
198 public function calculateDependencies() {
|
Chris@0
|
199 parent::calculateDependencies();
|
Chris@0
|
200
|
Chris@0
|
201 // Create dependency on the bundle.
|
Chris@0
|
202 $entity_type = \Drupal::entityManager()->getDefinition($this->target_entity_type_id);
|
Chris@0
|
203 $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle);
|
Chris@0
|
204 $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
|
Chris@0
|
205
|
Chris@0
|
206 return $this;
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 }
|