0

Basically i have a field (in line item(commerce custom product)) with Field type: Entity Reference and Widget:View. The view is a list of terms and has an aditional field type:Entity Reference View Widget Checkbox to provide a checkbox in so user could configure the product.

The whole chain works fine from user point of view but there is no option to set the default value (in UI) and the option to set the default value from hook_form_alter() is not giving the needed result.

This is hook_form_alter:

function test_form_commerce_cart_add_to_cart_form_3_alter(&$form, &$form_state, $form_id){

    $form['line_item_fields']['field_pesonal_data']['und'][0]['field_cover']['#default_value']=6;

}

Please provide a solution. Thx!

0

Did you try casting the value to a string? I had to do that when I was setting default values for a user reference field type. It wouldn't work otherwise. Something like

 $form['line_item_fields']['field_pesonal_data']['und'][0]['field_cover']['#default_value']=(string)6;

Also, I noticed that you had a spelling error. You had 'field_pesonal_data' It should probably be 'field_personal_data'

Also, I was a little confused by your use of terminology. You described that the user is configuring the entity reference field on the product. Did you mean configuring the entity on the line item? I just wanted to clarify, because if you are modifying the form that governs the adding of products (or product variations in Commerce Kickstart 2.0), you need to use a different form alter call because the product variation creation form uses the inline_entity_form module to manage the specific product variations.

That call is:

function hook_inline_entity_form_entity_form_alter(&$entity_form, &$form_state)

Look at the inline_entity_form.api in the contrib/inline_entity_form folder in Commerce Kickstart 2.0

for modifying a user reference on a product, I did the following:

function bh_vendor_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  global $user;
  if ($entity_form['#entity_type'] == 'commerce_product') {
    $entity_form['vendor_user_reference']['und']['#default_value'] = (string)$user->uid;
  }
}

My widget I used was a option list. You have to have the ['und'] or it won't work either.

1
  • "field_pesonal_data" is fine, no mistake was made The user is configuring the product by choosing some options, those options are supplied by line item type (are multiple options so using product variation would become a huge task). Now I tested in hook_form_alter() to set the default value for a textfield and it works as any other form_alter so this concludes that the problem is Widget:View and Entity Reference View Widget Checkbox which seesm to have no support for such things. – Rotari Radu Feb 23 '15 at 11:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.