Skip to content

hook_civicrm_customPre

Summary

This hook is called before the database write on a custom table.

Definition

hook_civicrm_customPre(string $op, int $groupID, int $entityID, array &$params)

Parameters

  • string $op - the type of operation being performed
  • int $groupID - the custom group ID
  • int $entityID - the entityID of the row in the custom table
  • array $params - the parameters that were sent into the calling function

Returns

  • null - the return value is ignored

Example

/**
 * This example compares the submitted value of a field with its current value.
 *
 * @param string $op
 *   The type of operation being performed.
 * @param int $groupID
 *   The custom group ID.
 * @param int $entityID
 *   The entityID of the row in the custom table.
 * @param array $params
 *   The parameters that were sent into the calling function.
 */
function MODULENAME_civicrm_customPre(string $op, int $groupID, int $entityID, array &$params): void {
  foreach ($params as $field) {
    if ($field['column_name'] == 'pipeline_stage') {
      //get existing value
      try {
        $existingValue = civicrm_api3('Contact', 'getvalue', [
          'id' => $field['entity_id'],
          'return' => "custom_{$field['custom_field_id']}",
        ]);

        if ($existingValue != $field['value']) {
          //create Activity to record the change
        }
      }
      catch (CiviCRM_API3_Exception $e) {}
    }
  }
}