I am using this module along with commerce_bpc (bulk product creation).
In the settings of bpc I have it set to 'Provide a "Save and create display" link on the Bulk creation form that takes the user to a pre-populated node creation form.'
The problem is/was when the user is directed to the form to create the product display for the products that were just created in bulk, the products are not listed in the inline entity form.

I realise that this module may provide bulk creation options in the future, but wanted mine to work now and thought I should share my solution/s.

first way is easy but you have to edit the module code.
This goes in the inline_entity_form_field_widget_form function after the $entity_ids are set

if(!count($entity_ids) && isset($_GET['bulk_creation_id']) && isset($_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']])){
        $entity_ids = $_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']];
      }

Or probably the better way is to add a module with the code below:

function MYMODULE_field_widget_inline_entity_form_form_alter(&$element, &$form_state, $context) {
  if(!count($context['items']) && isset($_GET['bulk_creation_id']) && isset($_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']])){
    // reset the element by removing array keys except those in below array
    $dont_remove_these = array('#entity', '#entity_type', '#bundle', '#field_name', '#language', '#field_parents', '#columns', '#title', '#description', '#required', '#delta');
    foreach($element as $key => $value){
      if(!in_array($key, $dont_remove_these)){
        unset($element[$key]);
      }
    }
    $entity_ids = array();
    unset($form_state['inline_entity_form']);
    unset($form_state['field']['#parents']);
    $settings = inline_entity_form_settings($context['field'], $context['instance']);
    
    $items = array();
    foreach($_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']] as $id){
      $items[] = array($settings['column'] => $id);
    }
    $element = inline_entity_form_field_widget_form($context['form'], $form_state, $context['field'], $context['instance'], $context['langcode'], $items, $context['delta'], $element);
  }
}

maybe neither of these is the best way to go about it, but it worked for me

Comments

bojanz’s picture

Title: commerce bulk product creation » Doesn't populate Inline Entity Form
Project: Inline Entity Form » Commerce Bulk Product Creation
Category: feature » bug

I think Commerce BPC could be smarter about the way it prepopulates the product reference field.

Right now bpc_display has hardcoded logic for each widget in bpc_display_form_node_form_alter().
A better way might be to update the product_reference instances on install,
declare a new function for $instance['default_value_function'] that takes into account the $_GET / $_SESSION values.
That way field_get_default_value would always pick up the BPC-added products and it would work with every widget.

pjcdawkins’s picture

Component: Miscellaneous » Code

Thanks for the coda, 2pha, in the OP. It was really helpful, but I found that commerce_bpc_get_node_types() (in commerce_bpc.module) will also need some work for selecting node types with product reference fields, when those fields' types are 'inline entity form' rather than 'commerce product reference'.

But yes the approach described in #1 would make more sense.

drewbolles’s picture

This integration would be awesome. The inline edit form & commerce backoffice seem to be the way to go as far as solving the UI issue for the end user. Adding mass variations inline is a bit slow, though. I'd love for them to be able to go form bpc -> inline edit form. That would be swell.

TajinderSingh’s picture

@2pha
Thanks for sharing the code.
It helped a lot to save the day :).

Thanks again.

discipolo’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
4.11 KB

heres my initial stab at implementing the suggestions from #4

discipolo’s picture

had removed too much. this patch still includes the title and the update function is moved to the install file

ThisIsDog’s picture

I've been working on my own patch for this module and think I it solved this issue as well. I'm in the process of cleaning it up and hope to have it posted later tonight. Perhaps we can compare our patches and collaborate on updating the module.

ThisIsDog’s picture

I just uploaded my patch and referenced this issue (Since I did more than just patch this one issue). My patch is issue 2282659

discipolo’s picture

rromore’s picture

Here's my attempt at a patch that utilizes the hook_field_widget_form_alter() function.

das-peter’s picture

I've come up with this pretty straight forward approach.
We should be able to use hook_node_prepare() which is called when the node form is prepared for a new node.
It allows us to inject the field data and every field widget that properly deals with passed in field $items should actually be compatible.

GoddamnNoise’s picture

I've tried the patch in #11 and it worked for me!. Great job!.