Afform: civi.afform.prefill never called within custom extension with Case
Overview
The population of two custom fields via civi.afform.prefill
when a FormBuilder is rendered is not working properly. Technically, from my discussion on Mattermost prefill should be called prior to field rendering.
Basically, we are attempting to create a Form that uses CiviCase to be able to allow members to request to change their membership type. The two custom fields that should be modified (on the fly are)
- Current Membership (to store the existing membership)
- Choose a Membership Type (a limited set of memberships that a person can change too)
Reproduction steps
- Create a Custom Data Field set used for Cases
- Create a field Current Membership with a DataType Alphanumeric Radio
- Create a field Choose a Membership Type with a DataType Integer Radio
- Create a FormBuilder linked to a Case
- Set Case Clients to Current User
- Set Case Type to your new Case Type
- Create a custom extension and add the code below
- Enable extension
- No fields are modified
Custom code
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function myext_civicrm_config(&$config) {
_myext_civix_civicrm_config($config);
$dispatcher = Civi::dispatcher();
// Add FormBuilder Prefill
$dispatcher->addListener('civi.afform.prefill', 'myext_afform_prefill', 99);
}
``` php
/**
* Implements AfformPrefillEvent
* @link https://docs.civicrm.org/dev/en/latest/afform/afform-events/
*/
function myext_afform_prefill($event) {
Civi::log()->warning('Event here');
$form = $event->getAfform();
Civi::log()->warning('Form : '.var_export($form,true));
if ($form['name'] == 'abc') {
$entity_values = $event->getEntityValues();
Civi::log()->warning('Values : '.var_export($entity_values,true));
}
}
Afform Markup
<af-form ctrl="afform">
<af-entity data="{contact_id: ['user_contact_id'], case_type_id: '4', status_id: '4', subject: 'Request Form - Change Membership Type'}" actions="{create: true, update: false}" type="Case" name="Case1" label="Case 1" security="FBAC" />
<div class="af-markup">
</div>
<fieldset af-fieldset="Case1" class="af-container">
<af-field name="Request_Membership_Change.Current_Membership" defn="{label: 'Your Current Membership', required: true}" />
<af-field name="Request_Membership_Change.Choose_a_Membership_Type" defn="{label: 'Choose a New Membership Type', required: true}" />
<af-field name="Request_Membership_Change.Documentation" defn="{label: 'Supporting Documentation'}" />
<button class="af-button btn btn-primary" crm-icon="fa-check" ng-click="afform.submit()">Submit Request</button>
</fieldset>
</af-form>
Current behaviour
The civi.afform.prefill
event never seems to be launched. I can confirm that it is being launched on the Validate hook so it should also work for prefill.
Expected behaviour
When loading the form the _prefill
hook Event would be launched and we could modify the custom fields like we can with hook_civicrm_fieldOptions
Environment information
- CiviCRM: 5.60
- CMS: Drupal 7
Comments
I wouldn't mind helping to debug this issue and/or provide a merge request. I would just need further pointers. When I tried to add logs in the actual afform prefill event creation there were no logs output. I could be doing something wrong but I have the impression it's not getting fired.
My gutt says it's because this is only presently setup for Autocomplete fields like Contacts Select list, but I could be totally off.