A word on value lists
Some form elements require fetching value lists from the database. CiviCrm offers the following facilities to help building form elements with custom data:
- option lists (documentation missing)
- multiple choices values from custom fields (documentation missing)
Generating form elements
Text input
$form->add( 'text', 'nom', ts( 'Contact Name' ) );
Textarea
$form->addElement('textarea','address_format', ts('Display Format'));
Hidden fields
$form->addElement('hidden','contact_edit_prefences', null, array('id'=> 'contact_edit_prefences') );
Checkboxes
$form->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?') );
Radio buttons
$gender = array('' => ts('- any gender -')) + CRM_Core_PseudoConstant::gender( ); foreach ($gender as $key => $var) { $genderOptions[$key] = HTML_QuickForm::createElement('radio', null, ts('Gender'), $var, $key); } $form->addGroup($genderOptions, 'gender_id', ts('Gender'));
Tri-value Boolean fields
(documentation missing -- there's probably a better way to do this)
$boolean = array(); $boolean[] = &HTML_QuickForm::createElement('radio', NULL, NULL, 'Yes', 1); $boolean[] = &HTML_QuickForm::createElement('radio', NULL, NULL, 'No', 0); $boolean[] = &HTML_QuickForm::createElement('radio', NULL, NULL, 'Either', ''); $form->addGroup( $boolean, 'handicap', ( 'Handicap' ) );
Combo boxes
$stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant::stateProvince( ); $form->addElement('select', 'state_province_id', ts('Individual\'s State'), $stateProvince);
Multiple selections
$includeCountry =& $form->addElement('advmultiselect', 'countryLimit', ts('Available Countries') . ' ', $country, array('size' => 5, 'style' => 'width:150px', 'class' => 'advmultiselect') ); $includeCountry->setButtonAttributes('add', array('value' => ts('Add >>'))); $includeCountry->setButtonAttributes('remove', array('value' => ts('<< Remove')));
Date fields
(some documentation missing)
$yearsInPast = 0; $yearsInFuture = 1; $dateParts = implode( CRM_Core_DAO::VALUE_SEPARATOR, array( 'Y', 'M' ) ); $form->add( 'date', 'date_birth', ts('Birth Date'), CRM_Core_SelectValues::date('custom', $yearsInPast, $yearsInFuture, $dateParts ) );
See also:
Modifying existing form elements
This is particularly useful in a CiviCRM hookBuildForm()
Remove an element
$form->removeElement('cvv2');
Although in this specific case, removing the cvv2 field for online payment (contribution forms) caused weird loops in the forms without throwing an error message, so setting a value and hiding it (in the smarty templates) was easier:
$cvv2 =& $form->getElement('cvv2'); $cvv2->setValue('000');
or
$defaults['cvv2'] = '000'; $form->setDefaults($defaults);
Radio buttons (ex: donation amount)
function civicrmdesjardins_civicrm_buildForm($formName, &$form) { /* * Donation forms: format amount 10.00$ to 10$ */ if ($formName == 'CRM_Contribute_Form_Contribution_Main') { $amounts =& $form->getElement('amount'); $elements =& $amounts->getElements(); foreach ($elements as $key => $val) { $text = $elements[$key]->getText(); $text = preg_replace('/\.00 \$/', '$', $text); $elements[$key]->setText($text); } } }
Force a default value
if ($form->elementExists('activity_type_id')) { $e =& $form->getElement('activity_type_id'); $e->setValue(1); $e->freeze(); }
Removing the WYSIWYG on a specific field
For example: the "Generate PDF letter" can mess up a template if it has smarty inside it.
/** * hook_civicrm_buildForm */ function fooexample_civicrm_buildForm( $formName, &$form ) { if ($formName == 'CRM_Contact_Form_Task_PDF') { // Remove the WYSIWYG on the PDF letter form. $e = $form->getElement('html_message'); $form->removeElement('html_message'); $attributes = array('value' => $e->getValue()); $form->addElement('textarea','html_message', $e->getLabel(), $attributes); $form->assign('editor', 'textarea'); } }
References
4012888888881881