SearchKit - Provide context information to subscribers
authorcolemanw <coleman@civicrm.org>
Mon, 3 Jul 2023 19:22:09 +0000 (15:22 -0400)
committercolemanw <coleman@civicrm.org>
Mon, 3 Jul 2023 19:22:09 +0000 (15:22 -0400)
Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php
ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php

index ce6cbb325b46569ed420a551cf2699aac3fa4caa..fbef3dffb9241c62010a37b556cff22fb5fa5598 100644 (file)
@@ -87,6 +87,11 @@ trait SavedSearchInspectorTrait {
       $this->display = \Civi\Api4\SearchDisplay::getDefault(FALSE)
         ->addSelect('*', 'type:name')
         ->setSavedSearch($this->savedSearch)
+        ->setContext([
+          'filters' => $this->filters ?? NULL,
+          'formName' => $this->formName ?? NULL,
+          'fieldName' => $this->fieldName ?? NULL,
+        ])
         // Set by AutocompleteAction
         ->setType($this->_displayType ?? 'table')
         ->execute()->first();
index 4e618dc0565c15da2adb486b0ad85cc21505b5fe..994e2018ec65cf0bc4e3451a40cebafbbd5a36c9 100644 (file)
@@ -16,10 +16,14 @@ use Civi\Api4\Query\SqlFunctionGROUP_CONCAT;
 use Civi\Api4\Utils\CoreUtil;
 
 /**
- * Return the default results table for a saved search.
+ * Generate the default display for a saved search.
+ *
+ * Dispatches `civi.search.defaultDisplay` event to allow subscribers to provide a display based on context.
  *
  * @method $this setType(string $type)
  * @method string getType()
+ * @method $this setContext(array $context)
+ * @method array getContext()
  * @package Civi\Api4\Action\SearchDisplay
  */
 class GetDefault extends \Civi\Api4\Generic\AbstractAction {
@@ -40,6 +44,12 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction {
    */
   protected $type = 'table';
 
+  /**
+   * Provide context information; passed through to `civi.search.defaultDisplay` subscribers
+   * @var array
+   */
+  protected $context = [];
+
   /**
    * @var array
    */
@@ -60,6 +70,7 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction {
     if (!strlen($label) && !empty($this->savedSearch['api_entity'])) {
       $label = CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'title_plural');
     }
+    // Initialize empty display
     $display = [
       'id' => NULL,
       'name' => NULL,
@@ -76,6 +87,7 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction {
       'savedSearch' => $this->savedSearch,
       'display' => &$display,
       'apiAction' => $this,
+      'context' => $this->context,
     ]));
 
     $fields = $this->entityFields();