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?
$form->setDefaults()