Merge pull request #20481 from colemanw/getFieldsFix
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 2 Jun 2021 20:59:52 +0000 (08:59 +1200)
committerGitHub <noreply@github.com>
Wed, 2 Jun 2021 20:59:52 +0000 (08:59 +1200)
APIv4 - Fix getFields to respect default_value from getFields

CRM/Core/DAO/CustomGroup.php
Civi/Api4/Generic/BasicGetFieldsAction.php
xml/schema/Core/CustomGroup.xml

index bc97e05434c013032e449c922bca214c787c09a1..3798bb95dd7c45693689ef8b291e7d97748ced64 100644 (file)
@@ -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' => [
index 6aee1082f21298ca1d06d45e62d32bdee3e49802..2a134d4fb73cc756de8b64079f22cae16b8e19ef 100644 (file)
@@ -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',
index 727da9205c8272aa8e78050aed83ad949e32c47e..5fec9603395afbb096dfd5a10cc71a88e06f0e92 100644 (file)
     <html>
       <label>Table Name</label>
     </html>
+    <readonly>true</readonly>
     <add>2.0</add>
   </field>
   <field>