From b6b6cb2d4f3f386ded0a44930756deba18574e2b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Aug 2020 11:33:09 -0400 Subject: [PATCH] Expose field label to APIv4 and Search creaor --- Civi/Api4/Generic/BasicGetFieldsAction.php | 9 ++++++++ Civi/Api4/Service/Spec/FieldSpec.php | 23 ++++++++++++++++++++ Civi/Api4/Service/Spec/SpecFormatter.php | 1 + ext/search/CRM/Search/Page/Ang.php | 4 ++-- ext/search/ang/search/crmSearch.component.js | 4 ++-- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index ec58714fe4..5f5d25ef3d 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -129,6 +129,7 @@ class BasicGetFieldsAction extends BasicGetAction { 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'), ], array_flip($fields)); $field += $defaults; + $field['label'] = $field['label'] ?? $field['title']; if (isset($defaults['options'])) { $field['options'] = $this->formatOptionList($field['options']); } @@ -217,14 +218,22 @@ class BasicGetFieldsAction extends BasicGetAction { [ 'name' => 'name', 'data_type' => 'String', + 'description' => ts('Unique field identifier'), ], [ 'name' => 'title', 'data_type' => 'String', + 'description' => ts('Technical name of field, shown in API and exports'), + ], + [ + 'name' => 'label', + 'data_type' => 'String', + 'description' => ts('User-facing label, shown on most forms and displays'), ], [ 'name' => 'description', 'data_type' => 'String', + 'description' => ts('Explanation of the purpose of the field'), ], [ 'name' => 'default_value', diff --git a/Civi/Api4/Service/Spec/FieldSpec.php b/Civi/Api4/Service/Spec/FieldSpec.php index 8b9ba5ca24..eb35cf1d30 100644 --- a/Civi/Api4/Service/Spec/FieldSpec.php +++ b/Civi/Api4/Service/Spec/FieldSpec.php @@ -32,6 +32,11 @@ class FieldSpec { */ protected $name; + /** + * @var string + */ + protected $label; + /** * @var string */ @@ -165,6 +170,24 @@ class FieldSpec { return $this; } + /** + * @return string + */ + public function getLabel() { + return $this->label; + } + + /** + * @param string $label + * + * @return $this + */ + public function setLabel($label) { + $this->label = $label; + + return $this; + } + /** * @return string */ diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index 1da26d1520..23209a4e24 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -73,6 +73,7 @@ class SpecFormatter { $field = new FieldSpec($name, $entity, $dataTypeName); $field->setRequired(!empty($data['required'])); $field->setTitle($data['title'] ?? NULL); + $field->setLabel($data['html']['label'] ?? NULL); $field->setOptions(!empty($data['pseudoconstant'])); } $field->setSerialize($data['serialize'] ?? NULL); diff --git a/ext/search/CRM/Search/Page/Ang.php b/ext/search/CRM/Search/Page/Ang.php index a13d8297b5..069dc0ee97 100644 --- a/ext/search/CRM/Search/Page/Ang.php +++ b/ext/search/CRM/Search/Page/Ang.php @@ -66,7 +66,7 @@ class CRM_Search_Page_Ang extends CRM_Core_Page { ->setChain([ 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']], ])->execute(); - $getFields = ['name', 'title', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize']; + $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize']; foreach ($schema as $entity) { // Skip if entity doesn't have a 'get' action or the user doesn't have permission to use get if ($entity['get']) { @@ -78,7 +78,7 @@ class CRM_Search_Page_Ang extends CRM_Core_Page { $entity['fields'] = civicrm_api4($entity['name'], 'getFields', [ 'select' => $getFields, 'where' => [['permission', 'IS NULL']], - 'orderBy' => ['title'], + 'orderBy' => ['label'], 'loadOptions' => $loadOptions, ]); // Get the names of params this entity supports (minus some obvious ones) diff --git a/ext/search/ang/search/crmSearch.component.js b/ext/search/ang/search/crmSearch.component.js index 1695f95701..e2d8c564d1 100644 --- a/ext/search/ang/search/crmSearch.component.js +++ b/ext/search/ang/search/crmSearch.component.js @@ -297,7 +297,7 @@ this.getFieldLabel = function(col) { var info = searchMeta.parseExpr(col), - label = info.field.title; + label = info.field.label; if (info.fn) { label = '(' + info.fn.title + ') ' + label; } @@ -393,7 +393,7 @@ return _.transform(searchMeta.getEntity(entityName).fields, function(result, field) { var item = { id: prefix + field.name + (field.options ? suffix : ''), - text: field.title, + text: field.label, description: field.description }; if (disabledIf(item.id)) { -- 2.25.1