From: Eileen McNaughton Date: Thu, 7 Oct 2021 04:38:32 +0000 (+1300) Subject: Remove now-obsolete getPseudoTokens X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7a39d0457266c84f159d2323ff24b6d5add2b7a9;p=civicrm-core.git Remove now-obsolete getPseudoTokens This is now only called from legacy code. The buildContributionArray path is still a little used :-( but is tested from CRM_Contribute_Form_Task_PDFLetterCommonTest and hard-coding the array now it is settled is appropriate to that code path's life expectancy This code is no longer used in the Tokens classes and the functions that used it from outside of there are now 'settled into their final state & waiting to die' so a hard-coded list will see out their lives. They are marked now as deprecated --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index f0f84f988d..0a9a285a38 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4904,9 +4904,15 @@ LIMIT 1;"; } /** + * Do not use - still called from CRM_Contribute_Form_Task_PDFLetter + * + * This needs to be refactored out of use & deprecated out of existence. + * * Get the contribution fields for $id and display labels where * appropriate (if the token is present). * + * @deprecated + * * @param int $id * @param array $messageToken * @@ -4925,11 +4931,34 @@ LIMIT 1;"; $result['values'][$id][$fieldName] = CRM_Core_BAO_CustomField::displayValue($result['values'][$id][$fieldName], $fieldName); } } - $processor = new CRM_Contribute_Tokens(); - $pseudoFields = array_keys($processor->getPseudoTokens()); + + $pseudoFields = [ + 'financial_type_id:label', + 'financial_type_id:name', + 'contribution_page_id:label', + 'contribution_page_id:name', + 'payment_instrument_id:label', + 'payment_instrument_id:name', + 'is_test:label', + 'is_pay_later:label', + 'contribution_status_id:label', + 'contribution_status_id:name', + 'is_template:label', + 'campaign_id:label', + 'campaign_id:name', + ]; foreach ($pseudoFields as $pseudoField) { $split = explode(':', $pseudoField); - $result['values'][$id][$pseudoField] = $processor->getPseudoValue($split[0], $split[1], $result['values'][$id][$split[0]] ?? ''); + $pseudoKey = $split[1]; + $realField = $split[0]; + $fieldValue = $result['values'][$id][$realField] ?? ''; + if ($pseudoKey === 'name') { + $fieldValue = (string) CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', $realField, $fieldValue); + } + if ($pseudoKey === 'label') { + $fieldValue = (string) CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', $realField, $fieldValue); + } + $result['values'][$id][$pseudoField] = $fieldValue; } } return $result; diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 8a4a0c22f6..45da688677 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -312,28 +312,6 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return $this->fieldMetadata; } - /** - * Get pseudoTokens - it tokens that reflect the name or label of a pseudoconstant. - * - * @internal - this function will likely be made protected soon. - * - * @return array - */ - public function getPseudoTokens(): array { - $return = []; - foreach (array_keys($this->getBasicTokens()) as $fieldName) { - if ($this->isAddPseudoTokens($fieldName)) { - $fieldLabel = $this->fieldMetadata[$fieldName]['input_attrs']['label'] ?? $this->fieldMetadata[$fieldName]['label']; - $return[$fieldName . ':label'] = $fieldLabel; - $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $fieldLabel; - } - if ($this->isBooleanField($fieldName)) { - $return[$fieldName . ':label'] = $this->getFieldMetadata()[$fieldName]['title']; - } - } - return $return; - } - /** * Get any tokens with custom calculation. */ @@ -341,35 +319,6 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return []; } - /** - * Is this a field we should add pseudo-tokens to? - * - * Pseudo-tokens allow access to name and label fields - e.g - * - * {contribution.contribution_status_id:name} might resolve to 'Completed' - * - * @param string $fieldName - */ - public function isAddPseudoTokens($fieldName): bool { - if (in_array($fieldName, $this->getCurrencyFieldName())) { - // 'currency' is manually added to the skip list as an anomaly. - // name & label aren't that suitable for 'currency' (symbol, which - // possibly maps to 'abbr' would be) and we can't gather that - // from the metadata as yet. - return FALSE; - } - if ($this->getFieldMetadata()[$fieldName]['type'] === 'Custom') { - // If we remove this early return then we get that extra nuanced goodness - // and support for the more portable v4 style field names - // on custom fields - where labels or names can be returned. - // At present the gap is that the metadata for the label is not accessed - // and tests failed on the enotice and we don't have a clear plan about - // v4 style custom tokens - but medium term this IF will probably go. - return FALSE; - } - return (bool) ($this->getFieldMetadata()[$fieldName]['options'] || !empty($this->getFieldMetadata()[$fieldName]['suffixes'])); - } - /** * Get the value for the relevant pseudo field. * @@ -382,7 +331,7 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { * * @internal function will likely be protected soon. */ - public function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string { + protected function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string { if ($pseudoKey === 'name') { $fieldValue = (string) CRM_Core_PseudoConstant::getName($this->getBAOName(), $realField, $fieldValue); } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 6e05a0d593..6a8ff52d44 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1523,15 +1523,32 @@ class CRM_Utils_Token { return $value; } + /** + * @deprecated + * + * Do not use this function - it still needs full removal from active code + * in CRM_Contribute_Form_Task_PDFLetter. + */ protected static function _buildContributionTokens() { $key = 'contribution'; if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$key])) { - $processor = new CRM_Contribute_Tokens(); $tokens = array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'), ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []], self::getCustomFieldTokens('Contribution'), - $processor->getPseudoTokens() + [ + 'financial_type_id:label', + 'financial_type_id:name', + 'contribution_page_id:label', + 'contribution_page_id:name', + 'payment_instrument_id:label', + 'payment_instrument_id:name', + 'is_test:label', + 'is_pay_later:label', + 'contribution_status_id:label', + 'contribution_status_id:name', + 'is_template:label', + ] ); foreach ($tokens as $token) { if (!empty($token['name'])) {