Skip to content
Snippets Groups Projects
Commit 45408d11 authored by colemanw's avatar colemanw
Browse files

Merge branch 'searchKitTasksGeneric' into 'master'

hooks/hook_civicrm_searchKitTasks: add generic task example

See merge request documentation/docs/dev!1169
parents 95055f92 aec22e7c
No related branches found
No related tags found
No related merge requests found
......@@ -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().
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment