From 048b069c47a2deb61f9e754f896140f60bd0643f Mon Sep 17 00:00:00 2001 From: Brian Shaughnessy Date: Wed, 22 Apr 2020 11:52:15 -0400 Subject: [PATCH] dev/core#1716 add customPre hook --- CRM/Core/BAO/CustomValueTable.php | 42 ++++++++++++++++++------------- CRM/Utils/Hook.php | 20 +++++++++++++++ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CRM/Core/BAO/CustomValueTable.php b/CRM/Core/BAO/CustomValueTable.php index 1fa9a508a8..9452a33026 100644 --- a/CRM/Core/BAO/CustomValueTable.php +++ b/CRM/Core/BAO/CustomValueTable.php @@ -47,25 +47,31 @@ class CRM_Core_BAO_CustomValueTable { $set = []; $params = []; $count = 1; - foreach ($fields as $field) { - if (!$sqlOP) { - $entityID = $field['entity_id']; - $hookID = $field['custom_group_id']; - $isMultiple = $field['is_multiple']; - if (array_key_exists('id', $field)) { - $sqlOP = "UPDATE $tableName "; - $where = " WHERE id = %{$count}"; - $params[$count] = [$field['id'], 'Integer']; - $count++; - $hookOP = 'edit'; - } - else { - $sqlOP = "INSERT INTO $tableName "; - $where = NULL; - $hookOP = 'create'; - } - } + $firstField = reset($fields); + $entityID = $firstField['entity_id']; + $hookID = $firstField['custom_group_id']; + $isMultiple = $firstField['is_multiple']; + if (array_key_exists('id', $firstField)) { + $sqlOP = "UPDATE $tableName "; + $where = " WHERE id = %{$count}"; + $params[$count] = [$firstField['id'], 'Integer']; + $count++; + $hookOP = 'edit'; + } + else { + $sqlOP = "INSERT INTO $tableName "; + $where = NULL; + $hookOP = 'create'; + } + + CRM_Utils_Hook::customPre($hookOP, + $hookID, + $entityID, + $fields + ); + + foreach ($fields as $field) { // fix the value before we store it $value = $field['value']; $type = $field['type']; diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index d634c0c7a1..857d451687 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -541,6 +541,26 @@ abstract class CRM_Utils_Hook { ->invoke(['op', 'groupID', 'entityID', 'params'], $op, $groupID, $entityID, $params, self::$_nullObject, self::$_nullObject, 'civicrm_custom'); } + /** + * This hook is called before a db write on a custom table. + * + * @param string $op + * The type of operation being performed. + * @param string $groupID + * The custom group ID. + * @param object $entityID + * The entityID of the row in the custom table. + * @param array $params + * The parameters that were sent into the calling function. + * + * @return null + * the return value is ignored + */ + public static function customPre($op, $groupID, $entityID, &$params) { + return self::singleton() + ->invoke(['op', 'groupID', 'entityID', 'params'], $op, $groupID, $entityID, $params, self::$_nullObject, self::$_nullObject, 'civicrm_customPre'); + } + /** * This hook is called when composing the ACL where clause to restrict * visibility of contacts to the logged in user -- 2.25.1