Smarty 3 causes crash if exception thrown, e.g. by crmAPI
Overview
I think this is a general problem that could occur if a smarty function throws an exception. The page crashes with:
undefined extension class 'Smarty_Internal_Method_Trigger_Error'
Reproduction steps
I edited EventInfo.tpl to put this in:
{crmAPI var='local_date_time' entity='Event' action='getvalue' return="custom_115" id=$event.id}
Note that I had a custom_115 field defined for all events, but this event did not have a value for that field.
Visit the page that uses that template.
Crash.
Environment information
- CiviCRM: 5.70 - 5.71.1
- PHP: 8.1
- CMS: D7
Comments
I'm not sure, but from memory api3 getvalue has changed; it now gives an error saying:
"error_message": "field custom_115 unset or not existing"
I'm sure before it just used to return nothing.
Or it could be that before Smarty 3, it was happy to treat nothing as '' and now it's more picky.
I think the main problem seems to be that the exception/error handling is broken in Smarty3. Obviously if there's a bug in the template, it's going not to work, but it oughtn't crash the whole page.
The code that throws the error that causes the crash is CRM/Core/Smarty/plugins/function.crmAPI.php L35:
$smarty->trigger_error('{crmAPI} ' . $e->getMessage());