2

CiviCRM 5.9.1 Drupal 7.61

Trying to create custom tokens using "hook_civicrm_container", TOKEN_REGISTER is getting set as well as TOKEN_EVALUATE function in $container variable but TOKEN_REGISTER function is never getting invoked. Testing through opening up 'Send en Email' action on contact record.

Below is the code in place in a custom module.

function test_module_civicrm_container($container) {

  $container->addResource(new FileResource(FILE));
  $container->findDefinition('dispatcher')->addMethodCall('addListener',
    array(\Civi\Token\Events::TOKEN_REGISTER, '_register_tokens')
  );
  $container->findDefinition('dispatcher')->addMethodCall('addListener',
    array(\Civi\Token\Events::TOKEN_EVALUATE, '_evaluate_tokens')
  );
  //CRM_Core_Error::debug('container', $container);exit;
}

function _register_tokens(\Civi\Token\Event\TokenRegisterEvent $e) {
echo 'came here';exit;
  $e->entity('profile')
    ->register('viewUrl', ts('Profile View URL'))
    ->register('viewLink', ts('Profile View Link'));
}

function _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['contactId']);
    $row->tokens('profile', 'viewLink', ts("<a href='%1'>Open Profile</a>", array(
      1 => $row->tokens['profile']['viewUrl'],
    )));
  }
}

Trying to follow doc here

Your Answer

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

Browse other questions tagged or ask your own question.