0

I use the Inline Entity Form module to build nested multiple forms. I have a design form in which there is an inline form - garment. And inside the garment form, there is a product field.

In the hook_inline_entity_form_entity_form_alter() I get $field_product_user_input

$field_product_user_input = $form_state->getUserInput()['field_design']['form']['inline_entity_form']['entities']['0']['form']['field_garment']['form']['inline_entity_form']['entities']['0']['form']['field_product']['0']['target_id'];

And I get what I need. But when I add a second inline entity form in the design, it gets the first garment's field value. This occurs because the second form has a key of '1' in the 'entities' array, while the code above uses '0' for the first form.

$form_state->getUserInput() show this result:

result = [
  'field_design' => [
    'form' => [
      'inline_entity_form' => [
        'entities' => [
          '0' => [
            'form' => [
              'field_garment' => [
                'form' => [
                  'inline_entity_form' => [
                    'entities' => [
                      '0' => [...],
                      '1' => [...] // new garment
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
    ],
  ],
];

1 Answer 1

0
  1. Use $form_state->getUserInput()['_triggering_element_name']
  2. Using such PHP functions as: strpos(), str_replace() and explode() build an array of parents from what is in $form_state->getUserInput()['_triggering_element_name']
  3. Use NestedArray::getValue()

Example:

if ($triggering_element_name = $form_state->getUserInput()['_triggering_element_name']) {
  if (strpos($triggering_element_name, '[field_product][0][target_id]') !== FALSE) {
    $triggering_element_name = str_replace(']', '', $triggering_element_name);
    $parents = explode('[', $triggering_element_name);
    if ($field_product = NestedArray::getValue($form_state->getUserInput(), $parents)) {
      $entity_product_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($field_product);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.