I needed to be able to add a new activity record to every contact in my search results.
Here's how I used a CiviCRM hook to add a function to the list of tasks you can perform on a set of search results.
Step 1: implement hook_civicrm_searchTasks()
I use Drupal, and civicrm hooks are done just like any other drupal hook. It's different if you're on Joomla/Wordpress.
So just create a drupal module (e.g. called mymod) and within the .module file create a function called
You can verify that this is working by doing a search and looking in the select box for your new task. (Not there? did you enable your drupal module? did you
drush cc all?)
Step 2: create a class and a template for this task
So now you need to create the file
<your customisation path>/CRM/Contact/Form/Task/MyTask.php and within it, define the CRM_Contact_Form_Task_MyTask class (note the directory structure maps the class name).
Your class must extend
CRM_Contact_Form_Task, which means it's also inheriting from
HTML_QuickForm_Page. It's all these layers of inheritance that prompted me to write this post, because documentation is split between projects (CiviCRM and Pear's HTML_QuickForm).
The template file must be in
<your templates path>/CRM/Contact/Form/Task/MyTask.tpl
Step 3: Ask user for extra information required for processing
So we wanted to record which newsletter was to be sent. We'll ask the user with a text field.
To add fields to the as-yet blank form, we override the
This defines elements within the form, but to get these to display to the user we need to edit the template file:
Step 4: Complete the task
Once we have this information we can do the updates in the
postProcess() method of our class.
You have access to the contact ids in
$this->_contactIds and access to the submitted form data in the associative array returned by
Something like this: