Problem/Motivation
We have a multi value Dynamic Entity Reference field that can reference both nodes and group entities. However, we want the second value to always be a group entity reference. However by default the type is set to "content", ie nodes.
We've been able to successfully change the type of the second value to be a group via a form alter (see code below). However, we lose any filtering of the values as set in the field settings (limiting groups using a views entity reference view -- see screenshot). We also tried simply changing the "type" select with Javascript if that would trigger the necessary changes but with no success.
FYI Manually changing to content and back to group manually makes the views entity reference filter work as expected.
Just wondering what I'm missing to make this work?
<?php
function custom_module_form_node_game_form_alter(&$form, $form_state, $form_id) {
$form['field_teams']['widget'][1]['target_type']['#default_value'] = 'group';
$form['field_teams']['widget'][1]['target_id']['#target_type'] = 'group';
$form['field_teams']['widget'][1]['target_id']['#selection_handler'] = 'default:group';
unset($form['field_teams']['widget'][1]['target_id']['#selection_settings']);
}
?>
Apologies if this has been answered before. I did search the issue queue with no success. Tried on version 1.x and 2.x. Can't test 3.x or higher since my site isn't quite PHP8 ready.
| Comment | File | Size | Author |
|---|
Comments
Comment #2
rhache commentedComment #3
rhache commentedComment #4
jibranCan you please upload the views YAML if possible? Can I have the autocomplete URLs for the DER fields when you change them using form alter? Maybe have a look at the network tab and see which endpoint it is hitting before and after changing it manually.
Comment #5
rhache commentedHere's the autocomplete input field when type changed via form alter:
And when type is change manually:
I can see how one points to
/entity_reference_autocomplete/group/default...(incorrect)/entity_reference_autocomplete/group/views/...(correct).This is the views entity reference file: