fieldOptions hook fatal error
Using fieldOptions hook in an extension crashes or generates a fatal error.
I've come across 2 issues:
- Using a method defined in a class created in the extension generates fatal error
- Using API crashes the website
Steps to reproduce:
- Create a new module
- Create a class with a static method inside CRM/extension-short-name/
- Call that method inside fieldOptions hook. For eg: CRM_extension-short-name_Class::method()
- Refresh civicrm home page
- Server error log will have an error similar to the following
Class 'CRM_Myextension_MyClass' not found in /var/www/drupal/sites/default/files/civicrm/ext/uk.co.millertech.myextension/myextension.php on line 159, referer: http://chamil.drupal.local/civicrm/dashboard?reset=1
- Next inside the fieldOptions hook, use the API to obtain any random data. For eg:
$result = civicrm_api3('OptionValue', 'get', [
'sequential' => 1,
'option_group_id' => "contribution_status",
]);
- Refresh civicrm home page
CiviCRM 5.15.1
Drupal 7
Issue
For the first issue, it seems extension files are not loaded before invoking fieldOptions hook. Calling _extension-short-name_civix_civicrm_config();
seems to fix that issue.
function myextension_civicrm_fieldOptions($entity, $field, &$options, $params)
{
_myextension_civix_civicrm_config();
CRM_Myextension_MyClass::myMethod();
}
Could not find a workaround for the second issue
Edited by Don Wijesooriya