1

I need to perform a simple update of default values to a few CiviContribution forms on a client site. I was reminded that developing a Drupal module to customize the behavior of CiviCRM was the way to accomplish what we are in need of accomplishing. I coded up the following module:

<?php

/**
 * buildForm hook
 */
function civicrm_cir_civicrm_buildForm( $formName, &$form )
{
  // Only operate on CiviCRM Contribution Forms
  if ( $formName == 'CRM_Contribute_Form_Contribution_Main' )
  {
    // Test for specific contribution forms by CiviCRM form ID
    if ($form->getVar( '_id' ) == 15 ||  // Yearly CC
        $form->getVar( '_id' ) == 16 ||  // Yearly PP
        $form->getVar( '_id' ) == 13 ||  // Monthly CC
        $form->getVar( '_id' ) == 14 ||  // Monthly PP
        $form->getVar( '_id' ) == 8  )   // Weekly CC
    {
      // Set the check box for recurring contribution
      $defaults['auto_renew'] = array('checked' => '1');
      $form->setDefaults( $defaults );
    }
  }
}

Initially when the CiviContribution forms are loading, the Drupal module fires in the desired form data change... The recurring contribution check box is checked.

enter image description here

However when the form finishes loading the check box flips back to unchecked, and the default selection price becomes bolded simultaneously.

enter image description here

A Drupal developer I know who is not vary familiar with the code of CiviCRM suggested perhaps if CiviCRM uses the Drupal Form API to build its forms, then I could use the "Hook Alter" type API's to rearrange the order these events fire in... allow the CiviCRM form default population to occur first, and then fire my custom module (above) to check the AutoRenew check box.

Or if this is still not the correct way to check the AutoRenew check box in current CiviCRM powered by Drupal 6.x, then please kindly advise what is the correct way to check the AutoRenew check box.

I am thankful,

Michael

1

From what you describe, it sounds like you are seeing some of CiviCRM's contribution page JS kick in after your customised form is rendered.

It is probable that configuring your CiviCRM Contribution page to use the defaults you desire will resolve this issue. Your approach of setting the defaults should translate to setting the form values, which should then be respected on initial load, but ... that's not happening :)

Take a look at the JS inlined on a default CiviCRM contribute form, eg the CiviCRM Demo site's Member Signup and Renew form when configured to permit optional membership auto-renew.

If you inspect the code immediately after the auto_renew element, you'll see this block coming from CiviCRM's templates.

<script type="text/javascript">
CRM.$(function($) {
    //if price set is set we use below below code to show for showing auto renew
    var autoRenewOption =  '0';
    $('#allow_auto_renew').hide();
    if ( autoRenewOption == 1 ) {
        $('#allow_auto_renew').show();
    } else if ( autoRenewOption == 2 ) {
        var autoRenew = $("#auto_renew");
        autoRenew.prop('checked',  true );
        autoRenew.attr( 'readonly', true );
        $('#allow_auto_renew').show();
    }
});
</script>

That code is fairly straightforward to read - if auto_renew is set to 2 (from the contribute form settings, which allow you to configure this behaviour per contribute form), then the JS will check that box on page load, as you're seeing.

If I'm reading all that right, and with the caveat of not having seen your actual site at all, I think you can configure this from your contribute form settings for each of the form IDs listed above. The setting is on the "Membership" tab and looks like this:

membership renew settings

The recurring defaults are tied to the settings in the Contribute form (and depend on settings in both Membership Type and Payment Processor config).

If that's not enough, you can override the JS by customising CiviCRM's templates, and you can add your own JS also if need be. That then would be your next path forward - modify that template variable based on the switch statement in your existing code.

You can certainly do it via JS though, as demonstrated by this customised contribute form (js here).

  • I was more interested in the recurring checkbox getting checked. I noticed the boldness changes at the same time the check box gets unchecked... thought that might be worth while to point out to lead to a solution. – Michael Lueck Jun 17 '15 at 22:40
  • Yes, that was what I intended to point you towards. You can confirm that your hook_civicrm_buildForm() changes are working by disabling JS in your browser and reloading the page. Then you can move onto working out why the JS is doing something unexpected, and whether this needs a code fix or just a config change on your Contribute form. Do update when you find the solution! – Chris Burgess Jun 17 '15 at 22:44
  • I disabled JavaScript via Firefox Web Developer toolbar. Indeed the recurring checkbox stays checked and the default price never gets bolded. I am also tracking an alternate "check the box PHP solution" on another thread. Seems setting auto_renew might not be the only way to specify recurring to CiviCRM. Thank you! :-) – Michael Lueck Jun 17 '15 at 23:17
  • Hi Michael (sorry, I addressed you as Martin on the forum!). I've updated the answer above. Good luck! – Chris Burgess Jun 18 '15 at 23:20
  • Greetings Chris, you stated: "If that's not enough, you can override the JS by customising CiviCRM's templates, and you can add your own JS also if need be." So instead of customizing the behavior through a PHP Drupal module, are you suggesting adding some custom JS to further customize CiviCRM's template? Yes I am familiar with the CiviCRM three options for auto-renew. We are seeking "Give option - default checked" which is not one of the provided options. Thank you. – Michael Lueck Jun 20 '15 at 2:10
0

If you want a more "Drupal-style" form which you can hook_alter() wantonly, look into using CiviCRM Contribute forms with Webform CiviCRM.

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.