CRM-18972: List of strings that cannot be localized cause the absence of ts() function
[civicrm-core.git] / Civi / Token / TokenCompatSubscriber.php
index a0aa9bf689077416d879b36e16b3fdf74d47f521..3eaf57d3b7cc6ac6d92ccf948c9647a45825767b 100644 (file)
@@ -49,9 +49,10 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $messageTokens = $e->getTokenProcessor()->getMessageTokens();
 
     foreach ($e->getRows() as $row) {
+      $contactId = $row->context['contactId'];
       if (empty($row->context['contact'])) {
         $params = array(
-          array('contact_id', '=', $row->context['contactId'], 0, 0),
+          array('contact_id', '=', $contactId, 0, 0),
         );
         list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params);
         $contact = reset($contact); //CRM-4524
@@ -70,16 +71,22 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         $contact = array_merge($contact, $row->context['tmpTokenParams']);
       }
 
+      $contactArray = !is_array($contactId) ? array($contactId => $contact) : $contact;
+
       // Note: This is a small contract change from the past; data should be missing
       // less randomly.
       //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']);
-      \CRM_Utils_Hook::tokenValues($contact,
-        $row->context['contactId'],
+      \CRM_Utils_Hook::tokenValues($contactArray,
+        (array) $contactId,
         empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id,
         $messageTokens,
         $row->context['controller']
       );
 
+      // merge the custom tokens in the $contact array
+      if (!empty($contactArray[$contactId])) {
+        $contact = array_merge($contact, $contactArray[$contactId]);
+      }
       $row->context('contact', $contact);
     }
   }
@@ -93,10 +100,6 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $isHtml = ($e->message['format'] == 'text/html');
     $useSmarty = !empty($e->context['smarty']);
 
-    if (!empty($e->context['contact'])) {
-      \CRM_Utils_Token::replaceGreetingTokens($e->string, NULL, $e->context['contact']['contact_id'], NULL, $useSmarty);
-    }
-
     $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain::getDomain(), $isHtml, $e->message['tokens'], $useSmarty);
 
     if (!empty($e->context['contact'])) {
@@ -104,6 +107,8 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
 
       // FIXME: This may depend on $contact being merged with hook values.
       $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty);
+
+      \CRM_Utils_Token::replaceGreetingTokens($e->string, NULL, $e->context['contact']['contact_id'], NULL, $useSmarty);
     }
 
     if ($useSmarty) {