Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\FieldableEntityStorageInterface;
|
Chris@0
|
7 use Drupal\Core\Field\FieldConfigBase;
|
Chris@0
|
8 use Drupal\Core\Field\FieldException;
|
Chris@0
|
9 use Drupal\field\FieldStorageConfigInterface;
|
Chris@0
|
10 use Drupal\field\FieldConfigInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Defines the Field entity.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @ConfigEntityType(
|
Chris@0
|
16 * id = "field_config",
|
Chris@0
|
17 * label = @Translation("Field"),
|
Chris@17
|
18 * label_collection = @Translation("Fields"),
|
Chris@17
|
19 * label_singular = @Translation("field"),
|
Chris@17
|
20 * label_plural = @Translation("fields"),
|
Chris@17
|
21 * label_count = @PluralTranslation(
|
Chris@17
|
22 * singular = "@count field",
|
Chris@17
|
23 * plural = "@count fields",
|
Chris@17
|
24 * ),
|
Chris@0
|
25 * handlers = {
|
Chris@0
|
26 * "access" = "Drupal\field\FieldConfigAccessControlHandler",
|
Chris@0
|
27 * "storage" = "Drupal\field\FieldConfigStorage"
|
Chris@0
|
28 * },
|
Chris@0
|
29 * config_prefix = "field",
|
Chris@0
|
30 * entity_keys = {
|
Chris@0
|
31 * "id" = "id",
|
Chris@0
|
32 * "label" = "label"
|
Chris@0
|
33 * },
|
Chris@0
|
34 * config_export = {
|
Chris@0
|
35 * "id",
|
Chris@0
|
36 * "field_name",
|
Chris@0
|
37 * "entity_type",
|
Chris@0
|
38 * "bundle",
|
Chris@0
|
39 * "label",
|
Chris@0
|
40 * "description",
|
Chris@0
|
41 * "required",
|
Chris@0
|
42 * "translatable",
|
Chris@0
|
43 * "default_value",
|
Chris@0
|
44 * "default_value_callback",
|
Chris@0
|
45 * "settings",
|
Chris@0
|
46 * "field_type",
|
Chris@0
|
47 * }
|
Chris@0
|
48 * )
|
Chris@0
|
49 */
|
Chris@0
|
50 class FieldConfig extends FieldConfigBase implements FieldConfigInterface {
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Flag indicating whether the field is deleted.
|
Chris@0
|
54 *
|
Chris@0
|
55 * The delete() method marks the field as "deleted" and removes the
|
Chris@0
|
56 * corresponding entry from the config storage, but keeps its definition in
|
Chris@0
|
57 * the state storage while field data is purged by a separate
|
Chris@0
|
58 * garbage-collection process.
|
Chris@0
|
59 *
|
Chris@0
|
60 * Deleted fields stay out of the regular entity lifecycle (notably, their
|
Chris@0
|
61 * values are not populated in loaded entities, and are not saved back).
|
Chris@0
|
62 *
|
Chris@0
|
63 * @var bool
|
Chris@0
|
64 */
|
Chris@0
|
65 protected $deleted = FALSE;
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * The associated FieldStorageConfig entity.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @var \Drupal\field\Entity\FieldStorageConfig
|
Chris@0
|
71 */
|
Chris@0
|
72 protected $fieldStorage;
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Constructs a FieldConfig object.
|
Chris@0
|
76 *
|
Chris@0
|
77 * In most cases, Field entities are created via
|
Chris@0
|
78 * FieldConfig::create($values), where $values is the same
|
Chris@0
|
79 * parameter as in this constructor.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @param array $values
|
Chris@0
|
82 * An array of field properties, keyed by property name. The
|
Chris@0
|
83 * storage associated with the field can be specified either with:
|
Chris@0
|
84 * - field_storage: the FieldStorageConfigInterface object,
|
Chris@0
|
85 * or by referring to an existing field storage in the current configuration
|
Chris@0
|
86 * with:
|
Chris@0
|
87 * - field_name: The field name.
|
Chris@0
|
88 * - entity_type: The entity type.
|
Chris@0
|
89 * Additionally, a 'bundle' property is required to indicate the entity
|
Chris@0
|
90 * bundle to which the field is attached to. Other array elements will be
|
Chris@0
|
91 * used to set the corresponding properties on the class; see the class
|
Chris@0
|
92 * property documentation for details.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @see entity_create()
|
Chris@0
|
95 */
|
Chris@0
|
96 public function __construct(array $values, $entity_type = 'field_config') {
|
Chris@0
|
97 // Allow either an injected FieldStorageConfig object, or a field_name and
|
Chris@0
|
98 // entity_type.
|
Chris@0
|
99 if (isset($values['field_storage'])) {
|
Chris@0
|
100 if (!$values['field_storage'] instanceof FieldStorageConfigInterface) {
|
Chris@0
|
101 throw new FieldException('Attempt to create a configurable field for a non-configurable field storage.');
|
Chris@0
|
102 }
|
Chris@0
|
103 $field_storage = $values['field_storage'];
|
Chris@0
|
104 $values['field_name'] = $field_storage->getName();
|
Chris@0
|
105 $values['entity_type'] = $field_storage->getTargetEntityTypeId();
|
Chris@0
|
106 // The internal property is fieldStorage, not field_storage.
|
Chris@0
|
107 unset($values['field_storage']);
|
Chris@0
|
108 $values['fieldStorage'] = $field_storage;
|
Chris@0
|
109 }
|
Chris@0
|
110 else {
|
Chris@0
|
111 if (empty($values['field_name'])) {
|
Chris@0
|
112 throw new FieldException('Attempt to create a field without a field_name.');
|
Chris@0
|
113 }
|
Chris@0
|
114 if (empty($values['entity_type'])) {
|
Chris@0
|
115 throw new FieldException("Attempt to create a field '{$values['field_name']}' without an entity_type.");
|
Chris@0
|
116 }
|
Chris@0
|
117 }
|
Chris@0
|
118 // 'bundle' is required in either case.
|
Chris@0
|
119 if (empty($values['bundle'])) {
|
Chris@0
|
120 throw new FieldException("Attempt to create a field '{$values['field_name']}' without a bundle.");
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 parent::__construct($values, $entity_type);
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function postCreate(EntityStorageInterface $storage) {
|
Chris@0
|
130 parent::postCreate($storage);
|
Chris@0
|
131
|
Chris@0
|
132 // Validate that we have a valid storage for this field. This throws an
|
Chris@0
|
133 // exception if the storage is invalid.
|
Chris@0
|
134 $this->getFieldStorageDefinition();
|
Chris@0
|
135
|
Chris@0
|
136 // 'Label' defaults to the field name (mostly useful for fields created in
|
Chris@0
|
137 // tests).
|
Chris@0
|
138 if (empty($this->label)) {
|
Chris@0
|
139 $this->label = $this->getName();
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Overrides \Drupal\Core\Entity\Entity::preSave().
|
Chris@0
|
145 *
|
Chris@0
|
146 * @throws \Drupal\Core\Field\FieldException
|
Chris@0
|
147 * If the field definition is invalid.
|
Chris@0
|
148 * @throws \Drupal\Core\Entity\EntityStorageException
|
Chris@0
|
149 * In case of failures at the configuration storage level.
|
Chris@0
|
150 */
|
Chris@0
|
151 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
152 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
153 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
|
Chris@0
|
154
|
Chris@0
|
155 $storage_definition = $this->getFieldStorageDefinition();
|
Chris@0
|
156
|
Chris@0
|
157 // Filter out unknown settings and make sure all settings are present, so
|
Chris@0
|
158 // that a complete field definition is passed to the various hooks and
|
Chris@0
|
159 // written to config.
|
Chris@0
|
160 $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType());
|
Chris@0
|
161 $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
|
Chris@0
|
162
|
Chris@0
|
163 if ($this->isNew()) {
|
Chris@0
|
164 // Notify the entity storage.
|
Chris@18
|
165 \Drupal::service('field_definition.listener')->onFieldDefinitionCreate($this);
|
Chris@0
|
166 }
|
Chris@0
|
167 else {
|
Chris@0
|
168 // Some updates are always disallowed.
|
Chris@0
|
169 if ($this->entity_type != $this->original->entity_type) {
|
Chris@0
|
170 throw new FieldException("Cannot change an existing field's entity_type.");
|
Chris@0
|
171 }
|
Chris@0
|
172 if ($this->bundle != $this->original->bundle) {
|
Chris@0
|
173 throw new FieldException("Cannot change an existing field's bundle.");
|
Chris@0
|
174 }
|
Chris@0
|
175 if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
|
Chris@0
|
176 throw new FieldException("Cannot change an existing field's storage.");
|
Chris@0
|
177 }
|
Chris@0
|
178 // Notify the entity storage.
|
Chris@18
|
179 \Drupal::service('field_definition.listener')->onFieldDefinitionUpdate($this, $this->original);
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 parent::preSave($storage);
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * {@inheritdoc}
|
Chris@0
|
187 */
|
Chris@0
|
188 public function calculateDependencies() {
|
Chris@0
|
189 parent::calculateDependencies();
|
Chris@0
|
190 // Mark the field_storage_config as a dependency.
|
Chris@0
|
191 $this->addDependency('config', $this->getFieldStorageDefinition()->getConfigDependencyName());
|
Chris@0
|
192 return $this;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * {@inheritdoc}
|
Chris@0
|
197 */
|
Chris@0
|
198 public static function preDelete(EntityStorageInterface $storage, array $fields) {
|
Chris@14
|
199 /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
|
Chris@14
|
200 $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
|
Chris@0
|
201 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@0
|
202
|
Chris@0
|
203 parent::preDelete($storage, $fields);
|
Chris@0
|
204
|
Chris@14
|
205 // Keep the field definitions in the deleted fields repository so we can use
|
Chris@14
|
206 // them later during field_purge_batch().
|
Chris@0
|
207 /** @var \Drupal\field\FieldConfigInterface $field */
|
Chris@0
|
208 foreach ($fields as $field) {
|
Chris@0
|
209 // Only mark a field for purging if there is data. Otherwise, just remove
|
Chris@0
|
210 // it.
|
Chris@0
|
211 $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
|
Chris@0
|
212 if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
|
Chris@14
|
213 $field = clone $field;
|
Chris@14
|
214 $field->deleted = TRUE;
|
Chris@14
|
215 $field->fieldStorage = NULL;
|
Chris@14
|
216 $deleted_fields_repository->addFieldDefinition($field);
|
Chris@0
|
217 }
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * {@inheritdoc}
|
Chris@0
|
223 */
|
Chris@0
|
224 public static function postDelete(EntityStorageInterface $storage, array $fields) {
|
Chris@0
|
225 // Clear the cache upfront, to refresh the results of getBundles().
|
Chris@18
|
226 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
|
Chris@0
|
227
|
Chris@0
|
228 // Notify the entity storage.
|
Chris@0
|
229 foreach ($fields as $field) {
|
Chris@0
|
230 if (!$field->deleted) {
|
Chris@18
|
231 \Drupal::service('field_definition.listener')->onFieldDefinitionDelete($field);
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 // If this is part of a configuration synchronization then the following
|
Chris@0
|
236 // configuration updates are not necessary.
|
Chris@0
|
237 $entity = reset($fields);
|
Chris@0
|
238 if ($entity->isSyncing()) {
|
Chris@0
|
239 return;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 // Delete the associated field storages if they are not used anymore and are
|
Chris@0
|
243 // not persistent.
|
Chris@0
|
244 $storages_to_delete = [];
|
Chris@0
|
245 foreach ($fields as $field) {
|
Chris@0
|
246 $storage_definition = $field->getFieldStorageDefinition();
|
Chris@0
|
247 if (!$field->deleted && !$field->isUninstalling() && $storage_definition->isDeletable()) {
|
Chris@0
|
248 // Key by field UUID to avoid deleting the same storage twice.
|
Chris@0
|
249 $storages_to_delete[$storage_definition->uuid()] = $storage_definition;
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252 if ($storages_to_delete) {
|
Chris@0
|
253 \Drupal::entityManager()->getStorage('field_storage_config')->delete($storages_to_delete);
|
Chris@0
|
254 }
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * {@inheritdoc}
|
Chris@0
|
259 */
|
Chris@0
|
260 protected function linkTemplates() {
|
Chris@0
|
261 $link_templates = parent::linkTemplates();
|
Chris@0
|
262 if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
|
Chris@0
|
263 $link_templates["{$this->entity_type}-field-edit-form"] = 'entity.field_config.' . $this->entity_type . '_field_edit_form';
|
Chris@0
|
264 $link_templates["{$this->entity_type}-storage-edit-form"] = 'entity.field_config.' . $this->entity_type . '_storage_edit_form';
|
Chris@0
|
265 $link_templates["{$this->entity_type}-field-delete-form"] = 'entity.field_config.' . $this->entity_type . '_field_delete_form';
|
Chris@0
|
266
|
Chris@0
|
267 if (isset($link_templates['config-translation-overview'])) {
|
Chris@0
|
268 $link_templates["config-translation-overview.{$this->entity_type}"] = "entity.field_config.config_translation_overview.{$this->entity_type}";
|
Chris@0
|
269 }
|
Chris@0
|
270 }
|
Chris@0
|
271 return $link_templates;
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * {@inheritdoc}
|
Chris@0
|
276 */
|
Chris@0
|
277 protected function urlRouteParameters($rel) {
|
Chris@0
|
278 $parameters = parent::urlRouteParameters($rel);
|
Chris@0
|
279 $entity_type = \Drupal::entityManager()->getDefinition($this->entity_type);
|
Chris@0
|
280 $bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle';
|
Chris@0
|
281 $parameters[$bundle_parameter_key] = $this->bundle;
|
Chris@0
|
282 return $parameters;
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * {@inheritdoc}
|
Chris@0
|
287 */
|
Chris@0
|
288 public function isDeleted() {
|
Chris@0
|
289 return $this->deleted;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * {@inheritdoc}
|
Chris@0
|
294 */
|
Chris@0
|
295 public function getFieldStorageDefinition() {
|
Chris@0
|
296 if (!$this->fieldStorage) {
|
Chris@14
|
297 $field_storage_definition = NULL;
|
Chris@14
|
298
|
Chris@18
|
299 $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entity_type);
|
Chris@14
|
300 if (isset($field_storage_definitions[$this->field_name])) {
|
Chris@14
|
301 $field_storage_definition = $field_storage_definitions[$this->field_name];
|
Chris@14
|
302 }
|
Chris@14
|
303 // If this field has been deleted, try to find its field storage
|
Chris@14
|
304 // definition in the deleted fields repository.
|
Chris@14
|
305 elseif ($this->deleted) {
|
Chris@14
|
306 $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions();
|
Chris@14
|
307 foreach ($deleted_storage_definitions as $deleted_storage_definition) {
|
Chris@14
|
308 if ($deleted_storage_definition->getName() === $this->field_name) {
|
Chris@14
|
309 $field_storage_definition = $deleted_storage_definition;
|
Chris@14
|
310 }
|
Chris@14
|
311 }
|
Chris@14
|
312 }
|
Chris@14
|
313
|
Chris@14
|
314 if (!$field_storage_definition) {
|
Chris@0
|
315 throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}.");
|
Chris@0
|
316 }
|
Chris@14
|
317 if (!$field_storage_definition instanceof FieldStorageConfigInterface) {
|
Chris@0
|
318 throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}.");
|
Chris@0
|
319 }
|
Chris@14
|
320 $this->fieldStorage = $field_storage_definition;
|
Chris@0
|
321 }
|
Chris@0
|
322
|
Chris@0
|
323 return $this->fieldStorage;
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 /**
|
Chris@0
|
327 * {@inheritdoc}
|
Chris@0
|
328 */
|
Chris@0
|
329 public function isDisplayConfigurable($context) {
|
Chris@0
|
330 return TRUE;
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 /**
|
Chris@0
|
334 * {@inheritdoc}
|
Chris@0
|
335 */
|
Chris@0
|
336 public function getDisplayOptions($display_context) {
|
Chris@0
|
337 // Hide configurable fields by default.
|
Chris@0
|
338 return ['region' => 'hidden'];
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 /**
|
Chris@0
|
342 * {@inheritdoc}
|
Chris@0
|
343 */
|
Chris@0
|
344 public function isReadOnly() {
|
Chris@0
|
345 return FALSE;
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 /**
|
Chris@0
|
349 * {@inheritdoc}
|
Chris@0
|
350 */
|
Chris@0
|
351 public function isComputed() {
|
Chris@0
|
352 return FALSE;
|
Chris@0
|
353 }
|
Chris@0
|
354
|
Chris@0
|
355 /**
|
Chris@14
|
356 * {@inheritdoc}
|
Chris@14
|
357 */
|
Chris@14
|
358 public function getUniqueIdentifier() {
|
Chris@14
|
359 return $this->uuid();
|
Chris@14
|
360 }
|
Chris@14
|
361
|
Chris@14
|
362 /**
|
Chris@0
|
363 * Loads a field config entity based on the entity type and field name.
|
Chris@0
|
364 *
|
Chris@0
|
365 * @param string $entity_type_id
|
Chris@0
|
366 * ID of the entity type.
|
Chris@0
|
367 * @param string $bundle
|
Chris@0
|
368 * Bundle name.
|
Chris@0
|
369 * @param string $field_name
|
Chris@0
|
370 * Name of the field.
|
Chris@0
|
371 *
|
Chris@0
|
372 * @return static
|
Chris@0
|
373 * The field config entity if one exists for the provided field
|
Chris@0
|
374 * name, otherwise NULL.
|
Chris@0
|
375 */
|
Chris@0
|
376 public static function loadByName($entity_type_id, $bundle, $field_name) {
|
Chris@0
|
377 return \Drupal::entityManager()->getStorage('field_config')->load($entity_type_id . '.' . $bundle . '.' . $field_name);
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 }
|