SearchKit - fix rendering default display embedded in an afform
authorColeman Watts <coleman@civicrm.org>
Sun, 21 Nov 2021 17:33:06 +0000 (12:33 -0500)
committerColeman Watts <coleman@civicrm.org>
Sun, 21 Nov 2021 17:33:06 +0000 (12:33 -0500)
ext/search_kit/Civi/Search/AfformSearchMetadataInjector.php

index f99bfdc29aa60a8a4fbc7c4d8f00e4449ddc3bbb..98244fe9d74e2b12e62426b76ce71ecf6099c7b6 100644 (file)
@@ -32,10 +32,20 @@ class AfformSearchMetadataInjector {
           foreach (pq(implode(',', $displayTags), $doc) as $component) {
             $searchName = pq($component)->attr('search-name');
             $displayName = pq($component)->attr('display-name');
-            if ($searchName && $displayName) {
-              $display = \Civi\Api4\SearchDisplay::get(FALSE)
-                ->addWhere('name', '=', $displayName)
-                ->addWhere('saved_search_id.name', '=', $searchName)
+            if ($searchName) {
+              // Fetch search display if name is provided
+              if (is_string($displayName) && strlen($displayName)) {
+                $searchDisplayGet = \Civi\Api4\SearchDisplay::get(FALSE)
+                  ->addWhere('name', '=', $displayName)
+                  ->addWhere('saved_search_id.name', '=', $searchName);
+              }
+              // Fall-back to the default display
+              else {
+                $displayName = NULL;
+                $searchDisplayGet = \Civi\Api4\SearchDisplay::getDefault(FALSE)
+                  ->setSavedSearch($searchName);
+              }
+              $display = $searchDisplayGet
                 ->addSelect('settings', 'saved_search_id.api_entity', 'saved_search_id.api_params')
                 ->execute()->first();
               if ($display) {