I need to set an entity reference select list's #default_value during (or after?) a Feeds import of Commerce products. Two term reference fields are used as arguments in the entity reference view that the product's entity reference field uses, and there is always one result. I set the entity reference field to required to avoid the pesky -None- option in the list, so, when you view a product's page, the entity reference field appears to be filled in correctly. Alas, it is not. No #default_value is set. This is the code I have so far, but I'm not even sure I'm even using the right hooks for this because of the use of Feeds to add and update products:

function set_keywords_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'commerce_product_ui_product_form') {
$form['#after_build'][] = 'set_keywords_after_build';

function set_keywords_after_build($form, &$form_state) {
$form['field_product_keyword']['und']['#default_value'][] = 0; 
return $form; 

The logical thing to do would be to scrap the custom module and set it in the Feeds importer, but so far, I have not found out how to do that. What bugs me the most is that manually going into each product page and simply hitting Save will do exactly what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.