FieldConfig.php 12 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\field\Entity\FieldConfig.
6
7
 */

8
namespace Drupal\field\Entity;
9

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Field\FieldConfigBase;
13
use Drupal\Core\Field\FieldException;
14
use Drupal\field\FieldStorageConfigInterface;
15
use Drupal\field\FieldConfigInterface;
16
17

/**
18
 * Defines the Field entity.
19
 *
20
 * @ConfigEntityType(
21
22
 *   id = "field_config",
 *   label = @Translation("Field"),
23
 *   handlers = {
24
25
 *     "access" = "Drupal\field\FieldConfigAccessControlHandler",
 *     "storage" = "Drupal\field\FieldConfigStorage"
26
 *   },
27
 *   config_prefix = "field",
28
29
 *   entity_keys = {
 *     "id" = "id",
30
 *     "label" = "label"
31
32
33
 *   }
 * )
 */
34
class FieldConfig extends FieldConfigBase implements FieldConfigInterface {
35
36

  /**
37
   * Flag indicating whether the field is deleted.
38
   *
39
   * The delete() method marks the field as "deleted" and removes the
40
41
42
43
   * 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.
   *
44
   * Deleted fields stay out of the regular entity lifecycle (notably, their
45
46
47
48
   * values are not populated in loaded entities, and are not saved back).
   *
   * @var bool
   */
49
  public $deleted = FALSE;
50

51
  /**
52
   * The associated FieldStorageConfig entity.
53
   *
54
   * @var \Drupal\field\Entity\FieldStorageConfig
55
   */
56
  protected $fieldStorage;
57

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

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

110
111
112
113
  /**
   * {@inheritdoc}
   */
  public function postCreate(EntityStorageInterface $storage) {
114
115
    parent::postCreate($storage);

116
    // Validate that we have a valid storage for this field. This throws an
117
    // exception if the storage is invalid.
118
    $this->getFieldStorageDefinition();
119

120
121
    // 'Label' defaults to the field name (mostly useful for fields created in
    // tests).
122
    if (empty($this->label)) {
123
      $this->label = $this->getName();
124
125
126
    }
  }

127
  /**
128
   * Overrides \Drupal\Core\Entity\Entity::preSave().
129
   *
130
   * @throws \Drupal\Core\Field\FieldException
131
   *   If the field definition is invalid.
132
133
   * @throws \Drupal\Core\Entity\EntityStorageException
   *   In case of failures at the configuration storage level.
134
   */
135
  public function preSave(EntityStorageInterface $storage) {
136
137
138
    $entity_manager = \Drupal::entityManager();
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');

139
    $storage_definition = $this->getFieldStorageDefinition();
140

141
    if ($this->isNew()) {
142
143
      // Set the default field settings.
      $this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type);
144
      // Notify the entity storage.
145
      $entity_manager->getStorage($this->entity_type)->onFieldDefinitionCreate($this);
146
147
    }
    else {
148
149
      // Some updates are always disallowed.
      if ($this->entity_type != $this->original->entity_type) {
150
        throw new FieldException("Cannot change an existing field's entity_type.");
151
      }
152
      if ($this->bundle != $this->original->bundle && empty($this->bundleRenameAllowed)) {
153
        throw new FieldException("Cannot change an existing field's bundle.");
154
      }
155
      if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
156
        throw new FieldException("Cannot change an existing field's storage.");
157
      }
158
159
      // Set the default field settings.
      $this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type);
160
      // Notify the entity storage.
161
      $entity_manager->getStorage($this->entity_type)->onFieldDefinitionUpdate($this, $this->original);
162
    }
163
164

    parent::preSave($storage);
165
166
167
168
169
170
171
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
172
    // Mark the field_storage_config as a a dependency.
173
    $this->addDependency('config', $this->getFieldStorageDefinition()->getConfigDependencyName());
174
    return $this->dependencies;
175
  }
176

177
  /**
178
   * {@inheritdoc}
179
   */
180
  public static function preDelete(EntityStorageInterface $storage, array $fields) {
181
182
    $state = \Drupal::state();

183
    // Keep the field definitions in the state storage so we can use them
184
    // later during field_purge_batch().
185
186
187
188
    $deleted_fields = $state->get('field.field.deleted') ?: array();
    foreach ($fields as $field) {
      if (!$field->deleted) {
        $config = $field->toArray();
189
        $config['deleted'] = TRUE;
190
191
        $config['field_storage_uuid'] = $field->getFieldStorageDefinition()->uuid();
        $deleted_fields[$field->uuid()] = $config;
192
      }
193
    }
194
    $state->set('field.field.deleted', $deleted_fields);
195
196
  }

197
  /**
198
   * {@inheritdoc}
199
   */
200
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
201
    // Clear the cache upfront, to refresh the results of getBundles().
