Skip to content

Optionally Check For Existing Form Defaults Before Setting Form Default Values

tunbola@compucorp.co.uk requested to merge add-condition-for-default-values into master

Overview

The default values extension does its job well by setting default values for provided form fields for a form. However sometimes we want to set a default value conditionally, i.e to set a default value for a form field based on the already pre-set default value of another field in the form. A good example is the activity form that allows to set the value of the activity type field by passing the value in the URL. e.g civicrm/case/activity?action=add&atype=1 This URL will open the activity form with the meeting activity type already selected. Currently it is not possible to set the default value of the activity status field based on the activity type default. For example, When the Meeting activity form is opened, the activity status Scheduled should be selected by default and when the Phone call activity form is opened, the activity status Completed should be selected by default.

This PR makes the changes to allow the setting of default value on the form to be more flexible by allowing to check the default values of some fields on the form first before apply the default value to be set. More than one conditional fields to check the default values for can be added and for now the AND operator exists when there are multiple conditional fields to check.

Before

This functionality does not exist

After

Updated screen showing configuration of form defaults with conditional fields. DefaultValues

Screen showing the activity form with different configuration for setting the default activity status based on pre-selected activity type.

Meeting

Technical Details

  • Changes were made to the Config template to allow adding the optional conditional fields
  • The getDefaults function of the CRM_Defaultvalues_BAO_Defaultvalues was modified to setting the default value for the same field name multiple times. Initially this was not possible as the field value would be overriden to return only the latest one that is set. This change is necessary because is now possible to want to set the default value of a particular fields multiple times based on certain field conditions.
  • A new is_condition_valid function was added to check if the condition for setting the form field defaults for a form.
function is_condition_valid($form, $conditional) {
  $form_default_values = $form->_defaults;
  foreach ($conditional as $cond) {
    if (!isset($cond['condition_field']) && !isset($cond['condition_value'])) {
      continue;
    }
    if (!isset($form_default_values[$cond['condition_field']])) {
      continue;
    }
    if ($form_default_values[$cond['condition_field']] != $cond['condition_value']) {
      return FALSE;
    }
  }

  return TRUE;
}
Edited by tunbola@compucorp.co.uk

Merge request reports