From: colemanw Date: Tue, 19 Dec 2023 15:33:41 +0000 (-0500) Subject: SearchKit - Preprocess links only once; use api getLinks action X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=80e5ab8752b8e5a1002427cf51acd7f3dbd66dc2;p=civicrm-core.git SearchKit - Preprocess links only once; use api getLinks action Before: Links were being preprocessed by the getLinkInfo() function once-per-link-per-row. After: Links preprocessed once-per-link. New API action used. --- diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index 32d00aaead..936eae620e 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -508,7 +508,6 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { } protected function formatLink(array $link, array $data, string $text = NULL, $index = 0): ?array { - $link = $this->getLinkInfo($link); if (!$this->checkLinkAccess($link, $data, $index)) { return NULL; } @@ -663,10 +662,30 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { } /** - * @param array{path: string, entity: string, action: string, task: string, join: string, target: string, style: string, title: string, text: string} $link - * @return array{path: string, entity: string, action: string, task: string, join: string, target: string, style: string, title: string, text: string, prefix: string} + * Fills in info about each link in the search display. */ - private function getLinkInfo(array $link): array { + protected function preprocessLinks(): void { + foreach ($this->display['settings']['columns'] as &$column) { + if (!empty($column['link'])) { + $this->preprocessLink($column['link']); + } + if (!empty($column['links'])) { + foreach ($column['links'] as &$link) { + $this->preprocessLink($link); + } + } + } + if (!empty($this->display['settings']['toolbar'])) { + foreach ($this->display['settings']['toolbar'] as &$button) { + $this->preprocessLink($button); + } + } + } + + /** + * @param array{path: string, entity: string, action: string, task: string, join: string, target: string, style: string, title: string, text: string, prefix: string} $link + */ + private function preprocessLink(array &$link): void { $link += [ 'path' => '', 'target' => '', @@ -687,8 +706,15 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { $link['prefix'] = $link['join'] . '.'; } // Get path from action - if (!$link['path'] && !empty($link['action'])) { - $link['path'] = CoreUtil::getInfoItem($entity, 'paths')[$link['action']] ?? NULL; + if (!empty($link['action'])) { + $getLinks = civicrm_api4($entity, 'getLinks', [ + 'checkPermissions' => FALSE, + 'where' => [ + ['ui_action', '=', $link['action']], + ], + ]); + $link['path'] = $getLinks[0]['path'] ?? NULL; + $link['entity'] = $getLinks[0]['entity'] ?? NULL; // This is a bit clunky, the function_join_field gets un-munged later by $this->getJoinFromAlias() if ($this->canAggregate($link['prefix'] . $idKey)) { $link['prefix'] = 'GROUP_CONCAT_' . str_replace('.', '_', $link['prefix']); @@ -724,7 +750,6 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { } $link['key'] = $link['prefix'] . $idKey; } - return $link; } /** @@ -734,7 +759,6 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { * @return array */ private function getLinkTokens(array $link): array { - $link = $this->getLinkInfo($link); $tokens = []; if (!$link['path'] && !empty($link['task'])) { $tokens[] = $link['prefix'] . $this->getIdKeyName($link['entity']); diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php index 7c6e9d8b5a..01d587fa22 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php @@ -44,6 +44,7 @@ class Run extends AbstractRunAction { // Pager can operate in "page" mode for traditional pager, or "scroll" mode for infinite scrolling $pagerMode = NULL; + $this->preprocessLinks(); $this->augmentSelectClause($apiParams); $this->applyFilters();