Answers.Body: <p>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.</p>
  
  <p>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 :)</p>
  
  <p>Take a look at the JS inlined on a default CiviCRM contribute form, eg the <a href="http://d46.demo.civicrm.org/civicrm/contribute/transact?reset=1&amp;id=2&amp;action=preview" rel="nofollow noreferrer">CiviCRM Demo site's Member Signup and Renew form</a> when configured to permit optional membership auto-renew.</p>
  
  <p>If you inspect the code immediately after the <code>auto_renew</code> element, you'll see this block coming from CiviCRM's templates.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  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();
      }
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>If I'm reading all that right, and with the caveat of not having seen your actual site at all, <em>I think you can configure this from your contribute form settings</em> for each of the form IDs listed above. The setting is on the "Membership" tab and looks like this:</p>
  
  <p><img src="https://i.sstatic.net/oQxLw.png" alt="membership renew settings"></p>
  
  <p>The recurring defaults are tied to the settings in the Contribute form (and depend on settings in both Membership Type and Payment Processor config).</p>
  
  <p>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.</p>
  
  <p>You can certainly do it via JS though, as demonstrated by <a href="https://contribute.greens.org.nz/civicrm/contribute/transact?reset=1&amp;id=17" rel="nofollow noreferrer">this customised contribute form</a> (<a href="https://contribute.greens.org.nz/sites/default/civicrm/extensions/nz.org.greens.nzgcustom/js/contribute.js" rel="nofollow noreferrer">js here</a>).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T22:40:58.353Z
Answers.Text: 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.
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T22:44:00.437Z
Answers.Text: 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!
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T23:17:35.957Z
Answers.Text: 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! :-)
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-18T23:20:18.623Z
Answers.Text: Hi Michael (sorry, I addressed you as Martin on the forum!). I've updated the answer above. Good luck!
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-20T02:10:22.970Z
Answers.Text: 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.
Answers.UserDisplayName: Michael Lueck
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-20T09:18:35.727Z
Answers.Text: I would recommend you to (1) install [civix](https://github.com/totten/civix) (2) use `civix generate:module` to run yourself up a small CiviCRM extension (3) copy in your existing `hook_civicrm_buildForm` and the template you want to customise (4) adjust the JS to do your bidding. This packages the work nicely (instead of having your hook sitting in Drupal's modules and your templates off in a random CiviCRM folder).
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T12:22:06.093Z
Answers.LastActivityDate: 2015-06-19T01:39:13.857Z
Answers.LastEditDate: 2015-06-19T01:39:13.857Z
Answers.UserDisplayName: Chris Burgess
Answers.Body: <p>If you want a more "Drupal-style" form which you can hook_alter() wantonly, look into using CiviCRM Contribute forms with <a href="https://drupal.org/project/webform_civicrm" rel="nofollow">Webform CiviCRM</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T12:24:39.753Z
Answers.LastActivityDate: 2015-06-16T12:24:39.753Z
Answers.UserDisplayName: Chris Burgess
Body: <p>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:</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>Initially when the CiviContribution forms are loading, the Drupal module fires in the desired form data change... The recurring contribution check box is checked.</p>
  
  <p><img src="https://i.sstatic.net/S2E7Z.png" alt="enter image description here"></p>
  
  <p>However when the form finishes loading the check box flips back to unchecked, and the default selection price becomes bolded simultaneously.</p>
  
  <p><img src="https://i.sstatic.net/kYMco.png" alt="enter image description here"></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I am thankful,</p>
  
  <p>Michael</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-16T12:41:32.817Z
Comments.Text: possible duplicate of [Drupal module to check "auto renew" checkbox for membership getting overridden, why?](http://civicrm.stackexchange.com/questions/2716/drupal-module-to-check-auto-renew-checkbox-for-membership-getting-overridden)
Comments.UserDisplayName: Laryn
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-17T22:42:56.913Z
Comments.Text: Because I was told to here: http://forum.civicrm.org/index.php/topic,36534.msg155289.html#msg155268
Comments.UserDisplayName: Michael Lueck
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-17T22:52:14.670Z
Comments.Text: No worries -- I think Coleman's point was to post questions as questions (not in the answers area) but if it is the same question, you have the ability to edit it and add further relevant information. I believe this also bumps it back to the top so more eyes get on it. This prevents multiple partially answered aspects of the same question.
Comments.UserDisplayName: Laryn
Tags: <civicontribute><drupal>
Title: Customizing the CiviContribute form through Drupal module
ThreadText: # Customizing the CiviContribute form through Drupal module
  
  (Asked by: username_0 on 2015-06-16)
  
  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:
  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.
  
  However when the form finishes loading the check box flips back to unchecked, and the default selection price becomes bolded simultaneously.
  
  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
  
  **Tags:** civicontribute, drupal
  
  > **Comments:**
  > **username_1**: possible duplicate of [Drupal module to check "auto renew" checkbox for membership getting overridden, why?](http://civicrm.stackexchange.com/questions/2716/drupal-module-to-check-auto-renew-checkbox-for-membership-getting-overridden)  
  > **username_0**: Because I was told to here: http://forum.civicrm.org/index.php/topic,36534.msg155289.html#msg155268  
  > **username_1**: No worries -- I think Coleman's point was to post questions as questions (not in the answers area) but if it is the same question, you have the ability to edit it and add further relevant information. I believe this also bumps it back to the top so more eyes get on it. This prevents multiple partially answered aspects of the same question.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-06-16. Score: 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.
  
  
  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:
  
  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).
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: 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!  
  > **username_0**: 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! :-)  
  > **username_2**: Hi Michael (sorry, I addressed you as Martin on the forum!). I've updated the answer above. Good luck!  
  > **username_0**: 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.  
  > **username_2**: I would recommend you to (1) install [civix](https://github.com/totten/civix) (2) use `civix generate:module` to run yourself up a small CiviCRM extension (3) copy in your existing `hook_civicrm_buildForm` and the template you want to customise (4) adjust the JS to do your bidding. This packages the work nicely (instead of having your hook sitting in Drupal's modules and your templates off in a random CiviCRM folder).  
  
  ---
  
  ### Answer by username_2 on 2015-06-16. Score: 0
  
  If you want a more "Drupal-style" form which you can hook\_alter() wantonly, look into using CiviCRM Contribute forms with [Webform CiviCRM](https://drupal.org/project/webform_civicrm).
  
  
  ---
  
  