Answers.Body: <p>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. </p>
  
  <p>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. </p>
  
  <p>I also found a bug report indicating that token handling is redone and indicating that not all steps are done. See <a href="https://issues.civicrm.org/jira/browse/CRM-16553" rel="noreferrer">https://issues.civicrm.org/jira/browse/CRM-16553</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:25:37.510Z
Answers.Text: "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` ?
Answers.UserDisplayName: commonpike
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-30T09:44:20.437Z
Answers.Text: 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.
Answers.UserDisplayName: Jaap Jansma - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-18T13:42:55.103Z
Answers.LastActivityDate: 2016-03-18T13:59:55.793Z
Answers.LastEditDate: 2016-03-18T13:59:55.793Z
Answers.UserDisplayName: Jaap Jansma - CiviCooP
Body: <p>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. </p>
  
  <p>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. </p>
  
  <p>However I am confused in how to register an event listener. I have implemented the hook_civicrm_container but my events are never called.</p>
  
  <pre><code>use Symfony\Component\Config\Resource\FileResource;
  use Symfony\Component\DependencyInjection\ContainerBuilder;
  use Symfony\Component\DependencyInjection\Definition;
  
  function tokens_civicrm_container(containerBuilder $container) {
     $container-&gt;addResource(new FileResource(__FILE__));
     $container-&gt;findDefinition('dispatcher')-&gt;addMethodCall('addListener',
       array(\Civi\Token\Events::TOKEN_EVALUATE, 'tokens_evaluate_tokens')
     );
  }
  
  function tokens_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
    foreach ($e-&gt;getRows() as $row) {
      /** @var TokenRow $row */
      $row-&gt;format('text/html');
      $row-&gt;tokens('profile', 'viewUrl', 'http://example.com/profile/' . $row-&gt;context['contact_id']);
      $row-&gt;tokens('profile', 'viewLink', ts("&lt;a href='%1'&gt;Open Profile&lt;/a&gt;", array(
        1 =&gt; $row-&gt;tokens['profile']['viewUrl'],
      )));
    }
  }
  </code></pre>
  
  <p>What is wrong with my code? </p>
  
Tags: <extension><developer>
Title: Using event listeners in my extensions
ThreadText: # Using event listeners in my extensions
  
  (Asked by: username_0 on 2016-03-18)
  
  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?
  
  **Tags:** extension, developer
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-03-18. Score: 5 | Accepted answer
  
  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>
  
  > **Comments:**
  > **username_1**: "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` ?  
  > **username_0**: 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.  
  
  ---
  
  