From: colemanw Date: Thu, 29 Feb 2024 22:03:14 +0000 (-0500) Subject: SearchKit - Fix enable/disable links in displays X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=42008e8bd8ab37c609973e3c10750d5ffd88be35;p=civicrm-core.git SearchKit - Fix enable/disable links in displays This fixes task-based links (enable, disable, etc) when used as buttons, dropdowns, etc. in search displays --- diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index ccc7cf20bc..cce62e357c 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -554,17 +554,17 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { return NULL; } $link['text'] = $text ?? $this->replaceTokens($link['text'], $data, 'view'); - // Will return null if `$link[path]` is empty or if any tokens do not resolve - $path = $this->replaceTokens($link['path'], $data, 'url'); - if ($path) { - $link['url'] = $this->getUrl($path); - $keys = ['url', 'text', 'title', 'target', 'icon', 'style', 'autoOpen']; - } - elseif (!empty($link['task'])) { + if (!empty($link['task'])) { $keys = ['task', 'text', 'title', 'icon', 'style']; } else { - return NULL; + $path = $this->replaceTokens($link['path'], $data, 'url'); + if (!$path) { + // Return null if `$link[path]` is empty or if any tokens do not resolve + return NULL; + } + $link['url'] = $this->getUrl($path); + $keys = ['url', 'text', 'title', 'target', 'icon', 'style', 'autoOpen']; } $link = array_intersect_key($link, array_flip($keys)); return array_filter($link, function($value) { @@ -793,7 +793,7 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { $data = \CRM_Utils_JS::getRawProps($task['crmPopup']['data']); // Find the special key that combines selected ids and replace it with id token $idsKey = array_search("ids.join(',')", $data); - unset($data[$idsKey]); + unset($data[$idsKey], $link['task']); $amp = strpos($link['path'], '?') ? '&' : '?'; $link['path'] .= $amp . $idField . '=[' . $link['prefix'] . $idKey . ']'; // Add the rest of the data items diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 33a88ab3aa..d2637ea2a2 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -391,15 +391,74 @@ class SearchRunTest extends Api4TestBase implements TransactionalInterface { $this->assertEquals('fa-external-link', $result[0]['columns'][1]['links'][0]['icon']); // 2nd link is to the native SK bulk-update task $this->assertArrayNotHasKey('url', $result[0]['columns'][1]['links'][1]); + $this->assertArrayNotHasKey('action', $result[0]['columns'][1]['links'][1]); $this->assertEquals('update', $result[0]['columns'][1]['links'][1]['task']); $this->assertEquals('fa-pencil', $result[0]['columns'][1]['links'][1]['icon']); // 3rd link is a popup link to the delete contribution quickform + $this->assertArrayNotHasKey('task', $result[0]['columns'][1]['links'][2]); $this->assertStringContainsString('action=delete&id=' . $contributions[0]['id'], $result[0]['columns'][1]['links'][2]['url']); $this->assertEquals('crm-popup', $result[0]['columns'][1]['links'][2]['target']); $this->assertEquals('fa-trash', $result[0]['columns'][1]['links'][2]['icon']); $this->assertEquals('Delete', $result[0]['columns'][1]['links'][2]['title']); } + public function testEnableDisableTaskLinks():void { + $contributionPage = $this->createTestRecord('ContributionPage'); + $params = [ + 'checkPermissions' => FALSE, + 'return' => 'page:1', + 'savedSearch' => [ + 'api_entity' => 'ContributionPage', + 'api_params' => [ + 'version' => 4, + 'select' => ['title'], + 'where' => [['id', '=', $contributionPage['id']]], + ], + ], + 'display' => [ + 'type' => 'table', + 'label' => 'testDisplay', + 'settings' => [ + 'actions' => TRUE, + 'pager' => [], + 'columns' => [ + [ + 'key' => 'title', + 'label' => 'Title', + 'dataType' => 'String', + 'type' => 'field', + ], + [ + 'type' => 'buttons', + 'links' => [ + [ + 'entity' => 'ContributionPage', + 'task' => 'enable', + 'icon' => 'fa-pencil', + ], + [ + 'entity' => 'ContributionPage', + 'task' => 'disable', + 'icon' => 'fa-pencil', + ], + ], + ], + ], + 'sort' => [ + ['id', 'ASC'], + ], + ], + ], + ]; + $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertEquals(1, $result->count()); + // Native SK tasks should not have a url + $this->assertArrayNotHasKey('url', $result[0]['columns'][1]['links'][1]); + $this->assertArrayNotHasKey('action', $result[0]['columns'][1]['links'][1]); + $this->assertEquals('disable', $result[0]['columns'][1]['links'][1]['task']); + $this->assertEquals('fa-pencil', $result[0]['columns'][1]['links'][1]['icon']); + } + public function testRelationshipCacheLinks():void { $case = $this->createTestRecord('Case'); $relationships = $this->saveTestRecords('Relationship', [