Form rendering entityRef field with api params doesn't properly handle 'IN' filter when default values are set
This is a REALLY narrow issue that I experienced on 5.8.alpha1 when developing an extension that uses addEntityRef() fields in a settings form that sets default values, and the fields were filtered via API so that only valid results were options. The field was added as such where $priceFieldIDs is an array of valid IDs [1,3]
(full settings form attached - OtherAmountSettings.php):
$this->addEntityRef('other_amount_price_fields', ts('Other Amount Price Fields'), array( 'entity' => 'PriceField', 'api' => array( 'params' => array( 'id' => ['IN' => $priceFieldIDs], ), ), 'select' => array('minimumInputLength' => 0), 'multiple' => TRUE, ));
Current Behavior
IF there are any default values (i.e. a value was set before), all valid values appear in the field on form load. For example if I save ID 1 out of choices 1,2, & 3... the form reloads and displays IDs 1,2,3 in the form (if I save again with no changes it will set all 3 IDs that it displays).
Desired Behavior
IF there are any default values, only the previously saved values appear in the field on form load. For example if I save ID 1 out of choice 1,2, & 3... the form reloads and displays only ID 1, so if you re-save the form without changes.. no changes will be made.
Replication
Add an entityRef field to a form as above through an extension and using the api params and an 'IN' parameter. You can add this field to a custom settings form, or to a contribution page via hook_civicrm_buildForm (to see the behavior there, submit the form and then click 'go back'). Observe described behavior.
Proposed Solution
PR link: https://github.com/civicrm/civicrm-core/pull/13045
I've tried a solution that seems to work well which you'll see there, but i'm not super confident in it because it's removing some lines that I don't really understand the impetus behind writing... so any discussion on that is very welcome!