Answers.Body: <p>Handling <code>install, uninstall, enable, disable</code> is exactly what <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed/" rel="nofollow noreferrer"><code>hook_civicrm_managed()</code></a> is for! While this is an admittedly strange use for it and I have to wonder whether reproducing contributions using custom fields is really the best way to approach whatever problem your extension is trying to solve... that aside, it can be done!</p>
  <ol>
  <li>Upgrade your copy of <code>civix</code> to the latest version.</li>
  <li>Find the id of the option group you've already created to represent financial types (let's say it's <code>99</code>).</li>
  <li>From within your extension's directory, run the command <code>civix export OptionGroup 99</code></li>
  <li>This will generate a new <code>.mgd.php</code> file in your extension's <code>managed</code> directory.</li>
  <li>The file consists of 1 declaration of the optionGroup, plus a declaration for each option value. Leave the first one, and you'll convert the option values to a <code>foreach</code> loop:</li>
  <li>In the <code>.mdg.php</code> file, you can call the api to fetch all financial types, then loop through them to add to the return value.</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-27T01:02:39.527Z
Answers.Text: Ohh I see, I've tried to use the manage hook and didn't know you could do api calls in it, can you use api4 or is it strictly api3? for clarification I'm making an extension that adds a new activity type, so that it can be mapped onto a ninja form and when a user fills in the form an activity is generated, later when that activity is marked completed it creates a new contribution using the user's field's input on the activity ^-^.
Answers.UserDisplayName: BellaDoll
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-27T13:38:49.657Z
Answers.Text: Yes you can (and should) use APIv4. That is the format `civix export` will generate for you.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-27T13:40:25.417Z
Answers.Text: Why not have the ninja form directly create a contribution & set the status as *Pending*?
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-28T01:17:42.103Z
Answers.Text: Thank you for the help! it worked. As for ninja forms creating the contribution out right, to my knowledge the ninjaforms add-on for civicrm only allows for the creation/modification of contacts and activities, not other entities like cases or contributions. However there's an additional reason as to why i need the activity and not just the contribution that I cannot state , but thank you again ! ^-^
Answers.UserDisplayName: BellaDoll
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-28T20:53:48.587Z
Answers.Text: You're welcome!
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-27T00:27:46.033Z
Answers.LastActivityDate: 2023-12-27T00:27:46.033Z
Answers.UserDisplayName: Coleman
Body: <p>I'm new to Civicrm PHP and extension development so I'm sorry if this is a dumb question. ^-^</p>
  <p>I'm making an extension that creates a new custom field group, the custom field group is meant to replicate the fields of a contribution <em>(so it has payment method, amount, currency type, ETC).</em></p>
  <p>The problem is the financial type id field, unlike payment method there isn't an option group that I can link the custom field to.</p>
  <p>I've made a work around by creating an option group, updating the values inside to copy any changes made to the financial type table, while it's somewhat functional it's messy, breaks my site sometimes and has caused me hours of work fixing it <em>(I've had to make checks in install, uninstall, enable, disable, postCommit hooks)</em></p>
  <p>Is there a way I can add it to cause less bugs/errors like the contribution form, or should I just stick with my current work around and optimize it?</p>
  <p>Help would be appreciated! I've been working on this extension for nearly a month atp haha ^-^</p>
  
Tags: <custom-fields><extension><api><contributions><php>
Title: CIVICRM: Custom Field that uses financial type id
ThreadText: # CIVICRM: Custom Field that uses financial type id
  
  (Asked by: username_0 on 2023-12-26)
  
  I'm new to Civicrm PHP and extension development so I'm sorry if this is a dumb question. ^-^
  
  I'm making an extension that creates a new custom field group, the custom field group is meant to replicate the fields of a contribution *(so it has payment method, amount, currency type, ETC).*
  
  The problem is the financial type id field, unlike payment method there isn't an option group that I can link the custom field to.
  
  I've made a work around by creating an option group, updating the values inside to copy any changes made to the financial type table, while it's somewhat functional it's messy, breaks my site sometimes and has caused me hours of work fixing it *(I've had to make checks in install, uninstall, enable, disable, postCommit hooks)*
  
  Is there a way I can add it to cause less bugs/errors like the contribution form, or should I just stick with my current work around and optimize it?
  
  Help would be appreciated! I've been working on this extension for nearly a month atp haha ^-^
  
  **Tags:** custom-fields, extension, api, contributions, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-27. Score: 1 | Accepted answer
  
  Handling `install, uninstall, enable, disable` is exactly what [`hook_civicrm_managed()`](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed/) is for! While this is an admittedly strange use for it and I have to wonder whether reproducing contributions using custom fields is really the best way to approach whatever problem your extension is trying to solve... that aside, it can be done!
  
  1. Upgrade your copy of `civix` to the latest version.
  2. Find the id of the option group you've already created to represent financial types (let's say it's `99`).
  3. From within your extension's directory, run the command `civix export OptionGroup 99`
  4. This will generate a new `.mgd.php` file in your extension's `managed` directory.
  5. The file consists of 1 declaration of the optionGroup, plus a declaration for each option value. Leave the first one, and you'll convert the option values to a `foreach` loop:
  6. In the `.mdg.php` file, you can call the api to fetch all financial types, then loop through them to add to the return value.
  
  > **Comments:**
  > **username_0**: Ohh I see, I've tried to use the manage hook and didn't know you could do api calls in it, can you use api4 or is it strictly api3? for clarification I'm making an extension that adds a new activity type, so that it can be mapped onto a ninja form and when a user fills in the form an activity is generated, later when that activity is marked completed it creates a new contribution using the user's field's input on the activity ^-^.  
  > **username_1**: Yes you can (and should) use APIv4. That is the format `civix export` will generate for you.  
  > **username_1**: Why not have the ninja form directly create a contribution & set the status as *Pending*?  
  > **username_0**: Thank you for the help! it worked. As for ninja forms creating the contribution out right, to my knowledge the ninjaforms add-on for civicrm only allows for the creation/modification of contacts and activities, not other entities like cases or contributions. However there's an additional reason as to why i need the activity and not just the contribution that I cannot state , but thank you again ! ^-^  
  > **username_1**: You're welcome!  
  
  ---
  
  