From 9a4adcb9cdc9389358828899f4a94a0530d67aa4 Mon Sep 17 00:00:00 2001
From: "Giuseppe.Esposito" <giuseppe.esposito@sparkfabrik.com>
Date: Wed, 19 Mar 2025 17:30:57 +0100
Subject: [PATCH] fix: entityreference_filter_views_data_alter consider
 entityreference field on commerce entities

---
 entityreference_filter.views.inc | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/entityreference_filter.views.inc b/entityreference_filter.views.inc
index d93529b..22590be 100644
--- a/entityreference_filter.views.inc
+++ b/entityreference_filter.views.inc
@@ -12,7 +12,6 @@ use Drupal\field\Entity\FieldStorageConfig;
  * Implements hook_views_data_alter().
  */
 function entityreference_filter_views_data_alter(array &$data) {
-
   // Apply to entity reference fields, nid, uid, tid, gid and commerce
   // variations.
   foreach ($data as $table_name => $table_info) {
@@ -42,13 +41,21 @@ function entityreference_filter_views_data_alter(array &$data) {
           $target_entity_type = 'commerce_product';
         }
       }
-      // Other entityreference fields.
-      elseif (!empty($field_info['filter']['field_name']) && !empty($field_info['filter']['entity_type']) && mb_substr($field_name, -10) === '_target_id') {
-        $entity_type = $field_info['filter']['entity_type'];
-        $entity_field_name = $field_info['filter']['field_name'];
-        $field_storage_config = FieldStorageConfig::loadByName($entity_type, $entity_field_name);
-        if (!is_null($field_storage_config)) {
-          $target_entity_type = $field_storage_config->getSetting('target_type');
+
+      // Check for other entity reference fields.
+      // This check is separate from the previous conditions to handle cases
+      // where an outer condition is met but not an inner one.
+      // For example, when dealing with entity reference fields on commerce
+      // products that don't match the specific inner commerce
+      // product\variations conditions above.
+      if (!$target_entity_type) {
+        if (!empty($field_info['filter']['field_name']) && !empty($field_info['filter']['entity_type']) && mb_substr($field_name, -10) === '_target_id') {
+          $entity_type = $field_info['filter']['entity_type'];
+          $entity_field_name = $field_info['filter']['field_name'];
+          $field_storage_config = FieldStorageConfig::loadByName($entity_type, $entity_field_name);
+          if (!is_null($field_storage_config)) {
+            $target_entity_type = $field_storage_config->getSetting('target_type');
+          }
         }
       }
 
-- 
GitLab