Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for financial account custom fields #23067

Merged

Conversation

MegaphoneJon
Copy link
Contributor

Overview

The FinancialAccount entity almost but doesn't quite have support for custom fields.

Before

Can't save or display custom field data for financial accounts.

After

Works.

Technical Details

The BAO change is all that's necessary to support FinancialAccount custom fields in API4 - I almost put the rest in an extension, but the code is well-abstracted and so why not add support in core.

Comments

You'll need to add FinancialAccount to the cg_extend_objects option group. cv api OptionValue.create label='Financial Accounts' name='civicrm_financial_account' value='FinancialAccount' option_group_id='cg_extend_objects' will do the trick.

@civibot
Copy link

civibot bot commented Mar 31, 2022

(Standard links)

@civibot civibot bot added the master label Mar 31, 2022
@@ -97,6 +97,10 @@ public static function add(&$params) {
$financialAccount->copyValues($params);
$financialAccount->save();

if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], static::$_tableName, $financialAccount->id, $op);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this minimal change to the BAO is ok and not a blocker to the PR, but FYI the more complete solution is the refactoring we've been doing to many (eventually all) of our BAOs which is to deprecate and gut the add() function, reducing it to one line that calls self::writeRecord() and move the rest of the pre/post logic to pre/post hooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw Is there a simple entity that has implemented this change that can serve as a reference? I would also prefer to merge this now, but I'd be willing to redo it in a later PR, and ideally we can document it somewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty simple example of such a refactor. The del() function contained some pre-logic which got moved to a self_pre hook:

42f0bf1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MegaphoneJon possibly just this

diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php
index 07198cb149..359f3b0de3 100644
--- a/CRM/Financial/BAO/FinancialAccount.php
+++ b/CRM/Financial/BAO/FinancialAccount.php
@@ -78,31 +78,7 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco
       $queryParams = [1 => [$params['financial_account_type_id'], 'Integer']];
       CRM_Core_DAO::executeQuery($query, $queryParams);
     }
-
-    // action is taken depending upon the mode
-    $financialAccount = new CRM_Financial_DAO_FinancialAccount();
-
-    // invoke pre hook
-    $op = 'create';
-    if (!empty($params['id'])) {
-      $op = 'edit';
-    }
-    CRM_Utils_Hook::pre($op, 'FinancialAccount', CRM_Utils_Array::value('id', $params), $params);
-
-    if (!empty($params['id'])) {
-      $financialAccount->id = $params['id'];
-      $financialAccount->find(TRUE);
-    }
-
-    $financialAccount->copyValues($params);
-    $financialAccount->save();
-
-    // invoke post hook
-    $op = 'create';
-    if (!empty($params['id'])) {
-      $op = 'edit';
-    }
-    CRM_Utils_Hook::post($op, 'FinancialAccount', $financialAccount->id, $financialAccount);
+    $financialAccount = self::writeRecord($params);
     CRM_Core_PseudoConstant::flush();
 
     return $financialAccount;

@MegaphoneJon
Copy link
Contributor Author

Perfect, thank you. I'll bookmark this and try to implement it somewhere soon.

This makes me long for the in-person Civi events where this information would get shared, especially when we could use the sprint to give these techniques significant traction.

@colemanw
Copy link
Member

colemanw commented Apr 1, 2022

I've tested this and it works without any errors. Agree that a followup is in order though.

@colemanw colemanw merged commit 0550a87 into civicrm:master Apr 1, 2022
@colemanw
Copy link
Member

colemanw commented Apr 1, 2022

Here is a followup PR to cleanup the BAO: #23079

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon it's starting to feel more feasible - there is at least a small sprint in Manchester in October...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants