Merge pull request #16624 from mattwire/tokencompatsubscriber
[civicrm-core.git] / Civi / Token / TokenCompatSubscriber.php
index 8e2ac735ba1b176cb5216da295183ec7733282b9..d9b186e1f32131ea9fafe1cac8711bde94b71c3e 100644 (file)
@@ -24,16 +24,16 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
    * @inheritDoc
    */
   public static function getSubscribedEvents() {
-    return array(
+    return [
       Events::TOKEN_EVALUATE => 'onEvaluate',
       Events::TOKEN_RENDER => 'onRender',
-    );
+    ];
   }
 
   /**
    * Load token data.
    *
-   * @param TokenValueEvent $e
+   * @param \Civi\Token\Event\TokenValueEvent $e
    * @throws TokenException
    */
   public function onEvaluate(TokenValueEvent $e) {
@@ -41,7 +41,7 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     // hook *categories* (aka entities aka namespaces). We'll cache
     // this in the TokenProcessor's context.
 
-    $hookTokens = array();
+    $hookTokens = [];
     \CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     $e->getTokenProcessor()->context['hookTokenCategories'] = $categories;
@@ -49,14 +49,18 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $messageTokens = $e->getTokenProcessor()->getMessageTokens();
 
     foreach ($e->getRows() as $row) {
+      if (empty($row->context['contactId'])) {
+        continue;
+      }
       /** @var int $contactId */
       $contactId = $row->context['contactId'];
       if (empty($row->context['contact'])) {
-        $params = array(
-          array('contact_id', '=', $contactId, 0, 0),
-        );
+        $params = [
+          ['contact_id', '=', $contactId, 0, 0],
+        ];
         list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params);
-        $contact = reset($contact); //CRM-4524
+        //CRM-4524
+        $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
           // FIXME: Need to differentiate errors which kill the batch vs the individual row.
           throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']);
@@ -66,10 +70,10 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         if (!empty($messageTokens['contact'])) {
           foreach ($messageTokens['contact'] as $token) {
             if (\CRM_Core_BAO_CustomField::getKeyID($token)) {
-              $contact[$token] = civicrm_api3('Contact', 'getvalue', array(
+              $contact[$token] = civicrm_api3('Contact', 'getvalue', [
                 'return' => $token,
                 'id' => $contactId,
-              ));
+              ]);
             }
           }
         }
@@ -84,7 +88,7 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         $contact = array_merge($contact, $row->context['tmpTokenParams']);
       }
 
-      $contactArray = !is_array($contactId) ? array($contactId => $contact) : $contact;
+      $contactArray = !is_array($contactId) ? [$contactId => $contact] : $contact;
 
       // Note: This is a small contract change from the past; data should be missing
       // less randomly.
@@ -106,7 +110,7 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
   /**
    * Apply the various CRM_Utils_Token helpers.
    *
-   * @param TokenRenderEvent $e
+   * @param \Civi\Token\Event\TokenRenderEvent $e
    */
   public function onRender(TokenRenderEvent $e) {
     $isHtml = ($e->message['format'] == 'text/html');
@@ -116,12 +120,11 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, $domain, $isHtml, $e->message['tokens'], $useSmarty);
 
     if (!empty($e->context['contact'])) {
+      \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty);
       $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], TRUE, $useSmarty);
 
       // 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, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty);
     }
 
     if ($useSmarty) {