diff --git a/docs/hooks/hook_civicrm_searchKitTasks.md b/docs/hooks/hook_civicrm_searchKitTasks.md index db27de9b964b7fbd871d15628300874fc5bba39a..58e5a23821f0e770007c493b923b8816f7fbef54 100644 --- a/docs/hooks/hook_civicrm_searchKitTasks.md +++ b/docs/hooks/hook_civicrm_searchKitTasks.md @@ -35,7 +35,54 @@ This hook is called to allow you to add to or alter the tasks available in searc - null -## Example (Add a new task) +## Example: Add a new task with a generic interface + + /** + * Implements hook_civicrm_searchKitTasks(). + * + * @param array[] $tasks + * @param bool $checkPermissions + * @param int|null $userID + */ + function myext_civicrm_searchKitTasks(array &$tasks, bool $checkPermissions, ?int $userID) { + $tasks['Contact']['myAction'] = [ + 'title' => E::ts('My SearchKit Task'), + 'icon' => 'fa-random', + 'apiBatch' => [ + 'action' => 'myAction', + 'confirmMsg' => E::ts('Are you sure you want to run myAction on %1 contacts?'), + 'runMsg' => E::ts('Running myAction on %1 contacts...'), + 'successMsg' => E::ts('Successfully ran myAction on %1 contacts.'), + 'errorMsg' => E::ts('An error occurred while attempting to run myAction on %1 contacts.'), + ], + ]; + } + +The `myAction` is assumed to be a custom APIv4 action provided by your extension. For example, create the file `Civi/Api4/Action/Contact/MyAction.php` with the following code: + +```php +<?php + +namespace Civi\Api4\Action\Contact; + +use Civi; + +class MyAction extends \Civi\Api4\Generic\BasicBatchAction { + + protected function doTask($item) { + foreach ($item as $key => $contact_id) { + // todo whatever you need to do + } + + return $item; + } + +} +``` + +Make sure to use the correct namespace for your entity (Contact, Activity, etc), and to flush the CiviCRM cache after adding the new code. + +## Example: Add a new task with a custom interface /** * Implements hook_civicrm_searchKitTasks().