0

I created a content type in a module called travel_page.

In Drupal, I added a new empty List(text) field called English_side_menu

Now I want to populate the List text dynamically.

I did:

/**
 * Implements hook_form_alter().
 */
function travel_page_form_alter(&$form, $form_state, $form_id) {

  // $english_side_menu_list has been initialed here 

  $form['field_english_side_menu']['und']['#options'] = $english_side_menu_list;
  dpm( $form['field_english_side_menu']);   

}

Every thing is working fine, except the value that appears for the select once I submit the button, is the rank of the selection instead of the text selected.

Do you have an idea what is wrong in the code?

Thanks.

1
  • That's how HTML forms works - index of the selected option is submitted, not it's descriptive text. Exactly what you got. And it appears that it's not your code that processes it further, so what really bothers you?
    – Mołot
    Apr 18, 2014 at 7:29

3 Answers 3

1

For dynamic list text no need of form alter for a content type.

You can achieve this goal using Content Construction Kit (CCK) module.

Install this module and configure field as below image enter image description here and define function statement like

function _options_english_side_menu(){
    $english_side_menu_list = array('1' => 'value1', '2' => 'value2');
    return $english_side_menu_list;
}

Using this function you can assign dynamic list to list text.

0

The code should be similar to the following :

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

    if ($form_id == 'ENTER_YOUR_FORM_ID') {

    $english_side_menu_list = array('1' => 'value1', '2' => 'value2');
    //enter the values for the list above similarly
       $form['field_english_side_menu']['und']['#options'][] = $english_side_menu_list;

    }

 }
4
  • I did. It does the same thing. I got English_side_menu:1 when I select value1. I presume I did something wrong when I created the field English_side_menu, but can't show because it is not code. :-(
    – jilusan
    Apr 17, 2014 at 17:54
  • can u give me something like screenshot or code?
    – harshal
    Apr 17, 2014 at 18:32
  • The code, I don't have except the code below. what I did : Add new field, English_side_menu, List(text), Select list, Save, Save field settings, Save settings. I add content, select value1 from English_side_menu, save, what I get is: Submitted by xxx on Thu, 04/17/2014 - 14:50 english_side_menu: 1
    – jilusan
    Apr 17, 2014 at 18:46
  • I found a way : $english_side_menu_list = array('value1' => 'value1', 'value2' => 'value2'); What do you think?
    – jilusan
    Apr 17, 2014 at 19:08
0

That is because of the options you have set using $english_side_menu_list.

The keys of that array are what is submitted when the user selects a value and submits the form.

Either you have to map the selected value back to the human readable value by using the $english_side_menu_list array when you need to (like in the submit handler of the form or elsewhere when displaying that value), or you have to change the $english_side_menu_list array so that the keys are the same as the values (then that is how they will be submitted).

Your Answer

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

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