APIv4 - Clarify specProvider code
authorcolemanw <coleman@civicrm.org>
Fri, 26 May 2023 01:35:31 +0000 (21:35 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 26 May 2023 01:35:31 +0000 (21:35 -0400)
Some not-well-documented code led to confusion that's been copy-pasted a couple times now.
This removes the unnecessarily-pasted code and adds clarifying comments to the code which
originally caused the confusion.

Civi/Api4/Service/Spec/Provider/ContactGetSpecProvider.php
Civi/Api4/Service/Spec/Provider/ContributionGetSpecProvider.php
Civi/Api4/Service/Spec/Provider/GroupGetSpecProvider.php

index ffc8efa05609ed9654c5931e7f758b8a2bff0fd5..3e664c9c184cddfbae7af71e37dd496cea10c376 100644 (file)
@@ -40,7 +40,8 @@ class ContactGetSpecProvider extends \Civi\Core\Service\AutoService implements G
       ->setOptionsCallback([__CLASS__, 'getGroupList']);
     $spec->addFieldSpec($field);
 
-    // Fields specific to Individuals
+    // The following fields are specific to Individuals, so omit them if
+    // `contact_type` value was passed to `getFields` and is not "Individual"
     if (!$spec->getValue('contact_type') || $spec->getValue('contact_type') === 'Individual') {
       // Age field
       $field = new FieldSpec('age_years', 'Contact', 'Integer');
index b1037b7ca0a370106dc96bfbb69f031d971569f8..e6ca35c3af31d5ef4179fa4c4c86ca54379bbaaf 100644 (file)
@@ -27,40 +27,34 @@ class ContributionGetSpecProvider extends \Civi\Core\Service\AutoService impleme
    * @throws \CRM_Core_Exception
    */
   public function modifySpec(RequestSpec $spec): void {
-    // Amount paid field
-    if (!$spec->getValue('paid_amount')) {
-      $field = new FieldSpec('paid_amount', 'Contribution', 'Float');
-      $field->setLabel(ts('Amount Paid'))
-        ->setTitle(ts('Amount Paid'))
-        ->setDescription(ts('Amount paid'))
-        ->setType('Extra')
-        ->setDataType('Money')
-        ->setReadonly(TRUE)
-        ->setSqlRenderer([__CLASS__, 'calculateAmountPaid']);
-      $spec->addFieldSpec($field);
-    }
-    if (!$spec->getValue('balance_amount')) {
-      $field = new FieldSpec('balance_amount', 'Contribution', 'Float');
-      $field->setLabel(ts('Balance'))
-        ->setTitle(ts('Balance'))
-        ->setDescription(ts('Balance'))
-        ->setType('Extra')
-        ->setDataType('Money')
-        ->setReadonly(TRUE)
-        ->setSqlRenderer([__CLASS__, 'calculateBalance']);
-      $spec->addFieldSpec($field);
-    }
-    if (!$spec->getValue('tax_exclusive_amount')) {
-      $field = new FieldSpec('tax_exclusive_amount', 'Contribution', 'Float');
-      $field->setLabel(ts('Tax Exclusive Amount'))
-        ->setTitle(ts('Tax Exclusive Amount'))
-        ->setDescription(ts('Tax Exclusive Amount'))
-        ->setType('Extra')
-        ->setDataType('Money')
-        ->setReadonly(TRUE)
-        ->setSqlRenderer([__CLASS__, 'calculateTaxExclusiveAmount']);
-      $spec->addFieldSpec($field);
-    }
+    // Add calculated fields
+    $field = new FieldSpec('paid_amount', 'Contribution', 'Float');
+    $field->setLabel(ts('Amount Paid'))
+      ->setTitle(ts('Amount Paid'))
+      ->setDescription(ts('Amount paid'))
+      ->setType('Extra')
+      ->setDataType('Money')
+      ->setReadonly(TRUE)
+      ->setSqlRenderer([__CLASS__, 'calculateAmountPaid']);
+    $spec->addFieldSpec($field);
+    $field = new FieldSpec('balance_amount', 'Contribution', 'Float');
+    $field->setLabel(ts('Balance'))
+      ->setTitle(ts('Balance'))
+      ->setDescription(ts('Balance'))
+      ->setType('Extra')
+      ->setDataType('Money')
+      ->setReadonly(TRUE)
+      ->setSqlRenderer([__CLASS__, 'calculateBalance']);
+    $spec->addFieldSpec($field);
+    $field = new FieldSpec('tax_exclusive_amount', 'Contribution', 'Float');
+    $field->setLabel(ts('Tax Exclusive Amount'))
+      ->setTitle(ts('Tax Exclusive Amount'))
+      ->setDescription(ts('Tax Exclusive Amount'))
+      ->setType('Extra')
+      ->setDataType('Money')
+      ->setReadonly(TRUE)
+      ->setSqlRenderer([__CLASS__, 'calculateTaxExclusiveAmount']);
+    $spec->addFieldSpec($field);
   }
 
   /**
index a42d7d37ab35ee15530853df37d9b232c50293b4..969f5c5e3197ad6f1dac2ece710c75abfc47fded 100644 (file)
@@ -27,16 +27,14 @@ class GroupGetSpecProvider extends \Civi\Core\Service\AutoService implements Gen
    * @throws \CRM_Core_Exception
    */
   public function modifySpec(RequestSpec $spec): void {
-    // Number of contacts
-    if (!$spec->getValue('contact_count')) {
-      $field = new FieldSpec('contact_count', 'Group', 'Integer');
-      $field->setLabel(ts('Contact Count'))
-        ->setDescription(ts('Number of contacts in group'))
-        ->setColumnName('id')
-        ->setReadonly(TRUE)
-        ->setSqlRenderer([__CLASS__, 'countContacts']);
-      $spec->addFieldSpec($field);
-    }
+    // Calculated field counts contacts in group
+    $field = new FieldSpec('contact_count', 'Group', 'Integer');
+    $field->setLabel(ts('Contact Count'))
+      ->setDescription(ts('Number of contacts in group'))
+      ->setColumnName('id')
+      ->setReadonly(TRUE)
+      ->setSqlRenderer([__CLASS__, 'countContacts']);
+    $spec->addFieldSpec($field);
   }
 
   /**