Answers.Body: <p>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:</p>
  
  <pre><code>function alwayscheckrecurring_civicrm_buildForm($formName, &amp;$form) {
    if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
      // "recurring" should always be set to "yes" if it exists.
      $defaults['is_recur'] = 1;
      $form-&gt;setDefaults( $defaults );
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T22:38:50.483Z
Answers.Text: Greetings Jon, That looked perfect. However, I no longer see the "checked / unchecked"... it just is unchecked always.
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T23:42:25.710Z
Answers.Text: 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.
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-18T23:17:51.530Z
Answers.Text: Michael - I'm not clear on what the current problem is.  Can you restate it?
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-20T02:01:13.663Z
Answers.Text: 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.
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T20:06:38.373Z
Answers.LastActivityDate: 2015-06-16T20:06:38.373Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>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). </p>
  
  <p>You just need to put a single line of code in the right place to set the option. </p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T01:09:14.877Z
Answers.Text: 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. :-)
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-27T03:01:54.720Z
Answers.LastActivityDate: 2015-05-27T03:01:54.720Z
Answers.UserDisplayName: Joe Murray
Answers.Body: <p>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?</p>
  
  <pre><code>&lt;?php
  
  /**
   * buildForm hook
   */
  function civicrm_cir_civicrm_buildForm( $formName, &amp;$form ) {
  
    // Pre-set the membership join forms to recurring
    if ( $formName == 'CRM_Contribute_Form_Contribution_Main' ) &amp;&amp; (
           $form-&gt;getVar( '_id' ) == 15 ||    // Yearly CC
           $form-&gt;getVar( '_id' ) == 16 ||    // Yearly PP
           $form-&gt;getVar( '_id' ) == 13 ||    // Monthly CC
           $form-&gt;getVar( '_id' ) == 14 ||    // Monthly PP
           $form-&gt;getVar( '_id' ) == 8  ) ) { // Weekly CC
      // Set the check box for recurring contribution
      $(function() {
        $('#auto_renew').attr('checked', true);
      });
    }
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-01T22:01:29.930Z
Answers.Text: 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.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-02T01:30:48.463Z
Answers.Text: 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.)
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-01T10:35:41.427Z
Answers.LastActivityDate: 2015-06-01T10:35:41.427Z
Answers.UserDisplayName: Michael Lueck
Answers.Body: <p>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! >:-|</p>
  
  <p>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.</p>
  
  <p>Here is the form while the page is still loading into Mozilla Firefox:</p>
  
  <p><img src="https://i.sstatic.net/AXrwm.png" alt="enter image description here"></p>
  
  <p>And here is the form when the page finishes loading:</p>
  
  <p><img src="https://i.sstatic.net/f4lh8.png" alt="enter image description here"></p>
  
  <p>So, this is the only CiviCRM customization on the site, what else could be adjusting the contribution form?</p>
  
  <pre><code>&lt;?php
  
  /**
   * buildForm hook
   */
  function civicrm_cir_civicrm_buildForm( $formName, &amp;$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-&gt;getVar( '_id' ) == 15 ||  // Yearly CC
          $form-&gt;getVar( '_id' ) == 16 ||  // Yearly PP
          $form-&gt;getVar( '_id' ) == 13 ||  // Monthly CC
          $form-&gt;getVar( '_id' ) == 14 ||  // Monthly PP
          $form-&gt;getVar( '_id' ) == 8  )   // Weekly CC
      {
        // Set the check box for recurring contribution
        $defaults['auto_renew'] = array('checked' =&gt; '1');
        $form-&gt;setDefaults( $defaults );
      }
    }
  }
  </code></pre>
  
  <p>I am thankful,</p>
  
  <p>Michael</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-03T00:35:00.083Z
Answers.LastActivityDate: 2015-06-03T01:12:46.127Z
Answers.LastEditDate: 2015-06-03T01:12:46.127Z
Answers.UserDisplayName: Michael Lueck
Body: <p><strong>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.</strong></p>
  
  <p>We have understood that with CiviCRM, there are three options for the auto-renew of Civi Memberships:</p>
  
  <p><em>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;</em></p>
  
  <p>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 <em>"Please renew my membership automatically."</em> check box is checked and grayed out.</p>
  
  <p>This particular site runs CiviCRM with Drupal 6.x, latest version of CiviCRM.</p>
  
Tags: <drupal><civimember>
Title: Drupal module to check "auto renew" checkbox for membership getting overridden, why?
ThreadText: # Drupal module to check "auto renew" checkbox for membership getting overridden, why?
  
  (Asked by: username_0 on 2015-05-27)
  
  **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.
  
  **Tags:** drupal, civimember
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2015-06-16. Score: 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 );
    }
  }
  
  > **Comments:**
  > **username_0**: Greetings Jon, That looked perfect. However, I no longer see the "checked / unchecked"... it just is unchecked always.  
  > **username_0**: 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.  
  > **username_1**: Michael - I'm not clear on what the current problem is.  Can you restate it?  
  > **username_0**: 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.  
  
  ---
  
  ### Answer by username_2 on 2015-05-27. Score: 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.
  
  > **Comments:**
  > **username_0**: 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. :-)  
  
  ---
  
  ### Answer by username_0 on 2015-06-01. Score: 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?
  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);
      });
    }
  
  }
  
  > **Comments:**
  > **username_3**: 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.  
  > **username_0**: Thank you username_3, 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.)  
  
  ---
  
  ### Answer by username_0 on 2015-06-03. Score: 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:
  
  And here is the form when the page finishes loading:
  
  So, this is the only CiviCRM customization on the site, what else could be adjusting the contribution 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
  
  
  ---
  
  