Building on Hans Rossel's helpful answer, below are paired PHP and javascript to remove some contact fields when adding or editing contacts.
Note there is a WordPress-specific function call to obtain the path to the javascript file, which I assume is stored in the .../plugins/CUSTOMPLUGIN/assets/
folder, with a define('WP_CUSTOMPLUGIN_FILE', __FILE__);
directive in the main plugin PHP file.
PHP hook
/**
* 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) {
if ($formName !== 'CRM_Contact_Form_Contact') {
return;
}
if (($form->getAction() !== CRM_Core_Action::ADD) && ($form->getAction() !== CRM_Core_Action::UPDATE)) {
return;
}
// ***WORDPRESS-SPECIFIC: javascript for contacts edit page
$jsUrl = plugins_url('assets/cv_editcntct.js', WP_CUSTOMPLUGIN_FILE);
CRM_Core_Resources::singleton()->addScriptUrl($jsUrl);
// Remove some address fields
for ($i = 0; $i <= 5; $i++) {
// geocoding
if ($form->elementExists('address[' . $i . '][geo_code_1]')) {
$form->removeElement('address[' . $i . '][geo_code_1]');
$form->removeElement('address[' . $i . '][geo_code_2]');
}
// county
if ($form->elementExists('address[' . $i . '][county_id]')) {
$form->removeElement('address[' . $i . '][county_id]');
}
}
// See also cv_editcntct.js
for ($i = 0; $i <= 5; $i++) {
// Remove field signature text
if ($form->elementExists('email[' . $i . '][signature_text]')) {
$form->removeElement('email[' . $i . '][signature_text]');
}
// Remove field signature html
if ($form->elementExists('email[' . $i . '][signature_html]')) {
$form->removeElement('email[' . $i . '][signature_html]');
}
}
if ($form->elementExists('employer_id')) {
$form->removeElement('employer_id');
}
if ($form->elementExists('job_title')) {
$form->removeElement('job_title');
}
if ($form->elementExists('image_URL')) {
$form->removeElement('image_URL');
}
$contactType = NULL;
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);
}
}
// Adjustments specific to contact type...
if ($contactType == 'Household') {
// no Household customizations
}
elseif ($contactType == 'Organization') {
// // Remove custom block
// unset($form->_groupTree[1]);
if ($form->elementExists('sic_code')) {
$form->removeElement('sic_code');
}
}
else {
// Individual
// // Remove field nickname
// if ($form->elementExists('nick_name')) {
// $form->removeElement('nick_name');
// }
}
}
Javascript cv_editcntct.js
file
(function($, undefined){
//console.log('hello from cv_editcntct.js');
// for individual: employer and job title has been removed
// adjust layout with or without nickname
var $st = $('#contact_sub_type');
var $nn = $st.closest('tr').find('#nick_name');
if ($nn.length) {
// remove all (empty) TD cells prior to nickname and adjust width
var $td = $nn.closest('td').attr('colspan','2');
$td.prevAll().remove();
$nn.css('width',$td.width()+'px');
// also adjust subtype width
$td = $st.closest('td').attr('colspan','3');
$st.css('width',$td.width()+'px');
} else {
// remove empty TD cells before subtype, and adjust subtype width
var $td = $st.closest('td').attr('colspan','3');
$td.prevAll().remove();
$st.css('width',$td.width()+'px');
}
// remove (empty) image URL section (table)
$('table.image_URL-section').remove();
// remove (empty) signature blocks
$('.email-signature.crm-collapsible').remove();
// since new emails can be added, monitor the DOM for added elements
// and remove signature blocks
$emailSection = $('.crm-section.contact_information-section');
if ($emailSection.length) {
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
var bAdded = false;
for (const mutation of mutationsList) {
if (mutation.addedNodes.length) {
bAdded = true;
break;
}
}
if (bAdded) {
// remove (empty) signature blocks
var $s = $('.email-signature.crm-collapsible');
if ($s.length) {
$s.remove();
console.log("Zapped a signature block!");
}
}
};
// Create a DOM observer
const observer = new MutationObserver(callback);
// Start watching for element additions (or removals) in the email section
observer.observe($emailSection.get(0), { childList: true, subtree: true });
}
})(jQuery);