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