202
    \Drupal::entityManager()->clearCachedFieldDefinitions();
203

204
    // Notify the entity storage.
205
206
207
    foreach ($fields as $field) {
      if (!$field->deleted) {
        \Drupal::entityManager()->getStorage($field->entity_type)->onFieldDefinitionDelete($field);
208
      }
209
    }
210

211
212
    // If this is part of a configuration synchronization then the following
    // configuration updates are not necessary.
213
    $entity = reset($fields);
214
215
216
217
    if ($entity->isSyncing()) {
      return;
    }

218
    // Delete field storages that have no more fields.
219
    $storages_to_delete = array();
220
221
222
    foreach ($fields as $field) {
      $storage_definition = $field->getFieldStorageDefinition();
      if (!$field->deleted && empty($field->noFieldDelete) && !$field->isUninstalling() && count($storage_definition->getBundles()) == 0) {
223
224
        // Key by field UUID to avoid deleting the same storage twice.
        $storages_to_delete[$storage_definition->uuid()] = $storage_definition;
225
      }
226
    }
227
228
    if ($storages_to_delete) {
      \Drupal::entityManager()->getStorage('field_storage_config')->delete($storages_to_delete);
229
    }
230

231
232
    // Cleanup entity displays.
    $displays_to_update = array();
233
234
235
    foreach ($fields as $field) {
      if (!$field->deleted) {
        $view_modes = \Drupal::entityManager()->getViewModeOptions($field->entity_type, TRUE);
236
        foreach (array_keys($view_modes) as $mode) {
237
          $displays_to_update['entity_view_display'][$field->entity_type . '.' . $field->bundle . '.' . $mode][] = $field->getName();
238
        }
239
        $form_modes = \Drupal::entityManager()->getFormModeOptions($field->entity_type, TRUE);
240
        foreach (array_keys($form_modes) as $mode) {
241
          $displays_to_update['entity_form_display'][$field->entity_type . '.' . $field->bundle . '.' . $mode][] = $field->getName();
242
243
244
245
246
247
248
249
250
        }
      }
    }
    foreach ($displays_to_update as $type => $ids) {
      foreach (entity_load_multiple($type, array_keys($ids)) as $id => $display) {
        foreach ($ids[$id] as $field_name) {
          $display->removeComponent($field_name);
        }
        $display->save();
251
252
253
254
      }
    }
  }

255
256
257
  /**
   * {@inheritdoc}
   */
258
259
260
  protected function linkTemplates() {
    $link_templates = parent::linkTemplates();
    if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
261
      $link_templates['edit-form'] = 'field_ui.field_edit_' . $this->entity_type;
262
      $link_templates['storage-edit-form'] = 'field_ui.storage_edit_' . $this->entity_type;
263
264
      $link_templates['delete-form'] = 'field_ui.delete_' . $this->entity_type;

265
266
267
      if (isset($link_templates['drupal:config-translation-overview'])) {
        $link_templates['drupal:config-translation-overview'] .= $link_templates['edit-form'];
      }
268
    }
269
270
    return $link_templates;
  }
271

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

282
283
284
  /**
   * {@inheritdoc}
   */
285
286
  public function isDeleted() {
    return $this->deleted;
287
288
  }

289
290
291
  /**
   * {@inheritdoc}
   */
292
293
294
295
  public function getFieldStorageDefinition() {
    if (!$this->fieldStorage) {
      $fields = $this->entityManager()->getFieldStorageDefinitions($this->entity_type);
      if (!isset($fields[$this->field_name])) {
296
        throw new FieldException(String::format('Attempt to create a field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));      }
297
      if (!$fields[$this->field_name] instanceof FieldStorageConfigInterface) {
298
        throw new FieldException(String::format('Attempt to create a configurable field of non-configurable field storage @field_name.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
299
300
      }
      $this->fieldStorage = $fields[$this->field_name];
301
    }
302
303

    return $this->fieldStorage;
304
305
  }

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  /**
   * {@inheritdoc}
   */
  public function isDisplayConfigurable($context) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getDisplayOptions($display_context) {
    // Hide configurable fields by default.
    return array('type' => 'hidden');
  }

321
322
323
324
325
326
327
328
329
330
331
332
333
334
  /**
   * {@inheritdoc}
   */
  public function isReadOnly() {
    return FALSE;
  }

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

335
336
337
338
339
340
341
342
343
344
345
  /**
   * 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
346
   *   The field config entity if one exists for the provided field
347
348
349
   *   name, otherwise NULL.
   */
  public static function loadByName($entity_type_id, $bundle, $field_name) {
350
    return \Drupal::entityManager()->getStorage('field_config')->load($entity_type_id . '.' . $bundle . '.' . $field_name);
351
352
  }

353
}