The situation
I have a node form with an inline entity form, allowing users to add 1...n instances of entity X.
In the form of entity X, I have two selects A and B.
The challenge
What I want to achieve, is to limit the options of select B , based on the selected value of select A.
What I tried
Using hook_inline_entity_form_entity_form_alter I added an #ajax change event to select A. This triggers a callback
function mymodule_ajax_callback(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
In this function the fun ends.
The problem
The (recurring) problem with IEF is that $form and $form_state always contain the complete form (in this case: the node form) data and not the inline entity form data, so data of the current entity X form.
Next, there seems to be no foolproof way of obtaining the values of the current entity X form
In my mymodule_ajax_callback function I now have something like
$myvar = $form_state->getUserInput()['field_a']['form']
but the value of $myvar seems quite random:
- it sometimes contains a key 'inline_entity_form' with nested values
- it sometimes contains numeric keys with nested values
Somewhere hidden in these nested value is the currently selected value of A, but I do not know how to know which one to take, as in theory, the end user could have multiple IEF open.
My question
How to obtain the value of select A of entity X the user is currently editing and set the options of select B of the entity X the user is is currently editing?
Comments
Comment #2
jvdkolk commentedComment #3
jvdkolk commentedComment #4
jvdkolk commentedComment #5
jvdkolk commentedGot it to work:
Comment #6
jvdkolk commented