2

I know I can do it using civicrm_buildForm but I don't know:

  • How to get the custom field
  • How to set the value to this field
3
  • What is your civicrm version? Mar 21 at 17:44
  • @PradeepNayak 5.65.1 Mar 21 at 17:45
  • Are you talking about offline contribution form? Mar 21 at 17:51

1 Answer 1

4

You can do something like below you can use API4 to get the exact machine names

civicrm/api4#/explorer/CustomField/get?select=%5B"id"%5D&where=%5B%5B"name","%3D",""%5D,%5B"custom_group_id:name","%3D",""%5D%5D&limit=0&checkPermissions=0&_index="0"

function extname_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
    if (isset($_GET['custom_59'])) {
      $customFieldId = \Civi\Api4\CustomField::get(FALSE)
        ->addSelect('id')
        ->addWhere('name', '=', 'is_eligible')
        ->addWhere('custom_group_id:name', '=', 'gift_aid')
        ->execute()
        ->first()['id'] ?? NULL;
      if (!empty($customFieldId)) {
        $form->setDefaults([
          'custom_' . $customFieldId => $_GET['custom_59'],
        ]);
      }
    }
  }
}
2
  • Thanks @PradeepNayak. However 59 is the custom field ID, right? My local env database is different from the prod database, so I need to get this custom field without using its ID. It's possible? Mar 21 at 17:55
  • If you are using the same machine name then yes possible. Mar 21 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.