123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * @file
- * Install, update and uninstall functions for the field module.
- */
- use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
- use Drupal\field\Entity\FieldConfig;
- use Drupal\field\Entity\FieldStorageConfig;
- /**
- * Removes the stale 'target_bundle' storage setting on entity_reference fields.
- */
- function field_update_8001() {
- $config = \Drupal::configFactory();
- /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
- $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
- // Iterate on all fields storage.
- foreach ($config->listAll('field.storage.') as $field_id) {
- $field_storage = $config->getEditable($field_id);
- $class = $field_type_manager->getPluginClass($field_storage->get('type'));
- // Deal only with entity reference fields and descendants.
- if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
- // Remove 'target_bundle' from settings.
- $field_storage->clear('settings.target_bundle')->save(TRUE);
- }
- }
- }
- /**
- * The 'entity_reference' field type is now provided by core.
- */
- function field_update_8002() {
- $config_factory = \Drupal::configFactory();
- // Iterate on all configuration entities.
- foreach ($config_factory->listAll() as $id) {
- $changed = FALSE;
- $config = $config_factory->getEditable($id);
- // Update field storage configurations.
- if (strpos($id, 'field.storage.') === 0) {
- // Deal only with entity reference fields.
- if ($config->get('type') == 'entity_reference') {
- // Fix the type provider.
- $config->set('module', 'core');
- $changed = TRUE;
- }
- }
- // Remove entity_reference module dependency from any configuration entity.
- if ($dependencies = $config->get('dependencies.module')) {
- if (($delta = array_search('entity_reference', $dependencies)) !== FALSE) {
- unset($dependencies[$delta]);
- if ($dependencies) {
- $config->set('dependencies.module', array_values($dependencies));
- }
- else {
- $config->clear('dependencies.module');
- }
- $changed = TRUE;
- }
- }
- if ($changed) {
- $config->save(TRUE);
- }
- }
- }
- /**
- * Populate the new 'auto_create_bundle' setting for entity reference fields.
- */
- function field_update_8003() {
- $config = \Drupal::configFactory();
- /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
- $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
- // Iterate over all fields.
- foreach ($config->listAll('field.field.') as $field_id) {
- $field = $config->getEditable($field_id);
- $class = $field_type_manager->getPluginClass($field->get('field_type'));
- // Deal only with entity reference fields and descendants.
- if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
- $handler_settings = $field->get('settings.handler_settings');
- if (is_array($handler_settings) && !empty($handler_settings['auto_create'])) {
- // If the field can reference multiple bundles, pick the first one
- // available in order to replicate the previous behavior.
- if (is_array($handler_settings['target_bundles']) && count($handler_settings['target_bundles']) > 1) {
- $handler_settings['auto_create_bundle'] = reset($handler_settings['target_bundles']);
- }
- // Otherwise, we don't know which bundle to use for auto-creation so we
- // have to disable the functionality completely.
- elseif (!$handler_settings['target_bundles']) {
- $handler_settings['auto_create'] = FALSE;
- $handler_settings['auto_create_bundle'] = NULL;
- }
- }
- $field->set('settings.handler_settings', $handler_settings)->save(TRUE);
- }
- }
- }
- /**
- * Update the definition of deleted fields.
- */
- function field_update_8500() {
- $state = \Drupal::state();
- // Convert the old deleted field definitions from an array to a FieldConfig
- // object.
- $deleted_field_definitions = $state->get('field.field.deleted', []);
- foreach ($deleted_field_definitions as $key => $deleted_field_definition) {
- if (is_array($deleted_field_definition)) {
- $deleted_field_definitions[$key] = new FieldConfig($deleted_field_definition);
- }
- }
- $state->set('field.field.deleted', $deleted_field_definitions);
- // Convert the old deleted field storage definitions from an array to a
- // FieldStorageConfig object.
- $deleted_field_storage_definitions = $state->get('field.storage.deleted', []);
- foreach ($deleted_field_storage_definitions as $key => $deleted_field_storage_definition) {
- if (is_array($deleted_field_storage_definition)) {
- $deleted_field_storage_definitions[$key] = new FieldStorageConfig($deleted_field_storage_definition);
- }
- }
- $state->set('field.storage.deleted', $deleted_field_storage_definitions);
- }
|