Creating custom contact tabs and filling them from Drupal

Veröffentlicht
2010-03-04 12:58
Written by
illmasterc - member of the CiviCRM community - view blog guidelines
To make custom tabs when viewing a contact, begin by using the tabs_hook "hook_civicrm_tabs".
http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmtabs
A simple example given on the hooks documentation link above

function civitest_civicrm_tabs( &$tabs, $contactID ) { // unset the contribition tab, i.e. remove it from the page unset( $tabs[1] ); // let's add a new "contribution" tab with a different name and put it last // this is just a demo, in the real world, you would create a url which would // return an html snippet etc. $url = CRM_Utils_System::url( 'civicrm/contact/view/contribution', "reset=1&snippet=1&force=1&cid=$contactID" ); $tabs[] = array( 'id' => 'mySupercoolTab', 'url' => $url, 'title' => 'Contribution Tab Renamed', 'weight' => 300 ); }

Replace the 'civicrm/contact/view/contribution' line with a Drupal path that you have defined in the menu array of your custom module. At this point, if you execute this code, you will have an entire HTML webpage stuffed inside the content area of your new custom tab including whatever that function you wrote returns. To ONLY return the HTML your Drupal function defines, you can create a custom template for the URL you defined in your menu array.
If you are unfamiliar with how Drupal's template suggestions work check out this link http://drupal.org/node/223440
Once you have figured out the correct naming scheme for the new template you have created to be picked up by whatever URL you defined in your custom module's menu array, simply make the template a one-liner , and it will return only what you have defined in your function.

Comments

You don't want to unset tabs[1] unless you want to drop the first tab after the summary tab (which can vary depending on what the user has rights to see). The sample code is meant as a demo and to be honest is not tested.

If you want to overwrite a tab we found this to be more reliable. Basically iterate through the tabs and if one matches the tag you want to overwrite go ahead, otherwise keep going. In this example we are changing Contributions to Dues/Contributions

foreach($tabs as $key => $value) {
if ($tabs[$key]['id'] == 'contribute') {
$count = $tabs[$key]['count'];
$url = CRM_Utils_System::url( 'civicrm/contact/view/contribution',
"reset=1&snippet=1&force=1&cid=$contactID" );
$tabs[$key] = array( 'id' => 'contribute',
'url' => $url,
'title' => ts('Dues/Contributions'),
'count' => $count);
}
}