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
Comment #1
bojanz CreditAttribution: bojanz commentedI 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.
Comment #2
pjcdawkins CreditAttribution: pjcdawkins commentedThanks 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.
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedThis 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.
Comment #4
TajinderSingh CreditAttribution: TajinderSingh commented@2pha
Thanks for sharing the code.
It helped a lot to save the day :).
Thanks again.
Comment #5
discipolo CreditAttribution: discipolo commentedheres my initial stab at implementing the suggestions from #4
Comment #6
discipolo CreditAttribution: discipolo commentedhad removed too much. this patch still includes the title and the update function is moved to the install file
Comment #7
ThisIsDog CreditAttribution: ThisIsDog commentedI'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.
Comment #8
ThisIsDog CreditAttribution: ThisIsDog commentedI just uploaded my patch and referenced this issue (Since I did more than just patch this one issue). My patch is issue 2282659
Comment #9
discipolo CreditAttribution: discipolo commentedComment #10
rromore CreditAttribution: rromore commentedHere's my attempt at a patch that utilizes the hook_field_widget_form_alter() function.
Comment #11
das-peter CreditAttribution: das-peter at Cando commentedI'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.Comment #12
GoddamnNoise CreditAttribution: GoddamnNoise at Karakana Factoría TIC commentedI've tried the patch in #11 and it worked for me!. Great job!.
Comment #13
JMTorres CreditAttribution: JMTorres as a volunteer commentedAdded back node title pre-population.
Comment #14
smurfxx CreditAttribution: smurfxx commentedSorry for the question but is the dev version of this issue correct?
In the patches you post I see a bpc_display folder that not exists on 1.x-dev but in 2.x-dev yes.
Do I have to apply all patches to 2.x-dev version?