Have a question about CiviCRM? Get it answered quickly at the new CiviCRM Stack Exchange Q+A siteThis forum was archived on 25 November 2017. Learn more.How to get involved.What to do if you think you've found a bug.
/** 4 * Implementation of hook_civicrm_buildForm(). 5 */ 6 function modulename_civicrm_buildForm($formName, &$form) { 7 // CRM_Contribute_Form_Contribution_Main is the contribution page form. 8 if ($formName == 'CRM_Contribute_Form_Contribution_Main') { 9 // The membership placeholder line item needs to have a default value set 10 // that is greater than or equal to the largest membership fee. 11 $maxPrice = 0; 12 13 require_once drupal_get_path('module', 'civicrm') .'/../api/Membership.php'; 14 15 // Membership type IDs are stashed in membership block as comma separated 16 // string. 17 $types = explode(',', $form->_membershipBlock['membership_types']); 18 $prices = array(); 19 foreach ($types as $type) { 20 21 // Get the membership type. 22 $membership_type = crm_get_membership_types(array('id' => $type)); 23 if (!is_a($membership_type, 'CRM_Core_Error')) { 24 25 // Stash the price by Membership type ID. 26 $prices[$type] = $membership_type[$type]['minimum_fee']; 27 28 // Keep track of the largest price in the membership block. 29 if ($maxPrice < $membership_type[$type]['minimum_fee']) { 30 $maxPrice = $membership_type[$type]['minimum_fee']; 31 } 32 } 33 } 34 35 // _elementIndex keys are recognizeable names while the value is an integer 36 // that is the index of the actual _elements array in the form object. 37 $index = $form->_elementIndex['selectMembership']; 38 $selectMembership = $form->_elements[$index]; 39 40 // TODO: This loop could me merged with the previous loop because the number 41 // of types in the membership block should be the same as the number of 42 // elements in the selectMembership group in the form. 43 foreach ($selectMembership->_elements as &$element) { 44 45 // Nothing in the input element refers to the membership type except for 46 // the value attribute of the HTML element. 47 $membership_type = $element->getAttribute('value'); 48 $element->setAttribute('price', json_encode(array($membership_type, $prices[$membership_type]))); 49 } 50 51 // Set the membership placeholder to an always valid value, the most 52 // expensive membership in the block. 53 $index = $form->_elementIndex['price_8']; 54 $element =& $form->_elements[$index]; 55 $element->setAttribute('value', (int) $maxPrice); 56 57 // Removing the price attribute causes the script to ignore the field's 58 // value when calculating the JS UI total. 59 $element->removeAttribute('price'); 60 } 61 } 63 /** 64 * Implementation of hook_civicrm_validate(). 65 */ 66 function modulename_civicrm_validate($formName, &$fields, &$files, &$form) { 67 if ($formName == 'CRM_Contribute_Form_Contribution_Main') { 68 69 // Grab the membership type the user selected. 70 // TODO: Handle no input. I suspect if the user chooses nothing, the largest 71 // membership price will be in the total. 72 $selectMembership = $fields['selectMembership']; 73 $membership_type = crm_get_membership_types(array('id' => $selectMembership)); 74 75 // See matt2000: http://pastebin.com/cY4ywrUL 76 $data =& $form->controller->container(); 77 $data['values']['Main']['price_8'] = $membership_type[$selectMembership]['minimum_fee']; 78 } 79 }
function joomla_civicrm_buildForm($formName, &$form) { // CRM_Contribute_Form_Contribution_Main is the contribution page form. if ($formName == 'CRM_Contribute_Form_Contribution_Main') { // The membership placeholder line item needs to have a default value set // that is greater than or equal to the largest membership fee. $maxPrice = 270; require_once '/home/nyspa/nyspa15/administrator/components/com_civicrm/civicrm/api/Membership.php'; // Membership type IDs are stashed in membership block as comma separated // string. $types = explode(',', $form->_membershipBlock['membership_types']); $prices = array(); foreach ($types as $type) { // Get the membership type. $membership_type = crm_get_membership_types(array('id' => $type)); if (!is_a($membership_type, 'CRM_Core_Error')) { // Stash the price by Membership type ID. $prices[$type] = $membership_type[$type]['minimum_fee']; // Keep track of the largest price in the membership block. if ($maxPrice < $membership_type[$type]['minimum_fee']) { $maxPrice = $membership_type[$type]['minimum_fee']; } } } // _elementIndex keys are recognizeable names while the value is an integer // that is the index of the actual _elements array in the form object. $index = $form->_elementIndex['selectMembership']; $selectMembership = $form->_elements[$index]; // TODO: This loop could me merged with the previous loop because the number // of types in the membership block should be the same as the number of // elements in the selectMembership group in the form. foreach ($selectMembership->_elements as &$element) { // Nothing in the input element refers to the membership type except for // the value attribute of the HTML element. $membership_type = $element->getAttribute('value'); $element->setAttribute('price', json_encode(array($membership_type, $prices[$membership_type]))); } // Set the membership placeholder to an always valid value, the most // expensive membership in the block. $index = $form->_elementIndex['price_16']; $element =& $form->_elements[$index]; $element->setAttribute('value', (int) $maxPrice); // Removing the price attribute causes the script to ignore the field's // value when calculating the JS UI total. $element->removeAttribute('price'); } } /** * Implementation of hook_civicrm_validate(). */ function joomla_civicrm_validate($formName, &$fields, &$files, &$form) { if ($formName == 'CRM_Contribute_Form_Contribution_Main') { // Grab the membership type the user selected. // TODO: Handle no input. I suspect if the user chooses nothing, the largest // membership price will be in the total. $selectMembership = $fields['selectMembership']; $membership_type = crm_get_membership_types(array('id' => $selectMembership)); // See matt2000: http://pastebin.com/cY4ywrUL $data =& $form->controller->container(); $data['values']['Main']['price_16'] = $membership_type[$selectMembership]['minimum_fee']; } }