Skip to content

Commit

Permalink
SearchKit - Use correct action for getFields for in-place edit
Browse files Browse the repository at this point in the history
Adds some caching to that function since it's called for every row
  • Loading branch information
colemanw committed Aug 23, 2023
1 parent 2b6a405 commit 1090ecc
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction {
*/
private $entityActions;

private $editableInfo = [];

/**
* Override execute method to change the result object type
* @return \Civi\Api4\Result\SearchDisplayRunResult
Expand Down Expand Up @@ -814,17 +816,29 @@ private function fieldBelongsToEntity($entityName, $fieldName, $entityValues, $c
* @return array{entity: string, input_type: string, data_type: string, options: bool, serialize: bool, nullable: bool, fk_entity: string, value_key: string, value_path: string, id_key: string, id_path: string, explicit_join: string, grouping_fields: array}|null
*/
private function getEditableInfo($key) {
$result = NULL;
if (array_key_exists($key, $this->editableInfo)) {
return $this->editableInfo[$key];
}
// Strip pseudoconstant suffix
[$key] = explode(':', $key);
$field = $this->getField($key);
// If field is an implicit join to another entity (not a custom group), use the original fk field
if (!empty($field['implicit_join']) && empty($field['custom_field_id'])) {
return $this->getEditableInfo(substr($key, 0, -1 - strlen($field['name'])));
}
$result = NULL;
if ($field) {
// Reload field with correct action because `$this->getField()` uses 'get' as the action
// TODO: Load options if pseudoconstant is dynamic (`ControlField` present)
$createModeField = civicrm_api4($field['entity'], 'getFields', [
'where' => [['name', '=', $field['name']]],
'checkPermissions' => FALSE,
'action' => 'create',
])->first() ?? [];
// Merge with the augmented metadata like `explicit_join`
$field = $createModeField + $field;
$idKey = CoreUtil::getIdFieldName($field['entity']);
$path = ($field['explicit_join'] ? $field['explicit_join'] . '.' : '');
$path = (!empty($field['explicit_join']) ? $field['explicit_join'] . '.' : '');
$idPath = $path . $idKey;
// Hack to support editing relationships
if ($field['entity'] === 'RelationshipCache') {
Expand Down Expand Up @@ -855,7 +869,7 @@ private function getEditableInfo($key) {
}
}
}
return $result;
return $this->editableInfo[$key] = $result;
}

/**
Expand Down

0 comments on commit 1090ecc

Please sign in to comment.