From 88620a599c1386daa8ecc874b0e83f3356128d45 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 2 Jun 2021 15:09:10 -0400 Subject: [PATCH] APIv4 - Fix getFields to respect default_value from getFields GetFields had an odd way of setting defaults for field metadata; this reuses the default_value property instead for more internal consistency. --- CRM/Core/DAO/CustomGroup.php | 3 ++- Civi/Api4/Generic/BasicGetFieldsAction.php | 20 +++++++++++--------- xml/schema/Core/CustomGroup.xml | 1 + 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CRM/Core/DAO/CustomGroup.php b/CRM/Core/DAO/CustomGroup.php index bc97e05434..3798bb95dd 100644 --- a/CRM/Core/DAO/CustomGroup.php +++ b/CRM/Core/DAO/CustomGroup.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Core/CustomGroup.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:fc2cf4a8b27ca8d4eaced3f1a3fcddd5) + * (GenCodeChecksum:ddee40c6f144396056437d29bc0337d7) */ /** @@ -437,6 +437,7 @@ class CRM_Core_DAO_CustomGroup extends CRM_Core_DAO { 'html' => [ 'label' => ts("Table Name"), ], + 'readonly' => TRUE, 'add' => '2.0', ], 'is_multiple' => [ diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 6aee1082f2..2a134d4fb7 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -111,7 +111,8 @@ class BasicGetFieldsAction extends BasicGetAction { * @param array $values */ protected function formatResults(&$values) { - $fields = array_column($this->fields(), 'name'); + $fieldDefaults = array_column($this->fields(), 'default_value', 'name') + + array_fill_keys(array_column($this->fields(), 'name'), NULL); // Enforce field permissions if ($this->checkPermissions) { foreach ($values as $key => $field) { @@ -124,17 +125,15 @@ class BasicGetFieldsAction extends BasicGetAction { $defaults = array_intersect_key([ 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])), 'entity' => $this->getEntityName(), - 'required' => FALSE, - 'readonly' => FALSE, 'options' => !empty($field['pseudoconstant']), - 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'), - ], array_flip($fields)); - $field += $defaults; - $field['label'] = $field['label'] ?? $field['title']; + ], $fieldDefaults); + $field += $defaults + $fieldDefaults; + if (array_key_exists('label', $fieldDefaults)) { + $field['label'] = $field['label'] ?? $field['title'] ?? $field['name']; + } if (isset($defaults['options'])) { $field['options'] = $this->formatOptionList($field['options']); } - $field += array_fill_keys($fields, NULL); } } @@ -261,6 +260,7 @@ class BasicGetFieldsAction extends BasicGetAction { [ 'name' => 'required', 'data_type' => 'Boolean', + 'default_value' => FALSE, ], [ 'name' => 'required_if', @@ -269,10 +269,11 @@ class BasicGetFieldsAction extends BasicGetAction { [ 'name' => 'options', 'data_type' => 'Array', + 'default_value' => FALSE, ], [ 'name' => 'data_type', - 'data_type' => 'String', + 'default_value' => 'String', 'options' => [ 'Array' => ts('Array'), 'Boolean' => ts('Boolean'), @@ -319,6 +320,7 @@ class BasicGetFieldsAction extends BasicGetAction { 'name' => 'readonly', 'data_type' => 'Boolean', 'description' => 'True for auto-increment, calculated, or otherwise non-editable fields.', + 'default_value' => FALSE, ], [ 'name' => 'output_formatters', diff --git a/xml/schema/Core/CustomGroup.xml b/xml/schema/Core/CustomGroup.xml index 727da9205c..5fec960339 100644 --- a/xml/schema/Core/CustomGroup.xml +++ b/xml/schema/Core/CustomGroup.xml @@ -147,6 +147,7 @@ + true 2.0 -- 2.25.1