CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Set default values for address and date feilds
Pages: [1]

Author Topic: Set default values for address and date feilds  (Read 1448 times)

noelg

  • Guest
Set default values for address and date feilds
August 20, 2009, 04:01:51 pm
Hello,

Is there anyway to set the state field to a default value for new members? I would also like to set the default year in the calender drop down as well.

Appreciate your help.

Noel

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Set default values for address and date feilds
August 20, 2009, 04:35:54 pm

yes, use the buildForm hook and then set the defaults in that hook. There is an example in:
 
file: drupal/civitest.module.sample, function civitest_civicrm_buildForm

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

noelg

  • Guest
Re: Set default values for address and date feilds
August 23, 2009, 04:08:36 pm
Thank you very much! :D

kmitz

  • I post occasionally
  • **
  • Posts: 74
  • Karma: 2
Re: Set default values for address and date feilds
March 06, 2010, 10:00:19 am
Lobo,

I found your example in the module mentioned above, but I'm not clear on what I have to do to make the state on the "New Individual" contact form default to Connecticut.  I've got the Drupal module working fine...no issues there.

I see that the field I want to set is:  address[1]['state_province_id'] with the id of the select element being 'address_1_state_province_id'.

and the value I want by default is Connecticut, or id=1006.

Code: [Select]
function eca_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contact_Form_Contact') {

    $defaults = array('address' => array(1 => array('state_province_id' => 1006)));
    $form->setDefaults( $defaults );

    //print_r($form);

  }
}

If I check the _defaultValues array by uncommenting the print_r and checking the source, my modification seems to be in the correct location.  I can see that state_province_id has the selected attribute set correctly at 1006, but for some reason it's not selected in the UI.

PS I will post the complete example once I get it working.
« Last Edit: March 06, 2010, 12:24:50 pm by kmitz »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Set default values for address and date feilds
March 06, 2010, 01:34:19 pm

can u try the following

Code: [Select]
    $defaults = array( 'address[1][state_province_id] => 1006 );

OR

    $defaults = array( 'address_1_state_province_id' => 1006 );

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

kmitz

  • I post occasionally
  • **
  • Posts: 74
  • Karma: 2
Re: Set default values for address and date feilds
March 06, 2010, 01:55:01 pm
Thanks Lobo.

My problem was that I kept refreshing the page, and my code somehow wasn't taking effect. When I selected a new Organization from the dropdown block, it worked properly.

Here's the complete module code:

Code: [Select]
function eca_civicrm_buildForm($formName, &$form) {
 
  // uncomment the next line, then navigate to the page in question to find out the form name
  // echo $formName;
 
  if ($formName == 'CRM_Contact_Form_Contact') {
   
    //all of these methods worked for me; take your pick
    $defaults = array('address' => array(1 => array('state_province_id' => 1006)));
    //$defaults['address[1][state_province_id]'] = 1006;
    //$defaults = array( 'address[1][state_province_id]' => 1006 );
   
    $form->setDefaults( $defaults );
    //print_r($form);
  }
}

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Set default values for address and date feilds

This forum was archived on 2017-11-26.