Skip to content

Add a link to a menu in a SearchDisplay

The challenge

In our extension we want to add a link to a core Contact Summary Tab.

The example

As an example we will look at the EventSession extension. In this extension we provide the possibility to specify sessions for an event, which can take place at the same time but in different locations, for example CiviCRM for Newbies in room 1 at 10:00 and Introduction to SearchKit in room 2 at 10:00 during CiviCamp. It also provides the possibility for participants to register for sessions. For that we need to add 2 links to the standard menu in the Contact Summary Event Tab.

In standard CiviCRM we see the menu below when we click on the menu button on the Contact Summary Event Tab:

Core Menu

We want to add 2 options to the menu, one to register for sessions for the event and one to view the sessions the participant registered for.

The solution

We will add a listener for the hook hook_civicrm_managed and add a static method to perform the validation.

Add the listener

In the extension the listeners for hooks are added in the eventsession.php file using the eventsession_civicrm_config method. So we will add the listener for hook_civicrm_managed to the list that is already there

Hook listeners

There are more ways in which you can add a listener for a hook, see the Symfony method documentation

function eventsession_civicrm_config(&$config): void {
  Civi::dispatcher()->addListener('hook_civicrm_managed', ['Civi\Eventsession\HookHandler', 'onManaged', ]);
  _eventsession_civix_civicrm_config($config);
}

Add the method

We will add the method onManaged as specified in the listener to the HookHandler class. In this method we will test for the correct SearchDisplay and add the links. And obviously we already developed our specific pages that are accessed with the specified URL's.

use Civi\Core\Event\GenericHookEvent;

  public static function onManaged(GenericHookEvent $event): void {
    foreach ($event->entities as $entityId => $entity) {
      if ($entity['entity'] == 'SearchDisplay' && $entity['name'] == 'SavedSearch_Contact_Summary_Events_SearchDisplay_Contact_Summary_Events_Tab') {
        foreach ($entity['params']['values']['settings']['columns'] as $columnId => $column) {
          if ($column['type'] == 'menu') {
            $links = $entity['params']['values']['settings']['columns'][$columnId]['links'];
            $links[] = [
              'path' => 'civicrm/eventsession/page/newregistrations?pid=[id]',
              'text' => E::ts('Register for Event Session(s)'),
              'style' => 'default',
              'target' => 'crm-popup',
              'icon' => 'fa-address-card',
              'task' => "",
              'condition' => [],
              'entity' => "",
              'action' => "",
              'join' => "",
            ];
            $links[] = [
              'path' => 'civicrm/eventsession/page/activeregistrations?pid=[id]',
              'text' => E::ts('Event Session(s) Registered For'),
              'style' => 'default',
              'target' => 'crm-popup',
              'icon' => 'fa-address-card',
              'task' => "",
              'condition' => [],
              'entity' => "",
              'action' => "",
              'join' => "",
            ];
            $event->entities[$entityId]['params']['values']['settings']['columns'][$columnId]['links'] = $links;
          }
        }
      }
    }
  }

The result

And please ignore the Dutch....

Added Links