0

I need a form_alter code or something that I could use to set my field value. I'm getting the value from the parameter and I've tried a lot of ways but nothing works for me. This is the list of codes I've tried on my custom module:

function custommodule_node_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#node']->type == 'custom_package') {
      $default = $_GET['field_product_add_ons'];
      $form["field_add_on_nids"][LANGUAGE_NONE][0]['value']['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE][0]['value']['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE][0]['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE]['#default_value'] = $default;

  }


  if($form_id == 'custom_package') {
      $default = $_GET['field_product_add_ons'];
      $form["field_add_on_nids"][LANGUAGE_NONE][0]['value']['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE][0]['value']['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE][0]['#default_value'] = $default;
      $form['field_add_on_nids'][LANGUAGE_NONE]['#default_value'] = $default;

  }
}

I think the code will be simple but the field is still blank. My field is just a single text field.

Thank you in advance for your help!

4 Answers 4

0

Is your custom module called custommodule_node or just custommodule? Assuming it's custommodule, your function should look like below. You use -> because node is an object.

function custommodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'custom_package') {
      $default = $_GET['field_product_add_ons'];
      $form->field_add_on_nids[LANGUAGE_NONE][0]['value'] = $default;
  }
}

also, there is no if ($form['#node']->type == 'custom_package')

Use if ($form_id == 'custom_package') to match $form_id.

1
  • Almost there, I removed what was unneeded. I also used dsm to double check everything. I found out that my form_id lacks "_node_form" at the end. Thank you.
    – Danz
    May 28, 2015 at 7:09
0

Try

 $form['field_add_on_nids']['und']['#default_value'] = $default;

Thanks

0

You can try either of these way any one will work

Option 1:

$form['field_name']['und']['#default_value'][0] = $default_value;

Option 2:

$form['field_name']['und']['#default_value'] = $default_value;

option 3 :

$form['field_name']['und'][0]['value']['#default_value']= $default_value;
0

I would try the following, as it works for me:

$form['field_myfield'][LANGUAGE_NONE]['#default_value'] = "value";

I believe that using LANGUAGE_NONE is considered a better practice than 'und'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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