I'm new to Drupal forms - trying to figure out a maddening problem.

I have a content type of 'doc-upload' with various fields including 'publisher_code'.

When I define the 'publisher_code' field type to 'text', and go through my upload process, yada yada yada, the value appears in my $node structure later as expected.

When I change 'publisher_code' to "field type: List (text), widget: Select list", and I try to dynamically populate that element with:

function mymodule_form_doc_upload_node_form_alter(&$form, &$form_state) {

  $publishers = array( 'pub1' => 'Publisher 1', 'pub2' => 'Publisher 2' );

  $form['field_publisher_code'] = array(
    '#title' => t('Select a publisher'),
    '#type' => 'select',
    '#options' => $publishers,
    '#multiple' => FALSE,
    '#empty_value' => '',
    '#empty_option' => '- None -',
    '#required' => TRUE

The form displays properly, but when the form is submitted, the 'field_publisher_code' doesn't come out the other side - presumably it's getting filtered/invalidated somewhere, but I can't seem to find where?

  • what is the type of the Publisher_code? and is there some reason you dont want use the select list already available in the content type field? Jan 20 '13 at 4:12
  • When I define 'publisher_code' as a regular text field, and enter a value on the 'create new doc-upload content type', it appears in the $node object after all the processing.
    – HeyBigE
    Jan 20 '13 at 13:42
  • why dont you add the field to the content type rather than doing this. Jan 20 '13 at 13:46
  • The field 'publisher_code' IS added to content-type 'doc-upload'. I need to dynamically build the list of publishers (which is another content-type). My example is limited in scope to illustrate the true nature of the problem - if 'publisher_code' is a regular text field and NOT modified in the form_alter() function above, it works fine. When I try to change the field type to 'Select' and modify it in the function, it doesn't. I don't know if it's because of the field Select type, or that I'm changing the field incorrectly.
    – HeyBigE
    Jan 20 '13 at 13:54
  • Do you want to list all the nodes in the other content type or a sub set of that? If you want to list all then you can add an entity reference field and then use it instead. Jan 20 '13 at 13:56

instead of over writing the form element just edit variables you want to change something like below

  $publishers = array( 'pub1' => 'Publisher 1', 'pub2' => 'Publisher 2' );

  $form['field_publisher_code']['#type'] = 'select';
  $form['field_publisher_code']['#options'] = $publishers;
  $form['field_publisher_code']['#multiple'] = FALSE;

I presume your $form already has the variable when you get the form.

  • Bingo - this is the answer I was looking for.. thank you!
    – HeyBigE
    Jan 21 '13 at 16:42

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.