diff --git a/checknumberpaymentmethod.php b/checknumberpaymentmethod.php index 0f64874..fe741fc 100644 --- a/checknumberpaymentmethod.php +++ b/checknumberpaymentmethod.php @@ -185,6 +185,30 @@ function checknumberpaymentmethod_civicrm_preProcess($formName, &$form) { * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess */ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { + if (in_array($formName, array( + "CRM_Contribute_Form_Contribution", + "CRM_Member_Form_Membership", + "CRM_Event_Form_Participant", + "CRM_Contribute_Form_AdditionalPayment", + "CRM_Member_Form_MembershipRenewal", + ))) { + if ($form->getVar('_action') & CRM_Core_Action::DELETE) { + return; + } + if ($formName == 'CRM_Contribute_Form_AdditionalPayment' + && $form->getVar('_view') == 'transaction' + && ($form->getVar('_action') & CRM_Core_Action::BROWSE) + ) { + return; + } + if (!empty($form->_defaultValues['payment_instrument_id'])) { + _checknumberpaymentmethod_add_payment_fields( + $form, + $form->_defaultValues['payment_instrument_id'] + ); + } + } + if (('CRM_Contribute_Form_AdditionalPayment' == $formName && $form->getVar('_view') == 'transaction' && ($form->getVar('_action') & CRM_Core_Action::BROWSE)) @@ -192,9 +216,12 @@ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { || 'CRM_Contribute_Form_ContributionView' == $formName ) { $payments = $form->get_template_vars('payments'); - _checknumberpaymentmethod_alterpayments($payments); + if ($payments) { + _checknumberpaymentmethod_alterpayments($payments); + } $form->assign('payments', $payments); } + if ('CRM_Admin_Form_Preferences_Contribute' == $formName) { $form->addElement( 'select', @@ -210,15 +237,9 @@ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { } if ($formName == 'CRM_Financial_Form_Payment' && !empty($form->paymentInstrumentID)) { - $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ - 'name' => 'check_payment_instrument_ids', - ]); - if (in_array($form->paymentInstrumentID, $paymentInstrumentsFromSettings)) { - $form->assign('paymentFields', array('check_number')); - $form->add('text', 'check_number', ts('Check Number')); - $form->assign('paymentTypeLabel', ts('Check Information')); - } + _checknumberpaymentmethod_add_payment_fields($form, $form->paymentInstrumentID); } + if ('CRM_Financial_Form_PaymentEdit' == $formName) { $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ 'name' => 'check_payment_instrument_ids', @@ -232,7 +253,7 @@ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { if ($form->_submitValues['payment_instrument_id'] != $values['payment_instrument_id']) { $numberOfCalls = 2; } - + CRM_Core_Smarty::singleton()->assign('payment_instrument_check_number_numberOfCalls', $numberOfCalls); CRM_Core_Smarty::singleton()->assign('payment_instrument_check_number', $form->_submitValues['check_number']); } @@ -246,6 +267,23 @@ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { } } +/** + * Add payment fields to form. + * + * @param object $form + * @param int $paymentInstrumentID + */ +function _checknumberpaymentmethod_add_payment_fields(&$form, $paymentInstrumentID) { + $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ + 'name' => 'check_payment_instrument_ids', + ]); + if (in_array($paymentInstrumentID, $paymentInstrumentsFromSettings)) { + $form->assign('paymentFields', array('check_number')); + $form->add('text', 'check_number', ts('Check Number')); + $form->assign('paymentTypeLabel', ts('Check Information')); + } +} + /** * Implements hook_civicrm_pre(). *