Added custom field for soft credit on Add/Edit contribution form

This commit is contained in:
2018-12-22 00:52:16 +00:00
parent f9fe27054a
commit 0a8855f74e
2 changed files with 131 additions and 0 deletions

View File

@ -173,4 +173,71 @@ function softcreditcustomfields_civicrm_buildForm($formName, &$form) {
));
}
}
if ('CRM_Contribute_Form_Contribution' == $formName && !($form->getVar('_action') & CRM_Core_Action::DELETE)) {
CRM_Core_Region::instance('page-footer')->add(array(
'template' => "CRM/Contribute/Form/SoftCredit-Custom.tpl",
));
for ($blockId = 1; $blockId <= 10; $blockId++) {
$entityId = NULL;//CRM_Utils_Array::value('id', CRM_Utils_Array::value($blockId, $softCredits));
_softcreditcustomfields_addCustomDataToForm($form, $entityId, $blockId);
}
}
}
/**
* Add custom data to the form.
*
* @param CRM_Core_Form $form
* @param int $entityId
* @param int $blockId
*/
function _softcreditcustomfields_addCustomDataToForm(&$form, $entityId, $blockId) {
$groupTree = CRM_Core_BAO_CustomGroup::getTree('ContributionSoft', NULL, $entityId);
if (isset($groupTree) && is_array($groupTree)) {
// use simplified formatted groupTree
$groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
// make sure custom fields are added /w element-name in the format - 'soft_credit_amount[$blockId][custom-X]'
foreach ($groupTree as $id => $group) {
foreach ($group['fields'] as $fldId => $field) {
$groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
$groupTree[$id]['fields'][$fldId]['element_name'] = "soft_credit[$blockId][{$field['element_name']}]";
}
}
$defaults = [];
CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
// since we change element name for softCredit custom data, we need to format the setdefault values
$softCreditDefaults = [];
foreach ($defaults as $key => $val) {
if (empty($val)) {
continue;
}
// inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
// this works for all types custom data
$keyValues = explode('[', str_replace(']', '', $key));
$softCreditDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
}
$form->setDefaults($softCreditDefaults);
// we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
// And we can't set it to 'soft_credit_' because we want to set it in a slightly different format.
CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
// during contact editing : if no softCredit is filled
// required custom data must not produce 'required' form rule error
// more handling done in formRule func
//self::storeRequiredCustomDataInfo($form, $groupTree);
$tplGroupTree = CRM_Core_Smarty::singleton()
->get_template_vars('soft_credit_groupTree');
$tplGroupTree = empty($tplGroupTree) ? [] : $tplGroupTree;
$form->assign('soft_credit_groupTree', $tplGroupTree + [$blockId => $groupTree]);
// unset the temp smarty var that got created
$form->assign('dnc_groupTree', NULL);
}
// softCredit custom data processing ends ..
}