3

I am developing an extension for civicrm 4.7. This extension includes several tokens. One of the tokens I want to include is a token for the activity assignee to be used in scheduled reminders.

In Civi 4.7 the hook system is expanded with Symfony Event Dispatchers and it looks like the token evaluate event will give me the oppurtunity to do exactly what is needed.

However I am confused in how to register an event listener. I have implemented the hook_civicrm_container but my events are never called.

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

function tokens_civicrm_container(containerBuilder $container) {
   $container->addResource(new FileResource(__FILE__));
   $container->findDefinition('dispatcher')->addMethodCall('addListener',
     array(\Civi\Token\Events::TOKEN_EVALUATE, 'tokens_evaluate_tokens')
   );
}

function tokens_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
  foreach ($e->getRows() as $row) {
    /** @var TokenRow $row */
    $row->format('text/html');
    $row->tokens('profile', 'viewUrl', 'http://example.com/profile/' . $row->context['contact_id']);
    $row->tokens('profile', 'viewLink', ts("<a href='%1'>Open Profile</a>", array(
      1 => $row->tokens['profile']['viewUrl'],
    )));
  }
}

What is wrong with my code?

5

I have found why it was not working. I was looking in the user interface whether my tokens showed up in the list with tokens a user can use. But they did not. After digging all the core code I found out that I had to use a combination of hook_tokens and an event listener for evaluate tokens.

I also had to put a require once statement to my token listener class in top of my module file otherwise it would fail with loading the file.

I also found a bug report indicating that token handling is redone and indicating that not all steps are done. See https://issues.civicrm.org/jira/browse/CRM-16553

  • "I had to use a combination of hook_tokens and an event listener" - do you mean you had to implement a hook_civicrm_tokens ? and / or a hook_civicrm_tokenValues ? – commonpike Nov 28 '17 at 17:25
  • 1
    No I mean I had to implement an event listener with the Civi service class. And yes I also had to implement hook_civicrm_tokens. Meaning I have duplicate code. – Jaap Jansma - CiviCooP Nov 30 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.