1

Please help to understand what the problem is. I created a field Select to list in the comments to type of material field values ​​I needed my, I redefined options list in the Custom Module

$query = db_select('node', 'n')
        ->fields('n', array('type', 'uid', 'title', 'nid'))
        ->condition('n.type', 'brigada_type', '=');
     $result = $query->execute();
     $brigade_list = array();
        foreach($result as $value){
             $brigade_list[$value->nid] = $value->title;    
        }
        $form['field_select_brigade']['und']['#options'] = array(0 => t('- Select -')) + $brigade_list;

After I select a value from the list and create a comment I check it, and then only published but during the inspection the default value strays.

before enter image description here

after enter image description here

2 Answers 2

1

Solved the problem yourself with the help of code. It was necessary to create a handler instead comment_form_submit and create in him the values ​​of a variable_set of my field value, so, I create custom field.

    function comment_author_form_comment_form_alter(&$form, &$form_state, $form_id){
           if($form_id == 'comment_node_zayavka_form'){
             $query = db_select('node', 'n')
                ->fields('n', array('type', 'uid', 'title', 'nid'))
                ->condition('n.type', 'brigada_type', '=');
             $result = $query->execute();
             $brigade_list = array();
                foreach($result as $value){
                    //if($value->uid == $user->uid){
                     $brigade_list[] = $value->title;
                    //} 
                }
                $form['field_select_brigade_1'] = array(
                    '#type' => 'select',
                    '#title' => t('Select brigade'),
                    '#options' => $brigade_list,
                    '#description' => t('Please select the team that was sent to you with reference to this application.'),
                    '#required' => TRUE,
                    '#default_value' => variable_get('comment_field_brigade_selection'),
                );
        $form['#submit'] = array('comment_author_comment_form_submit');
        }
function comment_author_comment_form_submit($form, &$form_state){
        if(isset($form_state['values']['field_select_brigade_1'])){
              variable_set('comment_field_brigade_selection', $form_state['values']['field_select_brigade_1']);
            }
        }
1
  • this solution still did not help me Commented Oct 10, 2013 at 15:29
0

You also need to assign a #default_value on your select form element in order to have the preselected option.

$form['field_select_brigade']['und']['#default_value'] =  $brigade_list[$preselected_key];
4
  • where the variable $preselected_key and how it formed? Commented Oct 9, 2013 at 7:20
  • The $preselected_key is something that you need to handle in your custom module (either from the $form_state or some value that has been saved in database). The value of it is the key of the saved option. Commented Oct 9, 2013 at 10:12
  • the fact is that there is no $form_state values​​ Commented Oct 9, 2013 at 12:59
  • I found value in the database table field_data_field_select_brigade, but I do not think that query the database again and get out of there value is the right solution, you can do it easier? Commented Oct 9, 2013 at 13:45

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.