field.post_update.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for Field module.
  5. */
  6. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  7. use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
  8. use Drupal\field\Entity\FieldStorageConfig;
  9. use Drupal\field\Entity\FieldConfig;
  10. /**
  11. * Re-save all field storage config objects to add 'custom_storage' property.
  12. */
  13. function field_post_update_save_custom_storage_property() {
  14. foreach (FieldStorageConfig::loadMultiple() as $field_storage_config) {
  15. $field_storage_config->save();
  16. }
  17. return t('All field storage configuration objects re-saved.');
  18. }
  19. /**
  20. * Fixes the 'handler' setting for entity reference fields.
  21. */
  22. function field_post_update_entity_reference_handler_setting() {
  23. foreach (FieldConfig::loadMultiple() as $field_config) {
  24. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  25. $item_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
  26. $class = $field_type_manager->getPluginClass($field_config->getType());
  27. if ($class === $item_class || is_subclass_of($class, $item_class)) {
  28. // field_field_config_presave() will fix the 'handler' setting on save.
  29. $field_config->save();
  30. }
  31. }
  32. return t('Selection handler for entity reference fields have been adjusted.');
  33. }
  34. /**
  35. * Adds the 'size' setting for email widgets.
  36. */
  37. function field_post_update_email_widget_size_setting() {
  38. foreach (EntityFormDisplay::loadMultiple() as $entity_form_display) {
  39. $changed = FALSE;
  40. foreach ($entity_form_display->getComponents() as $name => $options) {
  41. if (isset($options['type']) && $options['type'] === 'email_default') {
  42. $options['settings']['size'] = '60';
  43. $entity_form_display->setComponent($name, $options);
  44. $changed = TRUE;
  45. }
  46. }
  47. if ($changed) {
  48. $entity_form_display->save();
  49. }
  50. }
  51. return t('The new size setting for email widgets has been added.');
  52. }
  53. /**
  54. * Remove the stale 'handler_submit' setting for entity_reference fields.
  55. */
  56. function field_post_update_remove_handler_submit_setting() {
  57. $config = \Drupal::configFactory();
  58. /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  59. $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  60. // Iterate on all field configs.
  61. foreach ($config->listAll('field.field.') as $field_id) {
  62. $field = $config->getEditable($field_id);
  63. $class = $field_type_manager->getPluginClass($field->get('field_type'));
  64. // Deal only with entity reference fields and descendants.
  65. if ($class === EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) {
  66. if ($field->get('settings.handler_submit')) {
  67. // Remove 'handler_settings' from settings.
  68. $field->clear('settings.handler_submit')->save();
  69. }
  70. }
  71. }
  72. }