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