SearchKit - Use order_by from metadata to supply default sort for searchDisplays
authorColeman Watts <coleman@civicrm.org>
Fri, 26 Nov 2021 01:12:32 +0000 (20:12 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 29 Nov 2021 13:46:02 +0000 (08:46 -0500)
ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php
ext/search_kit/Civi/Search/Admin.php
ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayGrid.component.js
ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js
ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js

index 72bc09b69dfe91626ba1411cc79a29dd6728974a..cfdc81a2a07237102054186652f26ee8bf3d777e 100644 (file)
@@ -69,9 +69,17 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction {
           'show_count' => TRUE,
           'expose_limit' => TRUE,
         ],
+        'sort' => [],
         'columns' => [],
       ],
     ];
+    // Supply default sort if no orderBy given in api params
+    if (!empty($this->savedSearch['api_entity']) && empty($this->savedSearch['api_params']['orderBy'])) {
+      $defaultSort = CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'order_by');
+      if ($defaultSort) {
+        $display['settings']['sort'][] = [$defaultSort, 'ASC'];
+      }
+    }
     foreach ($this->getSelectClause() as $key => $clause) {
       $display['settings']['columns'][] = $this->configureColumn($clause, $key);
     }
index 01de7d95ae6c2e87642db87387b6c8d24b442bb2..345c6ce1a9e04e60958170ac2c726d163ab89114 100644 (file)
@@ -98,7 +98,7 @@ class Admin {
   public static function getSchema() {
     $schema = [];
     $entities = \Civi\Api4\Entity::get()
-      ->addSelect('name', 'title', 'title_plural', 'bridge_title', 'type', 'primary_key', 'description', 'label_field', 'icon', 'dao', 'bridge', 'ui_join_filters', 'searchable')
+      ->addSelect('name', 'title', 'title_plural', 'bridge_title', 'type', 'primary_key', 'description', 'label_field', 'icon', 'dao', 'bridge', 'ui_join_filters', 'searchable', 'order_by')
       ->addWhere('searchable', '!=', 'none')
       ->addOrderBy('title_plural')
       ->setChain([
index 3a55a7aab16868e8134fe63af284a6b1b8a92397..3273fafa1cd578a26c05a6a519897986cb56408a 100644 (file)
@@ -11,7 +11,7 @@
       parent: '^crmSearchAdminDisplay'
     },
     templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayGrid.html',
-    controller: function($scope) {
+    controller: function($scope, searchMeta) {
       var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
         ctrl = this;
 
           ctrl.display.settings = {
             colno: '3',
             limit: CRM.crmSearchAdmin.defaultPagerSize,
+            sort: [],
             pager: {}
           };
+          if (searchMeta.getEntity(ctrl.apiEntity).order_by) {
+            ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']);
+          }
         }
         ctrl.parent.initColumns({});
       };
index fe35e0a024fc9dbb97526f33c82e7edc11024a7b..de64dfb92c0a0f910d4ba85f4979bed37e9534b3 100644 (file)
@@ -11,7 +11,7 @@
       parent: '^crmSearchAdminDisplay'
     },
     templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayList.html',
-    controller: function($scope) {
+    controller: function($scope, searchMeta) {
       var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
         ctrl = this;
 
           ctrl.display.settings = {
             style: 'ul',
             limit: CRM.crmSearchAdmin.defaultPagerSize,
+            sort: [],
             pager: {}
           };
+          if (searchMeta.getEntity(ctrl.apiEntity).order_by) {
+            ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']);
+          }
         }
         ctrl.parent.initColumns({});
       };
index 78119bbad1b8bee0c3ac659459e5214a54b4eb76..55b4fbbd5fa86cb19ecadabab36137c44c11e2f6 100644 (file)
@@ -11,7 +11,7 @@
       parent: '^crmSearchAdminDisplay'
     },
     templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayTable.html',
-    controller: function($scope) {
+    controller: function($scope, searchMeta) {
       var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
         ctrl = this;
 
 
       this.$onInit = function () {
         if (!ctrl.display.settings) {
-          ctrl.display.settings = _.extend({}, CRM.crmSearchAdmin.defaultDisplay.settings, {columns: null});
+          ctrl.display.settings = _.extend({}, _.cloneDeep(CRM.crmSearchAdmin.defaultDisplay.settings), {columns: null});
+          if (searchMeta.getEntity(ctrl.apiEntity).order_by) {
+            ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']);
+          }
         }
         // Displays created prior to 5.43 may not have this property
         ctrl.display.settings.classes = ctrl.display.settings.classes || [];