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 »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Changing fields dynamically when user selects a membership type
Pages: [1]

Author Topic: Changing fields dynamically when user selects a membership type  (Read 506 times)

Gordon Fern

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5.37
  • PHP version: 5.4.29
Changing fields dynamically when user selects a membership type
June 12, 2014, 11:58:12 pm
I want to basically change which fields in a membership registration profile are mandatory and the defaults when a user selects a membership type.  Eg. So if institution membership is chosen fields asking about the institution become mandatory otherwise they are optional.  Also amend defaults such as receive journals (custom fields) any ideas on how to approach this.  Is this something that can be easily done or will it require custom code using a hook?

Update: In the end I solved the membership problem by putting a front page up in Drupal explaining the options then directing to a different membership contribution page for each type of membership.  This allowed me to configure a different number of fields required and change which where mandatory.  This left the changing of default values which I found no way of achieving through configuration.  So I implemented a custom module to patch default values.  Not very pretty code and hardly very generic (couldn't justify the expense of doing much more than a hack fix to the customer)


<module>.module  file
<?php

require_once('include/config.inc');


function <module-name>_civicrm_buildForm($formName, &$form) {

   if ($formName == "CRM_Contribute_Form_Contribution_Main") // Only want contribution pages
   {
      $form_id=CRM_Utils_Array::value('id',$_GET,'0');
      $form_ids=MyAppConstants::getForms();
      if (array_key_exists($form_id,$form_ids)) // Only want contribution pages known about
      {
         $contrib_name= $form_ids[$form_id];

         $config_file=CONFIG_PATH . "/" . $contrib_name . ".defaults";  // Suggest CONFIG_PATH is altered to something meaningful to you
         $fh=@fopen($config_file,"r");
         if ($fh) // Only interested in pages that an existent defaults file
         {
            while(($line=fgets($fh)) != false)
            {
                $pairs=explode("=",$line);
                $fields=MyAppConstants::getFields();
                $pairs[0] = trim($pairs[0]);
                $pairs[1] = trim($pairs[1]);
                if ( strlen($pairs[0]) > 0 )
                   $defaults[$fields[$pairs[0]]]=$pairs[1];
            }
            $form->setDefaults($defaults);
         }
         @fclose($fh);

      }
   }

}


?>

config.inc

<?php

/*
* Contains the configuration required for the adpca_custom module
*/

define('CONFIG_PATH', "/home//..../sites/default/files/<module>");

class MyAppConstants {
   private static $fields = array(
   "title"         =>   "prefix_id",
   "first_name"      =>   "first_name",
   "middle_name"      =>   "middle_name",
   "last_name"      =>   "last_name",
   "street_address"      =>   "street_address-Primary",
   "address_1"      =>   "supplemental_address_1-Primary",
   "address_2"      =>   "supplemental_address_2-Primary",
   "city"         =>   "city-Primary",
   "postal_code"      =>   "postal_code-Primary",
   "country"      =>   "country-Primary",
   "state"         =>   "state_province-Primary",
        "phone"         =>   "phone-ext-Primary-1",
   "mobile"      =>   "phone-Primary-2",
   "directory_view"   =>   "custom_1",
   "suppress_mail_address"   =>   "custom_4",
   "do_not_send_journal"   =>   "custom_5",
   "send_hard_copy_newsletter"=>   "custom_6",
   "do_not_email"      =>   "do_not_email",
   "do_not_mail"      =>   "do_not_mail",
   "couple_membership_partner"=>   "custom_7",
   "school_college_name"   =>   "custom_8",
        "graduation_date"   =>   "custom_9");
   private static $cont_forms=array(
   "3"         =>   "membership_individual",
   "4"         =>   "membership_student",
   "5"         =>   "membership_institutional",
   "6"         =>   "membership_supporting",
   "7"         =>   "membership_journal");
   public static function getFields() {
        return self::$fields;
   }
   public static function getForms() {
        return self::$cont_forms;
   }

}

?>

Typical config file

example.defaults
do_not_mail=1
do_not_email=0
« Last Edit: July 01, 2014, 03:08:40 am by Gordon Fern »

joanne

  • Administrator
  • Ask me questions
  • *****
  • Posts: 852
  • Karma: 83
  • CiviCRM version: 4.4.16
  • CMS version: Drupal 7
Re: Changing fields dynamically when user selects a membership type
June 13, 2014, 01:09:30 am
If you have one CiviCRM contribution page with a price set offering different membership types then you would need to custom code to achieve what you want.

The non-code alternative would be an introductory drupal page where they select the membership type they want with links through to separate contribution pages for each membership type.  Then you can have profiles and price sets specifically created for each membership type.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Changing fields dynamically when user selects a membership type
June 13, 2014, 12:03:20 pm
Another non-code alternative would be to use drupal-civicrm-webform integration and set up your membership form as a webform. Then you can take advantage of conditional fields and other webform features.
Try asking your question on the new CiviCRM help site.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Changing fields dynamically when user selects a membership type

This forum was archived on 2017-11-26.