FieldConfig.php 12.7 KB
Newer Older
1
2
<?php

3
namespace Drupal\field\Entity;
4

5
use Drupal\Core\Entity\EntityStorageInterface;
6
use Drupal\Core\Entity\FieldableEntityStorageInterface;
7
use Drupal\Core\Field\FieldConfigBase;
8
use Drupal\Core\Field\FieldException;
9
use Drupal\field\FieldStorageConfigInterface;
10
use Drupal\field\FieldConfigInterface;
11
12

/**
13
 * Defines the Field entity.
14
 *
15
 * @ConfigEntityType(
16
17
 *   id = "field_config",
 *   label = @Translation("Field"),
18
19
20
21
22
23
24
 *   label_collection = @Translation("Fields"),
 *   label_singular = @Translation("field"),
 *   label_plural = @Translation("fields"),
 *   label_count = @PluralTranslation(
 *     singular = "@count field",
 *     plural = "@count fields",
 *   ),
25
 *   handlers = {
26
27
 *     "access" = "Drupal\field\FieldConfigAccessControlHandler",
 *     "storage" = "Drupal\field\FieldConfigStorage"
28
 *   },
29
 *   config_prefix = "field",
30
31
 *   entity_keys = {
 *     "id" = "id",
32
 *     "label" = "label"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 *   },
 *   config_export = {
 *     "id",
 *     "field_name",
 *     "entity_type",
 *     "bundle",
 *     "label",
 *     "description",
 *     "required",
 *     "translatable",
 *     "default_value",
 *     "default_value_callback",
 *     "settings",
 *     "field_type",
47
48
49
 *   }
 * )
 */
50
class FieldConfig extends FieldConfigBase implements FieldConfigInterface {
51
52

  /**
53
   * Flag indicating whether the field is deleted.
54
   *
55
   * The delete() method marks the field as "deleted" and removes the
56
57
58
59
   * corresponding entry from the config storage, but keeps its definition in
   * the state storage while field data is purged by a separate
   * garbage-collection process.
   *
60
   * Deleted fields stay out of the regular entity lifecycle (notably, their
61
62
63
64
   * values are not populated in loaded entities, and are not saved back).
   *
   * @var bool
   */
65
  protected $deleted = FALSE;
66

67
  /**
68
   * The associated FieldStorageConfig entity.
69
   *
70
   * @var \Drupal\field\Entity\FieldStorageConfig
71
   */
72
  protected $fieldStorage;
73

74
  /**
75
   * Constructs a FieldConfig object.
76
   *
77
   * In most cases, Field entities are created via
78
   * FieldConfig::create($values), where $values is the same
79
80
   * parameter as in this constructor.
   *
81
   * @param array $values
82
   *   An array of field properties, keyed by property name. The
83
   *   storage associated with the field can be specified either with:
84
85
86
   *   - field_storage: the FieldStorageConfigInterface object,
   *   or by referring to an existing field storage in the current configuration
   *   with:
87
88
89
   *   - field_name: The field name.
   *   - entity_type: The entity type.
   *   Additionally, a 'bundle' property is required to indicate the entity
90
   *   bundle to which the field is attached to. Other array elements will be
91
92
93
   *   used to set the corresponding properties on the class; see the class
   *   property documentation for details.
   *
94
   * @see entity_create()
95
   */
96
  public function __construct(array $values, $entity_type = 'field_config') {
97
    // Allow either an injected FieldStorageConfig object, or a field_name and
98
    // entity_type.
99
100
    if (isset($values['field_storage'])) {
      if (!$values['field_storage'] instanceof FieldStorageConfigInterface) {
101
        throw new FieldException('Attempt to create a configurable field for a non-configurable field storage.');
102
      }
103
104
105
      $field_storage = $values['field_storage'];
      $values['field_name'] = $field_storage->getName();
      $values['entity_type'] = $field_storage->getTargetEntityTypeId();
106
107
108
      // The internal property is fieldStorage, not field_storage.
      unset($values['field_storage']);
      $values['fieldStorage'] = $field_storage;
109
    }
110
111
    else {
      if (empty($values['field_name'])) {
112
        throw new FieldException('Attempt to create a field without a field_name.');
113
114
      }
      if (empty($values['entity_type'])) {
115
        throw new FieldException("Attempt to create a field '{$values['field_name']}' without an entity_type.");
116
      }
117
    }
118
    // 'bundle' is required in either case.
119
    if (empty($values['bundle'])) {
120
      throw new FieldException("Attempt to create a field '{$values['field_name']}' without a bundle.");
121
122
123
124
125
    }

    parent::__construct($values, $entity_type);
  }

126
127
128
129
  /**
   * {@inheritdoc}
   */
  public function postCreate(EntityStorageInterface $storage) {
130
131
    parent::postCreate($storage);

132
    // Validate that we have a valid storage for this field. This throws an
133
    // exception if the storage is invalid.
134
    $this->getFieldStorageDefinition();
135

136
137
    // 'Label' defaults to the field name (mostly useful for fields created in
    // tests).
138
    if (empty($this->label)) {
139
      $this->label = $this->getName();
140
141
142
    }
  }

143
  /**
144
   * Overrides \Drupal\Core\Entity\Entity::preSave().
145
   *
146
   * @throws \Drupal\Core\Field\FieldException
147
   *   If the field definition is invalid.
148
149
   * @throws \Drupal\Core\Entity\EntityStorageException
   *   In case of failures at the configuration storage level.
150
   */
151
  public function preSave(EntityStorageInterface $storage) {
152
153
154
    $entity_manager = \Drupal::entityManager();
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');

155
    $storage_definition = $this->getFieldStorageDefinition();
156

157
158
159
    // Filter out unknown settings and make sure all settings are present, so
    // that a complete field definition is passed to the various hooks and
    // written to config.
160
    $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType());
161
162
    $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

163
    if ($this->isNew()) {
164
      // Notify the entity storage.
165
      $entity_manager->onFieldDefinitionCreate($this);
166
167
    }
    else {
168
169
      // Some updates are always disallowed.
      if ($this->entity_type != $this->original->entity_type) {
170
        throw new FieldException("Cannot change an existing field's entity_type.");
171
      }
172
      if ($this->bundle != $this->original->bundle) {
173
        throw new FieldException("Cannot change an existing field's bundle.");
174
      }
175
      if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
176
        throw new FieldException("Cannot change an existing field's storage.");
177
      }
178
      // Notify the entity storage.
179
      $entity_manager->onFieldDefinitionUpdate($this, $this->original);
180
    }
