Some example code if you want to remove certain elements on the contact add and edit forms:
/**
* Implements hook_civicrm_buildForm().
*
* Changes some fields on the add and update contact forms
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function MODULE_civicrm_buildForm($formName, &$form) {
// print_r($formName);
if ($formName == 'CRM_Contact_Form_Contact') {
if (($form->getAction() == CRM_Core_Action::ADD) || ($form->getAction() == CRM_Core_Action::UPDATE)) {
// print_r($form);
// Add a javascript file for changes in display
CRM_Core_Resources::singleton()->addScriptUrl('https://EXAMPLE.ORG/sites/all/themes/EXAMPLE/EXAMPLE.js');
// Remove field override geocoding
for ($i = 0; $i <= 5; $i++) {
if ($form->elementExists('address[' . $i . '][manual_geo_code]')) {
$form->removeElement('address[' . $i . '][manual_geo_code]');
// $element = &$form->getElement('address[1][manual_geo_code]');
// crm_Core_error::debug( 'options', $element->_options );
// also you can add new options w/ $element->addOption( );
}
}
// Remove field signature text
for ($i = 0; $i <= 5; $i++) {
if ($form->elementExists('email[' . $i . '][signature_text]')) {
$form->removeElement('email[' . $i . '][signature_text]');
}
}
// Remove field signature html
for ($i = 0; $i <= 5; $i++) {
if ($form->elementExists('email[' . $i . '][signature_html]')) {
$form->removeElement('email[' . $i . '][signature_html]');
}
}
// Remove field external id
if ($form->elementExists('external_identifier')) {
$form->removeElement('external_identifier');
}
// Remove field nickname
if ($form->elementExists('nick_name')) {
$form->removeElement('nick_name');
}
if ($form->getAction() == CRM_Core_Action::ADD) {
// we can get the contact type from the url ct parameter: Individual/household/organisation
$contactType = CRM_Utils_Array::value('ct', $_GET, '0');
}
elseif ($form->getAction() == CRM_Core_Action::UPDATE) {
// there is no info in the url so we need to use the contact id and check
$contactId = CRM_Utils_Array::value('cid', $_GET, '0');
if ($contactId != 0) {
$contactType = CRM_Contact_BAO_Contact::getContactType($contactId);
// $subType = CRM_Contact_BAO_Contact::getContactSubType($contactId);
}
}
// Household
if ($contactType == 'Household') {
// Remove custom block
unset($form->_groupTree[3]);
// Remove custom block
unset($form->_groupTree[7]);
// Change label of household name
if ($form->elementExists('household_name')) {
$element = &$form->getElement('household_name');
$element->setLabel('NEW LABEL');
}
}
elseif ($contactType == 'Organization') {
// Remove custom block
unset($form->_groupTree[1]);
// Remove custom block
unset($form->_groupTree[7]);
if ($form->elementExists('sic_code')) {
$form->removeElement('sic_code');
}
}
else {
// Individual
}
}
}
}