Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchKit - Update hook_civicrm_searchKitTasks signature #20467

Merged
merged 1 commit into from Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php
Expand Up @@ -64,10 +64,16 @@ public function _run(\Civi\Api4\Generic\Result $result) {
}

if ($entity['name'] === 'Contact') {
// Add contact tasks which support standalone mode (with a 'url' property)
$contactTasks = \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission());
// Add contact tasks which support standalone mode
$contactTasks = $this->checkPermissions ? \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission()) : NULL;
foreach (\CRM_Contact_Task::tasks() as $id => $task) {
if (isset($contactTasks[$id]) && !empty($task['url']) && $task['url'] !== 'civicrm/task/delete-contact') {
if (
(!$this->checkPermissions || isset($contactTasks[$id])) &&
// Must support standalone mode (with a 'url' property)
!empty($task['url']) &&
// The delete task is redundant with the new api-based one
$task['url'] !== 'civicrm/task/delete-contact'
) {
if ($task['url'] === 'civicrm/task/pick-profile') {
$task['title'] = E::ts('Profile Update');
}
Expand All @@ -84,6 +90,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {
}

if ($entity['name'] === 'Contribution') {
// FIXME: tasks() function always checks permissions, should respect `$this->checkPermissions`
foreach (\CRM_Contribute_Task::tasks() as $id => $task) {
if (!empty($task['url'])) {
$tasks[$entity['name']]['contribution.' . $id] = [
Expand All @@ -98,9 +105,19 @@ public function _run(\Civi\Api4\Generic\Result $result) {
}
}

// Call `hook_civicrm_searchKitTasks`.
// Note - this hook serves 2 purposes, both to augment this list of tasks AND to
// get a full list of Angular modules which provide tasks. That's why this hook needs
// the base-level array and not just the array of tasks for `$this->entity`.
// Although it may seem wasteful to have extensions add tasks for all possible entities and then
// discard most of it (all but the ones relevant to `$this->entity`), it's necessary to do it this way
// so that they can be declared as angular dependencies - see search_kit_civicrm_angularModules().
$null = NULL;
\CRM_Utils_Hook::singleton()->invoke(['tasks'], $tasks,
$null, $null, $null, $null, $null, 'civicrm_searchKitTasks'
$checkPermissions = $this->checkPermissions;
$userId = $this->checkPermissions ? \CRM_Core_Session::getLoggedInContactID() : NULL;
\CRM_Utils_Hook::singleton()->invoke(['tasks', 'checkPermissions', 'userId'],
$tasks, $checkPermissions, $userId,
$null, $null, $null, 'civicrm_searchKitTasks'
);

usort($tasks[$entity['name']], function($a, $b) {
Expand Down
10 changes: 6 additions & 4 deletions ext/search_kit/search_kit.php
Expand Up @@ -45,11 +45,13 @@ function search_kit_civicrm_managed(&$entities) {
*/
function search_kit_civicrm_angularModules(&$angularModules) {
_search_kit_civix_civicrm_angularModules($angularModules);
// Fetch all search tasks provided by other modules and add them as crmSearchTasks dependencies
$tasks = $dependencies = [];
// Fetch all search tasks provided by extensions and add their Angular modules as crmSearchTasks dependencies
$tasks = [];
$null = NULL;
CRM_Utils_Hook::singleton()->invoke(['tasks'], $tasks,
$null, $null, $null, $null, $null, 'civicrm_searchKitTasks'
$checkPermissions = FALSE;
\CRM_Utils_Hook::singleton()->invoke(['tasks', 'checkPermissions', 'userId'],
$tasks, $checkPermissions, $null,
$null, $null, $null, 'civicrm_searchKitTasks'
);
foreach ($tasks as $entityTasks) {
foreach ($entityTasks as $task) {
Expand Down