181
182

    parent::preSave($storage);
183
184
185
186
187
188
189
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
190
    // Mark the field_storage_config as a dependency.
191
    $this->addDependency('config', $this->getFieldStorageDefinition()->getConfigDependencyName());
192
    return $this;
193
  }
194

195
  /**
196
   * {@inheritdoc}
197
   */
198
  public static function preDelete(EntityStorageInterface $storage, array $fields) {
199
200
    /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
    $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
201
    $entity_type_manager = \Drupal::entityTypeManager();
202

203
    parent::preDelete($storage, $fields);
204

205
206
    // Keep the field definitions in the deleted fields repository so we can use
    // them later during field_purge_batch().
207
    /** @var \Drupal\field\FieldConfigInterface $field */
208
    foreach ($fields as $field) {
209
210
211
212
      // Only mark a field for purging if there is data. Otherwise, just remove
      // it.
      $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
      if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
213
214
215
216
        $field = clone $field;
        $field->deleted = TRUE;
        $field->fieldStorage = NULL;
        $deleted_fields_repository->addFieldDefinition($field);
217
      }
218
219
220
    }
  }

221
  /**
222
   * {@inheritdoc}
223
   */
224
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
225
    // Clear the cache upfront, to refresh the results of getBundles().
226
    \Drupal::entityManager()->clearCachedFieldDefinitions();
227

228
    // Notify the entity storage.
229
230
    foreach ($fields as $field) {
      if (!$field->deleted) {
231
        \Drupal::entityManager()->onFieldDefinitionDelete($field);
232
      }
233
    }
234

235
236
    // If this is part of a configuration synchronization then the following
    // configuration updates are not necessary.
237
    $entity = reset($fields);
238
239
240
241
    if ($entity->isSyncing()) {
      return;
    }

242
243
    // Delete the associated field storages if they are not used anymore and are
    // not persistent.
244
    $storages_to_delete = [];
245
246
    foreach ($fields as $field) {
      $storage_definition = $field->getFieldStorageDefinition();
247
      if (!$field->deleted && !$field->isUninstalling() && $storage_definition->isDeletable()) {
248
249
        // Key by field UUID to avoid deleting the same storage twice.
        $storages_to_delete[$storage_definition->uuid()] = $storage_definition;
250
      }
251
    }
252
253
    if ($storages_to_delete) {
      \Drupal::entityManager()->getStorage('field_storage_config')->delete($storages_to_delete);
254
    }
255
256
  }

257
258
259
  /**
   * {@inheritdoc}
   */
260
261
262
  protected function linkTemplates() {
    $link_templates = parent::linkTemplates();
    if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
263
264
265
      $link_templates["{$this->entity_type}-field-edit-form"] = 'entity.field_config.' . $this->entity_type . '_field_edit_form';
      $link_templates["{$this->entity_type}-storage-edit-form"] = 'entity.field_config.' . $this->entity_type . '_storage_edit_form';
      $link_templates["{$this->entity_type}-field-delete-form"] = 'entity.field_config.' . $this->entity_type . '_field_delete_form';
266

267
268
      if (isset($link_templates['config-translation-overview'])) {
        $link_templates["config-translation-overview.{$this->entity_type}"] = "entity.field_config.config_translation_overview.{$this->entity_type}";
269
      }
270
    }
271
272
    return $link_templates;
  }
273

274
275
276
  /**
   * {@inheritdoc}
   */
277
278
  protected function urlRouteParameters($rel) {
    $parameters = parent::urlRouteParameters($rel);
279
    $entity_type = \Drupal::entityManager()->getDefinition($this->entity_type);
280
281
    $bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle';
    $parameters[$bundle_parameter_key] = $this->bundle;
282
    return $parameters;
283
284
  }

285
286
287
  /**
   * {@inheritdoc}
   */
288
289
  public function isDeleted() {
    return $this->deleted;
290
291
  }

292
293
294
  /**
   * {@inheritdoc}
   */
295
296
  public function getFieldStorageDefinition() {
    if (!$this->fieldStorage) {
297
298
      $field_storage_definition = NULL;

299
      $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entity_type);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
      if (isset($field_storage_definitions[$this->field_name])) {
        $field_storage_definition = $field_storage_definitions[$this->field_name];
      }
      // If this field has been deleted, try to find its field storage
      // definition in the deleted fields repository.
      elseif ($this->deleted) {
        $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions();
        foreach ($deleted_storage_definitions as $deleted_storage_definition) {
          if ($deleted_storage_definition->getName() === $this->field_name) {
            $field_storage_definition = $deleted_storage_definition;
          }
        }
      }

      if (!$field_storage_definition) {
315
        throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}.");
316
      }
317
      if (!$field_storage_definition instanceof FieldStorageConfigInterface) {
318
        throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}.");
319
      }
320
      $this->fieldStorage = $field_storage_definition;
321
    }
322
323

    return $this->fieldStorage;
324
325
  }

326
327
328
329
330
331
332
333
334
335
336
337
  /**
   * {@inheritdoc}
   */
  public function isDisplayConfigurable($context) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getDisplayOptions($display_context) {
    // Hide configurable fields by default.
338
    return ['region' => 'hidden'];
339
340
  }

341
342
343
344
345
346
347
348
349
350
351
352
353
354
  /**
   * {@inheritdoc}
   */
  public function isReadOnly() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isComputed() {
    return FALSE;
  }

355
356
357
358
359
360
361
  /**
   * {@inheritdoc}
   */
  public function getUniqueIdentifier() {
    return $this->uuid();
  }

362
363
364
365
366
367
368
369
370
371
372
  /**
   * Loads a field config entity based on the entity type and field name.
   *
   * @param string $entity_type_id
   *   ID of the entity type.
   * @param string $bundle
   *   Bundle name.
   * @param string $field_name
   *   Name of the field.
   *
   * @return static
373
   *   The field config entity if one exists for the provided field
374
375
376
   *   name, otherwise NULL.
   */
  public static function loadByName($entity_type_id, $bundle, $field_name) {
377
    return \Drupal::entityManager()->getStorage('field_config')->load($entity_type_id . '.' . $bundle . '.' . $field_name);
378
379
  }

380
}