interdiff impossible; taking evasive action reverted: --- b/core/modules/field/field.module +++ a/core/modules/field/field.module @@ -6,8 +6,6 @@ */ use Drupal\Core\Config\ConfigImporter; -use Drupal\Core\Entity\Entity\EntityFormDisplay; -use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface; use Drupal\field\ConfigImporterFieldPurger; @@ -349,47 +347,6 @@ } } -/** - * Implements hook_ENTITY_TYPE_insert() for 'field_config'. - * - * Allow other view modes to update their configuration for the new field. - * Otherwise, configuration for view modes won't get updated until the mode - * is used for the first time, creating noise in config diffs. - * - * @see https://www.drupal.org/node/2915036 - */ -function field_field_config_insert(FieldConfigInterface $field) { - if ($field->isSyncing()) { - // Don't change anything during a configuration sync. - return; - } - - $entity_type_id = $field->getTargetEntityTypeId(); - $bundle = $field->getTargetBundle(); - - $entity_display_repository = \Drupal::service('entity_display.repository'); - $view_modes = $entity_display_repository->getViewModes($entity_type_id); - $form_modes = $entity_display_repository->getFormModes($entity_type_id); - - // Update view modes. - foreach ($view_modes as $view_mode_id => $view_mode) { - if ($view_mode['status']) { - if ($display = EntityViewDisplay::load($entity_type_id . '.' . $bundle . '.' . $view_mode_id)) { - $display->save(); - } - } - } - - // Update form modes. - foreach ($form_modes as $form_mode_id => $form_mode) { - if ($form_mode['status']) { - if ($form_display = EntityFormDisplay::load($entity_type_id . '.' . $bundle . '.' . $form_mode_id)) { - $form_display->save(); - } - } - } -} - /** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. * unchanged: --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -335,6 +335,26 @@ function field_form_config_admin_import_form_alter(&$form, FormStateInterface $f } /** + * Implements hook_ENTITY_TYPE_insert() for 'field_config'. + * + * Allow other view modes to update their configuration for the new field. + * Otherwise, configuration for view modes won't get updated until the mode + * is used for the first time, creating noise in config diffs. + * + * @see https://www.drupal.org/node/2915036 + */ +function field_field_config_insert(FieldConfigInterface $field) { + if ($field->isSyncing()) { + // Don't change anything during a configuration sync. + return; + } + + /** @var \Drupal\field\EntityDisplayRebuilder $display_rebuilder */ + $display_rebuilder = \Drupal::service('field.entity_display_rebuilder'); + $display_rebuilder->rebuildEntityTypeDisplays($field->getTargetEntityTypeId()); +} + +/** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. * * Reset the field handler settings, when the storage target_type is changed on only in patch2: unchanged: --- a/core/modules/field/field.services.yml +++ b/core/modules/field/field.services.yml @@ -5,3 +5,6 @@ services: - { name: module_install.uninstall_validator } arguments: ['@entity_type.manager', '@string_translation', '@plugin.manager.field.field_type'] lazy: true + field.entity_display_rebuilder: + class: Drupal\field\EntityDisplayRebuilder + arguments: ['@entity_type.manager', '@entity_display.repository', '@entity_type.bundle.info'] only in patch2: unchanged: --- /dev/null +++ b/core/modules/field/src/EntityDisplayRebuilder.php @@ -0,0 +1,91 @@ +entityTypeManager = $entity_type_manager; + $this->entityDisplayRepository = $entity_display_repository; + } + + /** + * Rebuild displays for all Entity Types. + */ + public function rebuildAllEntityTypeDisplays() { + + // Loop through all entity types. + $entity_types = $this->entityTypeManager->getDefinitions(); + foreach ($entity_types as $entity_type_id => $entity_type_definition) { + $this->rebuildEntityTypeDisplays($entity_type_id); + } + + } + + /** + * Rebuild displays for single Entity Type. + * + * @param string $entity_type_id + * The entity type machine name. + */ + public function rebuildEntityTypeDisplays($entity_type_id) { + // Loop through all bundles. + $entity_bundles = entity_get_bundles($entity_type_id); // @todo: inject service here. + foreach ($entity_bundles as $bundle => $bundle_definition) { + + // Get the displays. + $view_modes = $this->entityDisplayRepository->getViewModes($entity_type_id); + $form_modes = $this->entityDisplayRepository->getFormModes($entity_type_id); + // Explicitly add the default displays. + $view_modes['default'] = ['status' => TRUE]; + $form_modes['default'] = ['status' => TRUE]; + + // Update view modes displays. + foreach ($view_modes as $view_mode_id => $view_mode) { + if ($display = EntityViewDisplay::load($entity_type_id . '.' . $bundle . '.' . $view_mode_id)) { + $display->save(); + } + } + // Update form modes displays. + foreach ($form_modes as $form_mode_id => $form_mode) { + if ($form_display = EntityFormDisplay::load($entity_type_id . '.' . $bundle . '.' . $form_mode_id)) { + $form_display->save(); + } + } + + } + + } + +}