Merge pull request #9661 from monishdeb/CRM-18141
[civicrm-core.git] / Civi / Token / TokenRow.php
index a18a81c43533f2cead6445beee58411c44a65ee8..4f186d3d5c1ea9ccf3d84806cc76e7b8d834554c 100644 (file)
@@ -126,6 +126,29 @@ class TokenRow {
     return $this;
   }
 
+  /**
+   * Update the value of a custom field token.
+   *
+   * @param string $entity
+   * @param int $customFieldID
+   * @param int $entityID
+   * @return TokenRow
+   */
+  public function customToken($entity, $customFieldID, $entityID) {
+    $customFieldName = "custom_" . $customFieldID;
+    $fieldValue = civicrm_api3($entity, 'getvalue', array(
+      'return' => $customFieldName,
+      'id' => $entityID,
+    ));
+
+    // format the raw custom field value into proper display value
+    if ($fieldValue) {
+      $fieldValue = \CRM_Core_BAO_CustomField::displayValue($fieldValue, $customFieldID);
+    }
+
+    return $this->tokens($entity, $customFieldName, $fieldValue);
+  }
+
   /**
    * Update the value of a token. Apply formatting based on DB schema.
    *
@@ -134,6 +157,8 @@ class TokenRow {
    * @param string $baoName
    * @param array $baoField
    * @param mixed $fieldValue
+   * @return TokenRow
+   * @throws \CRM_Core_Exception
    */
   public function dbToken($tokenEntity, $tokenField, $baoName, $baoField, $fieldValue) {
     if ($fieldValue === NULL || $fieldValue === '') {