From f3a213378650a09d1ca9cf887e31edb36973fa00 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 15 Jun 2022 17:49:51 -0400 Subject: [PATCH] SearchKit - Only add filter values to Afform title if passed internally This narrows the scope of contextual titles from #22319 to only those filters passed internally, as used in drilldown forms. It makes sense to exclude values from exposed filters because that can lead to a confusing UI. --- .../ang/testContactEmailSearchForm.aff.html | 3 +- .../SearchDisplay/AbstractRunAction.php | 40 +++++++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ext/afform/mock/ang/testContactEmailSearchForm.aff.html b/ext/afform/mock/ang/testContactEmailSearchForm.aff.html index 390be362f6..90de6fee16 100644 --- a/ext/afform/mock/ang/testContactEmailSearchForm.aff.html +++ b/ext/afform/mock/ang/testContactEmailSearchForm.aff.html @@ -1,9 +1,8 @@
-
- +
diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index aaf693fb9d..ade9286e26 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -779,8 +779,9 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { */ protected function applyFilters() { // Allow all filters that are included in SELECT clause or are fields on the Afform. - $afformFilters = $this->getAfformFilters(); - $allowedFilters = array_merge($this->getSelectAliases(), $afformFilters); + $fieldFilters = $this->getAfformFilterFields(); + $directiveFilters = $this->getAfformDirectiveFilters(); + $allowedFilters = array_merge($this->getSelectAliases(), $fieldFilters, $directiveFilters); // Ignore empty strings $filters = array_filter($this->filters, [$this, 'hasValue']); @@ -793,7 +794,8 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { if (in_array($key, $allowedFilters, TRUE) || !array_diff($fieldNames, $allowedFilters)) { $this->applyFilter($fieldNames, $value); } - if (in_array($key, $afformFilters, TRUE)) { + // Filter labels are used to set the page title for drilldown forms + if (in_array($key, $directiveFilters, TRUE)) { $this->addFilterLabel($key, $value); } } @@ -1051,22 +1053,36 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { } /** - * Returns a list of filter fields and directive filters + * Returns a list of afform fields used as search filters * - * Automatically applies directive filters + * Limited to the current display * - * @return array + * @return string[] + */ + private function getAfformFilterFields() { + $afform = $this->loadAfform(); + if ($afform) { + return array_column(\CRM_Utils_Array::findAll( + $afform['searchDisplay']['fieldset'], + ['#tag' => 'af-field'] + ), 'name'); + } + return []; + } + + /** + * Finds all directive filters and applies the ones with a literal value + * + * Returns the list of filters that did not get auto-applied (value was passed via js) + * + * @return string[] */ - private function getAfformFilters() { + private function getAfformDirectiveFilters() { $afform = $this->loadAfform(); if (!$afform) { return []; } - // Get afform field filters - $filterKeys = array_column(\CRM_Utils_Array::findAll( - $afform['searchDisplay']['fieldset'], - ['#tag' => 'af-field'] - ), 'name'); + $filterKeys = []; // Get filters passed into search display directive from Afform markup $filterAttr = $afform['searchDisplay']['filters'] ?? NULL; if ($filterAttr && is_string($filterAttr) && $filterAttr[0] === '{') { -- 2.25.1