I created some database triggers that update a read only custom field that is displayed on the main contacts page. You change the value in another field and the db triggers update the custom field.

So how do I get that read only field to be refreshed with the new value or, at the very least, just refresh the entire page?

The civicrm ajax wiki page suggests CRM.refreshParent so I tried to put that in a post processing hook

function ccas_hooks_civicrm_postProcess($formName, &$form){
    if ( $formName == 'CRM_Contact_Form_Contact' or  $formName ==   
             'CRM_Contact_Form_Inline_CustomData') {

Of course, that didn't work. I ended up with following two error messages

Notice: Use of undefined constant CRM - assumed 'CRM' in ccas_hooks_civicrm_postProcess() (line 300 of ...

Notice: Use of undefined constant refreshParent - assumed 'refreshParent' in ccas_hooks_civicrm_postProcess() (line 300...

  • 1
    It looks like you are confusing javascript with PHP. CRM.refreshParent is a javascript function. – Coleman Mar 29 '15 at 17:02
  • 1
    Note: database triggers are normally aggregated by CiviCRM to get around the 1-trigger-per-table limit imposed by MySQL. So manually creating them might cause trouble. – Coleman Mar 29 '15 at 22:00
  • Excellent point about triggers. I believe the hook_civicrm_trigger_info (wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_trigger) allows one to add custom triggers logic that gets compiled into the core triggers. – Paul-Tahoe Mar 31 '15 at 0:22

The contact summary screen is divided into different panes, and they are each refreshed separately. If a popup form needs to refresh one or more panes, it can specify them by jQuery selector. For example, the relationship form has this in its post-process function:

$this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');

Which has the effect of refreshing the pane that contains, among other things, the current employer (which the relationship form may affect).

When you implement this in your form hook it will look like:

function ccas_hooks_civicrm_postProcess($formName, &$form){
  if ($formName == 'CRM_Contact_Form_Inline_CustomData') {
    $form->ajaxResponse['reloadBlocks'][] = '#crm-contactinfo-content';

Or substitute/add the jQuery selectors of whichever other blocks you'd like to see refreshed. Note that this code uses the array append syntax rather than overwriting the entire array.

  • I wasn't really trying to target the main contact edit form. I blindly copied some of the code I used for hooks_civicrm_validateForm, which does need to target both. – Paul-Tahoe Mar 31 '15 at 0:26
  • Thank you for an answer and excellent example. Unfortunately, one can not just stick the $this->ajaxResponse... in the hooks code. $this is not in object context. In the example, it's used in a function within a class. Since the example is extending CRM_Core_Form, I'd try the same thing but alas it complained of not finding class CRM_Core_Form. I would have been surprised if it had worked. Clearly, I could do the exact same thing by creating a custom form and adding the post thing, but it seems like a lot of work and maintenance – Paul-Tahoe Mar 31 '15 at 1:15
  • The variable $form gets passed into form-related hooks, which is a copy of the form object, so just change the code to $form->ajaxResponse... – Coleman Mar 31 '15 at 1:53
  • Ah, thank you, thank you. I didn't see the forest for the trees. It was right there in front of me. – Paul-Tahoe Mar 31 '15 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.