Skip to content

Commit

Permalink
SearchKit - Only add filter values to Afform title if passed internally
Browse files Browse the repository at this point in the history
This narrows the scope of contextual titles from civicrm#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.
  • Loading branch information
colemanw committed Jun 16, 2022
1 parent 084274a commit f3a2133
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
3 changes: 1 addition & 2 deletions ext/afform/mock/ang/testContactEmailSearchForm.aff.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<div af-fieldset="">
<af-field name="source" />
<af-field name="contact_type" />
<div class="af-container af-layout-inline">
<af-field name="Contact_Email_contact_id_01.email" />
<af-field name="Contact_Email_contact_id_01.location_type_id" defn="{input_attrs: {multiple: true}}" />
</div>
<crm-search-display-table filters="{last_name: 'AfformTest'}" search-name="TestContactEmailSearch" display-name="TestContactEmailDisplay"></crm-search-display-table>
<crm-search-display-table filters="{last_name: 'AfformTest', contact_type: dummy_var}" search-name="TestContactEmailSearch" display-name="TestContactEmailDisplay"></crm-search-display-table>
</div>
40 changes: 28 additions & 12 deletions ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,9 @@ protected function formatViewValue($key, $rawValue) {
*/
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']);
Expand All @@ -793,7 +794,8 @@ protected function applyFilters() {
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);
}
}
Expand Down Expand Up @@ -1051,22 +1053,36 @@ private function hasValue($value) {
}

/**
* 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] === '{') {
Expand Down

0 comments on commit f3a2133

Please sign in to comment.