3

Edit Note: Since I first began looking into this, I have been reminded that the Drupal way to solve this challenge is to develop a Drupal module to customize the default CiviCRM behavior. I have now done so, however something is overriding the custom Drupal module behavior it injects into CiviCRM.

We have understood that with CiviCRM, there are three options for the auto-renew of Civi Memberships:

civicrm_membership_type Add column - auto_renew TINYINT(4), default 0 values: 0 = No auto-renew option; 1 = Give option, but not required; 2 = Auto-renew required;

We have need of an "auto-renew by default / you may opt out" membership mode, as a handful of potential members are absolutely offended that the "Please renew my membership automatically." check box is checked and grayed out.

This particular site runs CiviCRM with Drupal 6.x, latest version of CiviCRM.

4 Answers 4

2

I had an almost identical problem to solve today, which was to default a recurring contribution checkbox to "yes". I think your original code is close, but QuickForm doesn't expect an array for a default value for a checkbox. So, for instance, here's the code that worked for me:

function alwayscheckrecurring_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
    // "recurring" should always be set to "yes" if it exists.
    $defaults['is_recur'] = 1;
    $form->setDefaults( $defaults );
  }
}
4
  • Greetings Jon, That looked perfect. However, I no longer see the "checked / unchecked"... it just is unchecked always. Jun 17, 2015 at 22:38
  • Also Jon, I switched back to my version of the module, disabled JavaScript and the check box stays checked / default price never goes bold. So, since I am having a tangle with JavaScript, not PHP code only, does that detail shed any light on a reliable solution? Thank you. Jun 17, 2015 at 23:42
  • Michael - I'm not clear on what the current problem is. Can you restate it? Jun 18, 2015 at 23:17
  • 1
    CiviCRM's JavaScript appears to be unchecking the recurring contribution check box, which my custom Drupal module successfully checks. I need to know how to modify the behavior, now, of CiviCRM's core JavaScript. And/or alternate PHP syntax which will be compatible with the CiviCRM JavaScript. It sounded like your alternate PHP syntax was likely to have avoided being adjusted by the JavaScript, instead I saw no checking of the box what so ever using your PHP syntax. Thank you. Jun 20, 2015 at 2:01
1

It sounds like you want to have the option defaulted to true, which is a good thing to do in a small site customization of the membership signup page(s).

You just need to put a single line of code in the right place to set the option.

A developer could use civix to create an extension for custom code for your site, then implement the buildForm (iirc) hook, check the args to filter so the context is the page(s) you want to change, then set the value of the correct field to true.

1
  • 1
    Aaahhh... and I once coded such a "check the box when the page loads" module. Thank you for the reminder. I will look up that code again, and post it back on this thread if it indeed is a viable solution. :-) May 28, 2015 at 1:09
0

hhhmmm... brushing the dust off this five year old module code is a bit of a challenge... Enabling the module updated for the present requirement causes Drupal to completely "white screen". Does the error I made come to mind?

<?php

/**
 * buildForm hook
 */
function civicrm_cir_civicrm_buildForm( $formName, &$form ) {

  // Pre-set the membership join forms to recurring
  if ( $formName == 'CRM_Contribute_Form_Contribution_Main' ) && (
         $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
    $(function() {
      $('#auto_renew').attr('checked', true);
    });
  }

}
2
  • This is posted as an answer to the above question but looks like it does not provide an answer but poses a different question. Can you either rephrase as an answer or delete and repost as a question? The reason this code crashes btw is that you are mixing up PHP and Javascript. The last few lines are js and do not belong in php code. Also you should turn on "display errors" on your sandbox so php will tell you more than a wsod.
    – Coleman
    Jun 1, 2015 at 22:01
  • Thank you Coleman, I have no idea how I stumbled upon some Javascript source code, I will attempt to correct it into all PHP code. About this StackExchange new forum... most confusing system. (Perhaps, though, a little better than Twitter.) Jun 2, 2015 at 1:30
0

It looks like I have the Drupal module firing the needed behavior into the CiviCRM Contribution page form initially... however once the page completes loading, the check box is flipped back to UNCHECKED. Not funny! >:-|

Also, at the same time the check box gets cleared, the default price option also becomes bolded. Once the default price gets bolded, also the "Please renew my membership automatically." check box gets unchecked.

Here is the form while the page is still loading into Mozilla Firefox:

enter image description here

And here is the form when the page finishes loading:

enter image description here

So, this is the only CiviCRM customization on the site, what else could be adjusting the contribution form?

<?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 );
    }
  }
}

I am thankful,

Michael

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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