diff --git a/docs/hooks/changes.md b/docs/hooks/changes.md index fe3e7c49dda3bf960c5b7b679b419fdac0fb1a55..6be20d8c10c12a6f55cf9f620176507c7d10de99 100644 --- a/docs/hooks/changes.md +++ b/docs/hooks/changes.md @@ -6,7 +6,9 @@ For API changes, see [APIv4 Changelog](../api/v4/changes.md) and [APIv3 Changelo ## CiviCRM 5.x -### 5.47: hool_civicrm_pre - no longer called twice in some flows for relationship create (ie creating an employer relationship and importing contacts with a relationship) +### 5.61 hook_importAlterMappedRow added for contribution imports + +### 5.47: hook_civicrm_pre - no longer called twice in some flows for relationship create (ie creating an employer relationship and importing contacts with a relationship) ### 5.43: hool_civicrm_alterMailParams - parameter `workflow` added, `valueName` deprecated, removal of old deprecated `groupName` commenced diff --git a/docs/hooks/hook_civicrm_import.md b/docs/hooks/hook_civicrm_import.md index a1c8d70b74005a255cb2c0f5e12a3cbdf5ae3d5a..c0cd235a623a21f740620de80685de5d58ed561f 100644 --- a/docs/hooks/hook_civicrm_import.md +++ b/docs/hooks/hook_civicrm_import.md @@ -1,7 +1,11 @@ -# hook_civicrm_import +~~# hook_civicrm_import~~ ## Summary +This hook is deprecated. The description below may be unreliable. + +We are working to replace it with `hook_civicrm_importAlterMappedRow` + This hook is called after contacts have been imported into the system, and before the temp import table has been destroyed. @@ -14,7 +18,7 @@ in future versions may extend to other objects. ## Definition - hook_civicrm_import( $object, $usage, &$objectRef, &$params ) + hook_civicrm_import(string $object, $usage, &$objectRef, &$params) ## Parameters diff --git a/docs/hooks/hook_civicrm_importAlterMappedRow.md b/docs/hooks/hook_civicrm_importAlterMappedRow.md new file mode 100644 index 0000000000000000000000000000000000000000..51d6d253070beb2e47f396b3e7e69710a468cd4e --- /dev/null +++ b/docs/hooks/hook_civicrm_importAlterMappedRow.md @@ -0,0 +1,51 @@ +# hook_civicrm_importAlterMappedParams + +## Summary + +This hook allows an import row to be adjusted, after the user configuration has +been interpreted into an apiv4-ready format. It is called twice. The first time +the usage is `validate` and the second time the usage is `import`. In the validate +usage you should avoid doing any db lookups or anything resource intensive. Validate +mode is intended for a quick pass before presenting the preview screen. For +the `import` usage you can do more intensive lookups and change the data if needed. + +## Definition + + hook_civicrm_import_importAlterMappedRow(string $importType, string $context, array &$mappedRow, array $rowValues, int $userJobID) + +## Parameters + +- @param string $importType This corresponds to the value in `civicrm_user_job.job_type`. +- @param string $context import or validate. + In validate context only 'cheap' lookups should be done (e.g. using cached information). + Validate is intended to quickly process a whole file for errors. You should focus on + setting or unsetting key values to or from `'invalid_import_value'`. + + During import mode heavier lookups can be done (e.g using custom logic to find the + relevant contact) as this is then passed to the api functions. If a row is invalid during + import mode you should throw an exception. +- @param array $mappedRow (reference) The rows that have been mapped to an array of params. +- @param array $rowValues The row from the data source (non-associative array) +- @param int $userJobID id from civicrm_user_job + +## Availability + +This hook was first available in CiviCRM 5.61 for contribution imports. Other +imports will be added when the `$mappedRow` data is internally in APIv4 format +(as we want the data to be stable when the hook is added). + +## Example + +This example shows replacing the contact Organization name with one determined from +a custom function. (In this case the function is outside the example, but +it looks in the legal name & nick name fields for for the name) +```php +/** + * Implements hook_civicrm_importAlterMappedRow(). + */ + function my_extension_civicrm_importAlterMappedRow(string $importType, string $context, array &$mappedRow, array $rowValues, int $userJobID) { + if ($context === 'import' && $importType === 'contribution_import' && !empty($mappedRow['Contact']['organization_name'])) { + $mappedRow['Contact']['organization_name'] = Contact::resolveOrganizationName($mappedRow['Contact']['organization_name']); + } + } +```