When using "inline entity form - multiple values", a field with cardinality "unlimited" and no existing values displays only the "add" button.

I'd like a blank row to appear by default (e.g. pretend that the "add" button was already clicked upon form load).

What's the best way to make this happen?

Comments

AaronBauman’s picture

Issue summary: View changes
bojanz’s picture

Status: Active » Fixed

Set the field as required, it will open the add form for you.

Otherwise you'll need to implement the widget form alter, and fiddle with IEF's form state.

AaronBauman’s picture

Thanks for the hint, which put me down the right path (I hope).
I fixed this as follows; I hope it helps any other poor souls from having to toil on this issue:


// Do NOT set the field to required through Fields UI
function example_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form') {
    // Set the field to required here, temporarily, to trick IEF into displaying a single blank form:
    $form['field_example_ief_field']['#required'] = TRUE;
}

// hook_widget_form_alter fires after IEF has built out our field.
function example_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['field_name'] == 'field_example_ief_field') {
    // At this point, we are free to unset the #required flag
    $element['#required'] = FALSE;
    return;
  }
}

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.