Complex Widget selects wrong bundle when multiple bundles exist but user only has access to create one.

Created on 20 August 2019, over 5 years ago
Updated 9 December 2023, about 1 year ago

Problem

If the entity type being referenced has multiple bundles, but the user interacting with ief only has access to create entities of one of those bundles, ief will create an entity of the first bundle that exists NOT of the bundle the user is allowed to create.

Detail

  1. When there is only one "$create_bundle" the bundle element gets created as a value element rather than a select list.
    if ($create_bundles_count > 1) {
             ...
            }
            else {
              $element['actions']['bundle'] = [
                '#type' => 'value',
                '#value' => reset($create_bundles),
              ];
            }
    
  2. inline_entity_form_open_form() looks for the bundle in $form_state->getUserInput();
  3. Values from value elements are not included in $form_state->getUserInput() so the correct bundle is not selected.

Solution

This can be resolved by adding an else if into inline_entity_form_open_form that checks for the value element. Like so:

  /**
 * Button #submit callback: Opens a form in the IEF widget.
 *
 * The form is shown below the entity table, at the bottom of the widget.
 *
 * @param $form
 *   The complete parent form.
 * @param $form_state
 *   The form state of the parent form.
 */
function inline_entity_form_open_form($form, FormStateInterface $form_state) {
  $element = inline_entity_form_get_element($form, $form_state);
  $ief_id = $element['#ief_id'];
  $form_state->setRebuild();

  // Get the current form values.
  $parents = array_merge($element['#field_parents'], [$element['#field_name']]);
  $form_values = NestedArray::getValue($form_state->getUserInput(), $parents);

  $triggering_element = $form_state->getTriggeringElement();
  $form_state->set(['inline_entity_form', $ief_id, 'form'], $triggering_element['#ief_form']);
  if (!empty($form_values['actions']['bundle'])) {
    $form_state->set(['inline_entity_form', $ief_id, 'form settings'], [
      'bundle' => $form_values['actions']['bundle'],
    ]);
  }
  else if ($element['actions']['bundle']['#type'] == 'value') {
    $form_state->set(['inline_entity_form', $ief_id, 'form settings'], [
      'bundle' => $element['actions']['bundle']['#value'],
    ]);
  }
}

Patch on the way

🐛 Bug report
Status

Postponed: needs info

Version

3.0

Component

Code

Created by

🇬🇧United Kingdom rlmumford Manchester

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024