Is there a way to prepopulate inline entity form widget with a bundle type via URL?
I would like to add custom menu items to create node with each bundle type in this way:
/node/add/request?request_object_bundle=object_bundle_2 or /node/add/request/object_bundle_3

I have a node type "Request" with a custom entity "Request Object", created within Request through inline entity form widget.
The Request Object is split into four bundles with absolutely different fields.
I can use the widget type "Inline entity form - Multiple values" to let the user select a bundle from dropdown on Request creation form.
But I only need to attach one form of one chosen bundle to each Request, so "Inline entity form - Single value" is more appropriate.

For now when I use the "Inline entity form - Single value" widget - I always get the first bundle form preloaded into my Request form.
I tried all the hooks available in inline_entity_form.api.php to change the attached form attributes concerning Bundle, but did not succeed.
Please, help me find an appropriate way to select an inline entity form bundle via an argument in custom menu link.
I'm sure this usecase could be useful to others. Thank you in advance.


Ace Cooper’s picture

Issue summary: View changes

more details

Ace Cooper’s picture

My search brings me to line 449 in inline_entity_form.module:

  // Build the appropriate widget.
  // The "Single value" widget assumes it is operating on a required single
  // value reference field with 1 allowed bundle.
  if ($widget['type'] == 'inline_entity_form_single') {
    // Intentionally not using $settings['create_bundles'] here because this
    // widget doesn't care about permissions because of its use case.
    $bundle = reset($settings['bundles']);

If I change the reset($settings['bundles']) to the name of my custom entity bundle (e.g. 'other') - the IEF widget loads a form from the desired bundle. I have to extend this bit to accept arguments from URL (permission checks and security to be dealt with later).

UPD 1. The dirty hack works with the url /node/add/quote/other:

if (arg(3)) { $bundle = arg(3); }
else { $bundle = reset($settings['bundles']); }

Woohoo, now I shall test it for "coodies", make it configurable and would like to hear your opinions.

UPD 2. The bad thing is that if I use any Ajax buttons on the main node form (e.g. "Add more" for a field collection) - the arg(3) gets lost and field validation on Request submit works for the first bundle only.
I need to find a way to store the bundle argument on inline form creation.

UPD 3. So, the way I found around AJAX buttons screwing with arg(3) is to store the desired entity bundle within the $form_state.
Replaced the same line 449 from $bundle = reset($settings['bundles']);. Still crude, but effective.

if (arg(3)) { $ebndl = arg(3); }  
$array = $field['settings']['handler_settings']['target_bundles'];

if (in_array($ebndl, $array)) {
    $bundle = $ebndl;
    $form_state['inline_entity_form'][$ief_id]['form settings'] = array( 'bundle' => $bundle, );
} else {
    $bundle = $form_state['inline_entity_form'][$ief_id]['form settings']['bundle'];
Ace Cooper’s picture

Issue summary: View changes

even more accurate description

bojanz’s picture

Issue summary: View changes
Status: Active » Fixed

The IEF -dev version has hook_inline_entity_form_settings_alter() that you could use to modify $settings['bundles'].
Or write your own custom widget that calls the IEF controller (basically copying the single widget code, it's short anyway, and adding your custom bundle logic). That's what I would do.

Status: Fixed » Closed (fixed)

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