field.install 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the field module.
  5. */
  6. use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
  7. use Drupal\field\Entity\FieldConfig;
  8. use Drupal\field\Entity\FieldStorageConfig;
  9. /**
  10. * Removes the stale 'target_bundle' storage setting on entity_reference fields.
  11. */
  12. function field_update_8001() {
  13. $config = \Drupal::configFactory();
  14. /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  15. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  16. // Iterate on all fields storage.
  17. foreach ($config->listAll('field.storage.') as $field_id) {
  18. $field_storage = $config->getEditable($field_id);
  19. $class = $field_type_manager->getPluginClass($field_storage->get('type'));
  20. // Deal only with entity reference fields and descendants.
  21. if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
  22. // Remove 'target_bundle' from settings.
  23. $field_storage->clear('settings.target_bundle')->save(TRUE);
  24. }
  25. }
  26. }
  27. /**
  28. * The 'entity_reference' field type is now provided by core.
  29. */
  30. function field_update_8002() {
  31. $config_factory = \Drupal::configFactory();
  32. // Iterate on all configuration entities.
  33. foreach ($config_factory->listAll() as $id) {
  34. $changed = FALSE;
  35. $config = $config_factory->getEditable($id);
  36. // Update field storage configurations.
  37. if (strpos($id, 'field.storage.') === 0) {
  38. // Deal only with entity reference fields.
  39. if ($config->get('type') == 'entity_reference') {
  40. // Fix the type provider.
  41. $config->set('module', 'core');
  42. $changed = TRUE;
  43. }
  44. }
  45. // Remove entity_reference module dependency from any configuration entity.
  46. if ($dependencies = $config->get('dependencies.module')) {
  47. if (($delta = array_search('entity_reference', $dependencies)) !== FALSE) {
  48. unset($dependencies[$delta]);
  49. if ($dependencies) {
  50. $config->set('dependencies.module', array_values($dependencies));
  51. }
  52. else {
  53. $config->clear('dependencies.module');
  54. }
  55. $changed = TRUE;
  56. }
  57. }
  58. if ($changed) {
  59. $config->save(TRUE);
  60. }
  61. }
  62. }
  63. /**
  64. * Populate the new 'auto_create_bundle' setting for entity reference fields.
  65. */
  66. function field_update_8003() {
  67. $config = \Drupal::configFactory();
  68. /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  69. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  70. // Iterate over all fields.
  71. foreach ($config->listAll('field.field.') as $field_id) {
  72. $field = $config->getEditable($field_id);
  73. $class = $field_type_manager->getPluginClass($field->get('field_type'));
  74. // Deal only with entity reference fields and descendants.
  75. if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
  76. $handler_settings = $field->get('settings.handler_settings');
  77. if (is_array($handler_settings) && !empty($handler_settings['auto_create'])) {
  78. // If the field can reference multiple bundles, pick the first one
  79. // available in order to replicate the previous behavior.
  80. if (is_array($handler_settings['target_bundles']) && count($handler_settings['target_bundles']) > 1) {
  81. $handler_settings['auto_create_bundle'] = reset($handler_settings['target_bundles']);
  82. }
  83. // Otherwise, we don't know which bundle to use for auto-creation so we
  84. // have to disable the functionality completely.
  85. elseif (!$handler_settings['target_bundles']) {
  86. $handler_settings['auto_create'] = FALSE;
  87. $handler_settings['auto_create_bundle'] = NULL;
  88. }
  89. }
  90. $field->set('settings.handler_settings', $handler_settings)->save(TRUE);
  91. }
  92. }
  93. }
  94. /**
  95. * Update the definition of deleted fields.
  96. */
  97. function field_update_8500() {
  98. $state = \Drupal::state();
  99. // Convert the old deleted field definitions from an array to a FieldConfig
  100. // object.
  101. $deleted_field_definitions = $state->get('field.field.deleted', []);
  102. foreach ($deleted_field_definitions as $key => $deleted_field_definition) {
  103. if (is_array($deleted_field_definition)) {
  104. $deleted_field_definitions[$key] = new FieldConfig($deleted_field_definition);
  105. }
  106. }
  107. $state->set('field.field.deleted', $deleted_field_definitions);
  108. // Convert the old deleted field storage definitions from an array to a
  109. // FieldStorageConfig object.
  110. $deleted_field_storage_definitions = $state->get('field.storage.deleted', []);
  111. foreach ($deleted_field_storage_definitions as $key => $deleted_field_storage_definition) {
  112. if (is_array($deleted_field_storage_definition)) {
  113. $deleted_field_storage_definitions[$key] = new FieldStorageConfig($deleted_field_storage_definition);
  114. }
  115. }
  116. $state->set('field.storage.deleted', $deleted_field_storage_definitions);
  117. }