1

Been stuck on this for the past few days I want to set a default value for a custom field in a contribution. The Custom fields are associated by Financial Types, I intend to show the current contact id of the person the contribution is being submitted for in a custom field. I tried doing this originally within the CustomDatabyType.php file.

class CRM_Custom_Form_CustomDataByType extends CRM_Core_Form {

  public function setDefaultValues() {
    $contact_id = CRM_Core_Form::getContactID();
    $defaults = [];
    $defaults['custom_224_-1'] = $contact_id;
    CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE, $this->get('action'));
    return $defaults;
  }
}

To Clarify the reason i want the contact ID for the user i am submitting the form for is because i want to display that contacts 'Employer' in a custom field. I have tried the following:

CRM_Utils_Request::retrieve('cid', 'Positive');
$form->getContactID();
$form->_contactID;

The issue i am having is that is the above lines of code work if i associate it to a field within the contribution form but if i associate it to a custom field it will show the contactID of the user submitting the form not the user of whom the form is being submitting for.

I have also tried to do this via a custom buildForm hook:

function dfi_civicrm_buildForm($formName, &$form) {

    if ($formName == 'CRM_Contribute_Form_Contribution' || $form == CRM_Core_Action::ADD) {
        $thisContactID = $form->_contactID;
        $defaults['custom_224_-1'] = $thisContactID;
       $form->setDefaults($defaults);
    }
}

I hope i have been clear and i would really appreciate your help.

Thanks


An Idea i have had is to set the ContactID within a field in the 'Contribution section' use getVar to get the value of the field and then assign that value to the custom field.

Is this possible?

  • Try instead with hook_civicrm_buildForm and use $form->setDefaults() – Demerit Nov 14 '19 at 20:55
  • @Demerit - Its not working unfortunately. – Moe Nov 18 '19 at 13:02
  • github.com/civicrm/civicrm-core/pull/… - Found this however it only works for Activity Custom Fields not Contribution. – Moe Nov 20 '19 at 8:15
2

I was able to figure this out using the following code:

        $getSessionArray = CRM_Core_Session::singleton()->get('CRM_Utils_Recent');
        $thisContactID = $getSessionArray[0]['id'];
  • Great. Just a note for others that if you remove Contacts from the list at Administer - System Settings - Misc where it says "Recent Items Providers" then this might fail, but I'd guess that very few sites do that. – Demerit Dec 10 '19 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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