From debab304437beb83f1db0179d83e77e49cf18070 Mon Sep 17 00:00:00 2001
From: Jared Whiklo <jwhiklo@gmail.com>
Date: Wed, 14 Sep 2022 15:51:31 -0500
Subject: [PATCH] Clear the correct cache and fix coder issues

---
 src/Form/IslandoraSettingsForm.php | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php
index 9da34adf..5049ef16 100644
--- a/src/Form/IslandoraSettingsForm.php
+++ b/src/Form/IslandoraSettingsForm.php
@@ -357,22 +357,23 @@ class IslandoraSettingsForm extends ConfigFormBase {
   }
 
   /**
-   * Removes the Fedora Pseudo field from any entity bundles that have be unselected.
+   * Removes the Fedora URI field from entity bundles that have be unselected.
    *
    * @param array $current_config
-   *   The current set of entity type & bundle to have the pseudo field, format {bundle}:{entity_type}
+   *   The current set of entity types & bundles to have the pseudo field,
+   *   format {bundle}:{entity_type}.
    * @param array $new_config
-   *   The new set of entity type & bundle to have the pseudo field, format {bundle}:{entity_type}
+   *   The new set of entity types & bundles to have the pseudo field, format
+   *   {bundle}:{entity_type}.
    *
    * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
    * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
    * @throws \Drupal\Core\Entity\EntityStorageException
    */
   private function updateEntityViewConfiguration(array $current_config, array $new_config) {
-    $cache_tags = [];
     $removed = array_diff($current_config, $new_config);
+    $added = array_diff($new_config, $current_config);
     $entity_view_display = $this->entityTypeManager->getStorage('entity_view_display');
-    // types being removed
     foreach ($removed as $bundle_type) {
       [$bundle, $type_id] = explode(":", $bundle_type);
       $results = $entity_view_display->getQuery()
@@ -384,10 +385,13 @@ class IslandoraSettingsForm extends ConfigFormBase {
       foreach ($entities as $entity) {
         $entity->removeComponent(self::GEMINI_PSEUDO_FIELD);
         $entity->save();
-        $cache_tags = array_merge($cache_tags, $entity->getCacheTags());
       }
     }
-    $entity_view_display->resetCache();
-    Cache::invalidateTags($cache_tags);
+    if (count($removed) > 0 || count($added) > 0) {
+      // If we added or cleared a type then clear the extra_fields cache.
+      // @see Drupal/Core/Entity/EntityFieldManager::getExtraFields
+      Cache::invalidateTags(["entity_field_info"]);
+    }
   }
+
 }