From 46fb47e78b702366cf45d707899a74207a29b7c0 Mon Sep 17 00:00:00 2001
From: Dane Powell <git@danepowell.com>
Date: Tue, 24 Oct 2017 14:11:48 -0700
Subject: [PATCH] Issue #2915036 by Dane Powell: Fixed view modes not updating
 in response to new fields.

---
 core/modules/field/field.module | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/core/modules/field/field.module b/core/modules/field/field.module
index 1a18b91d73..6c9b6b07e7 100644
--- a/core/modules/field/field.module
+++ b/core/modules/field/field.module
@@ -347,6 +347,40 @@ 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.
+ */
+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();
+
+  $view_modes = \Drupal::entityManager()->getViewModes($entity_type_id);
+  $form_modes = \Drupal::entityManager()->getFormModes($entity_type_id);
+
+  // Update view modes.
+  foreach ($view_modes as $view_mode_id => $view_mode) {
+    if ($view_mode['status']) {
+      entity_get_display($entity_type_id, $bundle, $view_mode_id)
+        ->save();
+    }
+  }
+
+  // Update form modes.
+  foreach ($form_modes as $form_mode_id => $form_mode) {
+    if ($form_mode['status']) {
+      entity_get_form_display($entity_type_id, $bundle, $form_mode_id)
+        ->save();
+    }
+  }
+}
+
 /**
  * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
  *
@@ -379,6 +413,7 @@ function field_field_storage_config_update(FieldStorageConfigInterface $field_st
       $field->save();
     }
   }
+
 }
 
 /**
-- 
2.14.1

