Use generic pseudoconstant declaration methodology to add financial_type to pseudocon...
authoreileen <emcnaughton@wikimedia.org>
Thu, 28 Feb 2019 01:21:59 +0000 (14:21 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 4 Mar 2019 22:08:40 +0000 (11:08 +1300)
Getting away from the hard-hack allows us to have more consistent metadata for generic fields

CRM/Contribute/BAO/Contribution.php
CRM/Core/DAO.php

index a3bc592f2828e94ede39b4a47f247fd621f55a89..7edc5b31b092ebf53fe9fec273aa437d6dfc9774 100644 (file)
@@ -804,7 +804,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
           ]
         );
       }
-      $typeField = CRM_Financial_DAO_FinancialType::export();
+
       $financialAccount = CRM_Financial_DAO_FinancialAccount::export();
 
       $contributionPage = array(
@@ -872,7 +872,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
         ),
       );
 
-      $fields = array_merge($fields, $typeField, $contributionPage,
+      $fields = array_merge($fields, $contributionPage,
         $contributionNote, $extraFields, $softCreditFields, $financialAccount, $campaignTitle,
         CRM_Core_BAO_CustomField::getFieldsForImport('Contribution', FALSE, FALSE, FALSE, $checkPermission)
       );
index ea823c970adfb81236076e62e7b312a38487aca0..55a25aeb9abd47d1017943e108912381509a2605 100644 (file)
@@ -2365,20 +2365,47 @@ SELECT contact_id
    * This is relevant where we want to offer both the ID field and the label field
    * as an option, e.g. search builder.
    *
-   * It is currently limited for optionGroupName for purposes keeping the scope of the
+   * It is currently limited for optionGroupName & id+ name+ FK combos for purposes keeping the scope of the
    * change small, but is appropriate for other sorts of pseudoconstants.
    *
    * @param array $fields
    */
   public static function appendPseudoConstantsToFields(&$fields) {
     foreach ($fields as $field) {
-      if (!empty($field['pseudoconstant']) && !empty($field['pseudoconstant']['optionGroupName'])) {
-        $fields[$field['pseudoconstant']['optionGroupName']] = array(
-          'title' => CRM_Core_BAO_OptionGroup::getTitleByName($field['pseudoconstant']['optionGroupName']),
-          'name' => $field['pseudoconstant']['optionGroupName'],
-          'data_type' => CRM_Utils_Type::T_STRING,
-          'is_pseudofield_for' => $field['name'],
-        );
+      if (!empty($field['pseudoconstant'])) {
+        $pseudoConstant = $field['pseudoconstant'];
+        if (!empty($pseudoConstant['optionGroupName'])) {
+          $fields[$pseudoConstant['optionGroupName']] = [
+            'title' => CRM_Core_BAO_OptionGroup::getTitleByName($pseudoConstant['optionGroupName']),
+            'name' => $pseudoConstant['optionGroupName'],
+            'data_type' => CRM_Utils_Type::T_STRING,
+            'is_pseudofield_for' => $field['name'],
+          ];
+        }
+        // We restrict to id + name + FK as we are extending this a bit, but cautiously.
+        elseif (
+          !empty($field['FKClassName'])
+          && CRM_Utils_Array::value('keyColumn', $pseudoConstant) === 'id'
+          && CRM_Utils_Array::value('labelColumn', $pseudoConstant) === 'name'
+        ) {
+          $pseudoFieldName = str_replace('_' . $pseudoConstant['keyColumn'], '', $field['name']);
+          // This if is just an extra caution when adding change.
+          if (!isset($fields[$pseudoFieldName])) {
+            $daoName = $field['FKClassName'];
+            $fkFields = $daoName::fields();
+            foreach ($fkFields as $fkField) {
+              if ($fkField['name'] === $pseudoConstant['labelColumn']) {
+                $fields[$pseudoFieldName] = [
+                  'name' => $pseudoFieldName,
+                  'is_pseudofield_for' => $field['name'],
+                  'title' => $fkField['title'],
+                  'data_type' => $fkField['type'],
+                  'where' => $field['where'],
+                ];
+              }
+            }
+          }
+        }
       }
     }
   }