text.post_update.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * @file
  4. * Contains post update hooks for the text module.
  5. */
  6. use Drupal\Core\Config\Entity\ConfigEntityUpdater;
  7. use Drupal\Core\Entity\Display\EntityDisplayInterface;
  8. use Drupal\Core\Field\FieldConfigInterface;
  9. use Drupal\text\Plugin\Field\FieldWidget\TextareaWithSummaryWidget;
  10. /**
  11. * Update text_with_summary fields to add summary required flags.
  12. */
  13. function text_post_update_add_required_summary_flag(&$sandbox = NULL) {
  14. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  15. $field_callback = function (FieldConfigInterface $field) {
  16. if ($field->getType() !== 'text_with_summary') {
  17. return FALSE;
  18. }
  19. $field->setSetting('required_summary', FALSE);
  20. return TRUE;
  21. };
  22. $config_entity_updater->update($sandbox, 'field_config', $field_callback);
  23. }
  24. /**
  25. * Update text_with_summary widgets to add summary required flags.
  26. */
  27. function text_post_update_add_required_summary_flag_form_display(&$sandbox = NULL) {
  28. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  29. /** @var \Drupal\Core\Field\WidgetPluginManager $field_widget_manager */
  30. $field_widget_manager = \Drupal::service('plugin.manager.field.widget');
  31. $widget_callback = function (EntityDisplayInterface $display) use ($field_widget_manager) {
  32. $needs_save = FALSE;
  33. foreach ($display->getComponents() as $field_name => $component) {
  34. if (empty($component['type'])) {
  35. continue;
  36. }
  37. $plugin_definition = $field_widget_manager->getDefinition($component['type'], FALSE);
  38. if (is_a($plugin_definition['class'], TextareaWithSummaryWidget::class, TRUE)) {
  39. $component['settings']['show_summary'] = FALSE;
  40. $display->setComponent($field_name, $component);
  41. $needs_save = TRUE;
  42. }
  43. }
  44. return $needs_save;
  45. };
  46. $config_entity_updater->update($sandbox, 'entity_form_display', $widget_callback);
  47. }