*/
use Civi\Token\AbstractTokenSubscriber;
+use Civi\Token\Event\TokenValueEvent;
use Civi\Token\TokenRow;
use Civi\ActionSchedule\Event\MailingQueryEvent;
use Civi\Token\TokenProcessor;
public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
$this->prefetch = (array) $prefetch;
$fieldValue = $this->getFieldValue($row, $field);
+ if (is_array($fieldValue)) {
+ // eg. role_id for participant would be an array here.
+ $fieldValue = implode(',', $fieldValue);
+ }
if ($this->isPseudoField($field)) {
if (!empty($fieldValue)) {
* Get all the tokens supported by this processor.
*
* @return array|string[]
+ * @throws \API_Exception
*/
- public function getAllTokens(): array {
+ protected function getAllTokens(): array {
$basicTokens = $this->getBasicTokens();
foreach (array_keys($basicTokens) as $fieldName) {
// The goal is to be able to render more complete tokens
* @return string|int
*/
protected function getFieldValue(TokenRow $row, string $field) {
+ $entityName = $this->getEntityName();
+ if (isset($row->context[$entityName][$field])) {
+ return $row->context[$entityName][$field];
+ }
+
$actionSearchResult = $row->context['actionSearchResult'];
$aliasedField = $this->getEntityAlias() . $field;
if (isset($actionSearchResult->{$aliasedField})) {
* @return string[]
*/
public function getSkippedFields(): array {
- $fields = ['contact_id'];
+ // tags is offered in 'case' & is one of the only fields that is
+ // 'not a real field' offered up by case - seems like an oddity
+ // we should skip at the top level for now.
+ $fields = ['contact_id', 'tags'];
if (!CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
$fields[] = 'campaign_id';
}
return CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($this->getApiEntityName());
}
- public function getEntityIDField() {
+ public function getEntityIDField(): string {
return $this->getEntityName() . 'Id';
}
- public function prefetch(\Civi\Token\Event\TokenValueEvent $e): ?array {
+ public function prefetch(TokenValueEvent $e): ?array {
$entityIDs = $e->getTokenProcessor()->getContextValues($this->getEntityIDField());
if (empty($entityIDs)) {
return [];
return CRM_Core_Config::singleton()->defaultCurrency;
}
- public function getPrefetchFields(\Civi\Token\Event\TokenValueEvent $e): array {
- return array_intersect(array_merge($this->getActiveTokens($e), $this->getCurrencyFieldName()), array_keys($this->getAllTokens()));
+ /**
+ * Get the fields required to prefetch the entity.
+ *
+ * @param \Civi\Token\Event\TokenValueEvent $e
+ *
+ * @return array
+ * @throws \API_Exception
+ */
+ public function getPrefetchFields(TokenValueEvent $e): array {
+ $allTokens = array_keys($this->getAllTokens());
+ $requiredFields = array_intersect($this->getActiveTokens($e), $allTokens);
+ if (empty($requiredFields)) {
+ return [];
+ }
+ $requiredFields = array_merge($requiredFields, array_intersect($allTokens, array_merge(['id'], $this->getCurrencyFieldName())));
+ foreach ($this->getDependencies() as $field => $required) {
+ if (in_array($field, $this->getActiveTokens($e), TRUE)) {
+ foreach ((array) $required as $key) {
+ $requiredFields[] = $key;
+ }
+ }
+ }
+ return $requiredFields;
+ }
+
+ /**
+ * Get fields which need to be returned to render another token.
+ *
+ * @return array
+ */
+ public function getDependencies(): array {
+ return [];
}
}