[REF] Code readability changes on activity tokens.
authoreileen <emcnaughton@wikimedia.org>
Fri, 24 Apr 2020 04:33:26 +0000 (16:33 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 24 Apr 2020 05:00:17 +0000 (17:00 +1200)
I've been trying to get to grips with this code and this addresses 2 things I found very confusing - notably

- using activityId when we were referring to a fieldname - we use a combination of  &  for
variables but normally when it's closely related to a query we use activity_id
- the function name getEntityContextSchema() was unclear to me - I concluded that in fact we were retrieving
the nname of the field for the entity ID

CRM/Activity/Form/Task/PDFLetterCommon.php
CRM/Activity/Tokens.php
CRM/Core/TokenTrait.php
Civi/Token/TokenProcessor.php

index 424249fb5c8a2e28ce12d651fee9fb5ad8e25a26..7af6c006cfe0dbc9a0d956ca5fa62783a0224b6e 100644 (file)
@@ -53,7 +53,7 @@ class CRM_Activity_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLette
     $tp->addMessage('body_html', $html_message, 'text/html');
 
     foreach ($activityIds as $activityId) {
-      $tp->addRow()->context('activityId', $activityId);
+      $tp->addRow()->context('activity_id', $activityId);
     }
     $tp->evaluate();
 
@@ -69,7 +69,7 @@ class CRM_Activity_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLette
     return new TokenProcessor(\Civi::dispatcher(), [
       'controller' => get_class(),
       'smarty' => FALSE,
-      'schema' => ['activityId'],
+      'schema' => ['activity_id'],
     ]);
   }
 
index fff8e6224a507fe684cf1465cbf0881d7365ca91..e8d875c5e7ad852fc16f5d0b1fe9d3d9a2af19a3 100644 (file)
@@ -48,10 +48,12 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
   }
 
   /**
+   * Get the name of the field which holds the ID of the given entity.
+   *
    * @return string
    */
-  private function getEntityContextSchema(): string {
-    return 'activityId';
+  private function getEntityIDFieldName(): string {
+    return 'activity_id';
   }
 
   /**
@@ -88,7 +90,7 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
     // Find all the entity IDs
     $entityIds
       = $e->getTokenProcessor()->getContextValues('actionSearchResult', 'entityID')
-      + $e->getTokenProcessor()->getContextValues($this->getEntityContextSchema());
+      + $e->getTokenProcessor()->getContextValues($this->getEntityIDFieldName());
 
     if (!$entityIds) {
       return NULL;
@@ -122,6 +124,8 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
 
   /**
    * @inheritDoc
+   *
+   * @throws \CRM_Core_Exception
    */
   public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
     // maps token name to api field
@@ -130,7 +134,7 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
     ];
 
     // Get ActivityID either from actionSearchResult (for scheduled reminders) if exists
-    $activityId = $row->context['actionSearchResult']->entityID ?? $row->context[$this->getEntityContextSchema()];
+    $activityId = $row->context['actionSearchResult']->entityID ?? $row->context[$this->getEntityIDFieldName()];
 
     $activity = (object) $prefetch['activity'][$activityId];
 
index 26110155eff60f3b96140e65f0cd8087da1df0b5..bd77c7dc693ac1d6b307ae97008d820b4127a141 100644 (file)
@@ -19,7 +19,7 @@ trait CRM_Core_TokenTrait {
    * @inheritDoc
    */
   public function checkActive(\Civi\Token\TokenProcessor $processor) {
-    return in_array($this->getEntityContextSchema(), $processor->context['schema']) ||
+    return in_array($this->getEntityIDFieldName(), $processor->context['schema'], TRUE) ||
       (!empty($processor->context['actionMapping'])
         && $processor->context['actionMapping']->getEntity() === $this->getEntityTableName());
   }
@@ -51,6 +51,7 @@ trait CRM_Core_TokenTrait {
 
   /**
    * Find the fields that we need to get to construct the tokens requested.
+   *
    * @param  array $tokens list of tokens
    * @return array         list of fields needed to generate those tokens
    */
index 1364ed714faa336dcef5fca0638b8d9ec2af2aaa..4fa03b9038fb084e84b825834f17994ca30b84e1 100644 (file)
@@ -27,7 +27,7 @@ class TokenProcessor {
    *   - schema: array, a list of fields that will be provided for each row.
    *     This is automatically populated with any general context
    *     keys, but you may need to add extra keys for token-row data.
-   *     ex: ['contactId', 'activityId'].
+   *     ex: ['contactId', 'activity_id']. (Note we are standardising on the latter).
    */
   public $context;