field.install 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  8. * Removes the stale 'target_bundle' storage setting on entity_reference fields.
  9. */
  10. function field_update_8001() {
  11. $config = \Drupal::configFactory();
  12. /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  13. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  14. // Iterate on all fields storage.
  15. foreach ($config->listAll('field.storage.') as $field_id) {
  16. $field_storage = $config->getEditable($field_id);
  17. $class = $field_type_manager->getPluginClass($field_storage->get('type'));
  18. // Deal only with entity reference fields and descendants.
  19. if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
  20. // Remove 'target_bundle' from settings.
  21. $field_storage->clear('settings.target_bundle')->save(TRUE);
  22. }
  23. }
  24. }
  25. /**
  26. * The 'entity_reference' field type is now provided by core.
  27. */
  28. function field_update_8002() {
  29. $config_factory = \Drupal::configFactory();
  30. // Iterate on all configuration entities.
  31. foreach ($config_factory->listAll() as $id) {
  32. $changed = FALSE;
  33. $config = $config_factory->getEditable($id);
  34. // Update field storage configurations.
  35. if (strpos($id, 'field.storage.') === 0) {
  36. // Deal only with entity reference fields.
  37. if ($config->get('type') == 'entity_reference') {
  38. // Fix the type provider.
  39. $config->set('module', 'core');
  40. $changed = TRUE;
  41. }
  42. }
  43. // Remove entity_reference module dependency from any configuration entity.
  44. if ($dependencies = $config->get('dependencies.module')) {
  45. if (($delta = array_search('entity_reference', $dependencies)) !== FALSE) {
  46. unset($dependencies[$delta]);
  47. if ($dependencies) {
  48. $config->set('dependencies.module', array_values($dependencies));
  49. }
  50. else {
  51. $config->clear('dependencies.module');
  52. }
  53. $changed = TRUE;
  54. }
  55. }
  56. if ($changed) {
  57. $config->save(TRUE);
  58. }
  59. }
  60. }
  61. /**
  62. * Populate the new 'auto_create_bundle' setting for entity reference fields.
  63. */
  64. function field_update_8003() {
  65. $config = \Drupal::configFactory();
  66. /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  67. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  68. // Iterate over all fields.
  69. foreach ($config->listAll('field.field.') as $field_id) {
  70. $field = $config->getEditable($field_id);
  71. $class = $field_type_manager->getPluginClass($field->get('field_type'));
  72. // Deal only with entity reference fields and descendants.
  73. if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
  74. $handler_settings = $field->get('settings.handler_settings');
  75. if (is_array($handler_settings) && !empty($handler_settings['auto_create'])) {
  76. // If the field can reference multiple bundles, pick the first one
  77. // available in order to replicate the previous behavior.
  78. if (is_array($handler_settings['target_bundles']) && count($handler_settings['target_bundles']) > 1) {
  79. $handler_settings['auto_create_bundle'] = reset($handler_settings['target_bundles']);
  80. }
  81. // Otherwise, we don't know which bundle to use for auto-creation so we
  82. // have to disable the functionality completely.
  83. elseif (!$handler_settings['target_bundles']) {
  84. $handler_settings['auto_create'] = FALSE;
  85. $handler_settings['auto_create_bundle'] = NULL;
  86. }
  87. }
  88. $field->set('settings.handler_settings', $handler_settings)->save(TRUE);
  89. }
  90. }
  91. }