VaLLe.
VaLLe.

Reputation: 160

Problems with replacing CiviCRM tokens in API

I'm currently developing a CiviCRM extension, where I need to replace CiviCRM-Tokens (used in pdf and mailing generation) in html code.

I did a little bit of research in the core files and tried to recreate the behaviour in the PDFLetterCommon.php (/civicrm/CRM/Contact/Form/Task/PDFLetterCommon.php) where it replaces the tokens in the postProcess function.

Here is the original CiviCRM Code:

list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);

$skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
$skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;

foreach ($form->_contactIds as $item => $contactId) {
  $params = array('contact_id' => $contactId);

  list($contact) = CRM_Utils_Token::getTokenDetails($params,
    $returnProperties,
    $skipOnHold,
    $skipDeceased,
    NULL,
    $messageToken,
    'CRM_Contact_Form_Task_PDFLetterCommon'
  );

  ...

 }

And here is my version for testing:

(this code is located inside an api function in my extension)

$messageToken = CRM_Utils_Token::getTokens($params["html"]);

$returnProperties = array();
if (isset($messageToken['contact'])) {
  foreach ($messageToken['contact'] as $key => $value) {
    $returnProperties[$value] = 1;
  }
}

$skipOnHold = FALSE;
$skipDeceased = TRUE;
$tokenParams = array("contact_id" => 67450);

list($contact) = CRM_Utils_Token::getTokenDetails($tokenParams,
    $returnProperties,
    $skipOnHold,
    $skipDeceased,
    NULL,
    $messageToken,
    'CRM_Contact_Form_Task_PDFLetterCommon'
);

I'm using the default values for $skipOnHold (false) and $skipDeceased (true) and also just passing one (existing) user id into the $params array ($tokenParams in my code).

Here is my problem:

My $messageToken and $returnProperties variables are being filled correctly via CiviCRM's core functions but when I pass them all into CRM_Utils_Token::getTokenDetails() the returned $contact variable holds an empty array.

I'm really out of ideas, I've been looking into CRM/Utils/Token.php where getTokenDetails() is located, but have been unsuccessful in finding the problem with my code.

Thanks in advance for any help!

Upvotes: 0

Views: 134

Answers (0)

Related Questions