Skip to content

Commit

Permalink
APIv3 - Improve entityRef search for Afform (and other non-db entitie…
Browse files Browse the repository at this point in the history
…s) by supporting operators

This borrows APIv4 code to support operators in `_civicrm_api3_basic_array_get`,
which makes it possible to have searchable entityRef widgets for Afforms,
which allows Afforms to be selected for use in WP shortcodes.
  • Loading branch information
colemanw committed Feb 28, 2021
1 parent 2791073 commit 934cd39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Civi/Api4/Generic/Traits/ArrayQueryActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function walkFilters($row, $filters) {
return $result;

default:
return $this->filterCompare($row, $filters);
return self::filterCompare($row, $filters);
}
}

Expand All @@ -104,7 +104,7 @@ private function walkFilters($row, $filters) {
* @return bool
* @throws \Civi\API\Exception\NotImplementedException
*/
private function filterCompare($row, $condition) {
public static function filterCompare($row, $condition) {
if (!is_array($condition)) {
throw new NotImplementedException('Unexpected where syntax; expecting array.');
}
Expand Down
13 changes: 12 additions & 1 deletion api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,17 @@ function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $filt

$matches = [];

$isMatch = function($record, $key, $searchValue) {
if (is_array($searchValue) && count($searchValue) === 1 && in_array(array_keys($searchValue)[0], CRM_Core_DAO::acceptedSQLOperators())) {
$operator = array_keys($searchValue)[0];
$searchValue = array_values($searchValue)[0];
return \Civi\Api4\Generic\Traits\ArrayQueryActionTrait::filterCompare($record, [$key, $operator, $searchValue]);
}
// Prior to supporting multiple search operators this function used the sloppy == comparison
// So for backward compatibility we'll continue to use it when no operator is explicitly given
return $searchValue == ($record[$key] ?? NULL);
};

$currentOffset = 0;
foreach ($records as $record) {
if ($idCol != 'id') {
Expand All @@ -2488,7 +2499,7 @@ function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $filt
if ($k == 'id') {
$k = $idCol;
}
if (in_array($k, $filterableFields) && $record[$k] != $v) {
if (in_array($k, $filterableFields) && !$isMatch($record, $k, $v)) {
$match = FALSE;
break;
}
Expand Down
4 changes: 3 additions & 1 deletion ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ function afform_civicrm_preProcess($formName, &$form) {
'key' => 'name',
'entity' => 'Afform',
'select' => ['minimumInputLength' => 0],
'api' => [],
'api' => [
'params' => ['type' => ['IN' => ['form', 'search']]],
],
],
];
}
Expand Down

0 comments on commit 934cd39

Please sign in to comment.