TypeError when trying to replace tokens for custom fields that don't exist
Overview
A TypeError: Return value of CRM_Core_EntityTokens::getCustomFieldName() must be of the type string, none returned
is being thrown when trying to replace custom_123
tokens for custom fields that don't exist.
Reproduction steps
- For a contact, choose the action Print/Merge Document
- In the message body field, enter a token for a custom field, that does not exist, e.g.
{contact.custom_99999}
- Hit Preview or Download Document
This is just an example, the error occurs everywhere a non-existent custom field token is being fed into the token system.
Current behaviour
PHP fatals with a 500 with an error message TypeError: Return value of CRM_Core_EntityTokens::getCustomFieldName() must be of the type string, none returned in CRM_Core_EntityTokens->getCustomFieldName() (Line 511 of /path/to/civicrm/core/CRM/Core/EntityTokens.php).
Expected behaviour
Invalid tokens should just be ignored or replaced with an empty string (not sure what the previous behavior was).
Comments
\CRM_Core_EntityTokens::getCustomFieldName()
should not be type hinted to string
for its return value when it can't keep that promise.
If the token systems really requires validating token names before calling this code path, this should be clearly documented and done within Core.