From cc744c7e8dc9db28d157fc7958f52a2010a42d22 Mon Sep 17 00:00:00 2001
From: Marcus Johansson <25670-Marcus_Johansson@users.noreply.drupalcode.org>
Date: Mon, 22 Jan 2024 10:59:11 +0100
Subject: [PATCH] Issue #3416190 by Marcus_Johansson: Duplication of
 configuration items for status on config-imort

---
 ai_interpolator.module | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/ai_interpolator.module b/ai_interpolator.module
index 5910ef6..e817c20 100644
--- a/ai_interpolator.module
+++ b/ai_interpolator.module
@@ -36,7 +36,8 @@ function ai_interpolator_entity_presave(EntityInterface $entity) {
  * Implements hook_entity_insert().
  */
 function ai_interpolator_entity_insert(EntityInterface $entity) {
-  if ($entity instanceof FieldConfig) {
+  // Never generate on import since that will be done via the importer.
+  if ($entity instanceof FieldConfig && _ai_interpolator_can_import_toggle()) {
     \Drupal::service('ai_interpolator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
   }
   if (_ai_interpolator_entity_can_save_toggle()) {
@@ -48,7 +49,7 @@ function ai_interpolator_entity_insert(EntityInterface $entity) {
  * Implements hook_entity_update().
  */
 function ai_interpolator_entity_update(EntityInterface $entity) {
-  if ($entity instanceof FieldConfig) {
+  if ($entity instanceof FieldConfig && _ai_interpolator_can_import_toggle()) {
     \Drupal::service('ai_interpolator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
   }
   if (_ai_interpolator_entity_can_save_toggle()) {
@@ -60,11 +61,20 @@ function ai_interpolator_entity_update(EntityInterface $entity) {
  * Implements hook_entity_delete().
  */
 function ai_interpolator_entity_delete(EntityInterface $entity) {
-  if ($entity instanceof FieldConfig && _ai_interpolator_entity_can_save_toggle()) {
+  if ($entity instanceof FieldConfig && _ai_interpolator_entity_can_save_toggle() && _ai_interpolator_can_import_toggle()) {
     \Drupal::service('ai_interpolator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
   }
 }
 
+/**
+ * Implements hook_entity_insert().
+ */
+function ai_interpolator_config_import_steps_alter(&$context) {
+  // We disable generation from entities while importing, so we don't have a
+  // mis-match of configurations.
+  _ai_interpolator_can_import_toggle(FALSE);
+}
+
 /**
  * Global function to toggle or check if to save.
  *
@@ -84,3 +94,23 @@ function _ai_interpolator_entity_can_save_toggle($newToggle = NULL) {
   }
   return $toggle;
 }
+
+/**
+ * Global function to toggle if import is possible.
+ *
+ * @param bool $newToggle
+ *   The new toggle if you want to change it.
+ *
+ * @return bool
+ *   If it is possible to import or not.
+ */
+function _ai_interpolator_can_import_toggle($newToggle = NULL) {
+  static $toggle;
+  // Set default to TRUE.
+  // @codingStandardsIgnoreLine
+  $toggle = $toggle === NULL ? TRUE : $toggle;
+  if (isset($newToggle)) {
+    $toggle = $newToggle;
+  }
+  return $toggle;
+}
-- 
GitLab