Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drupal #default_value not applied in select option in drupal_render

I try to print a drupal 'select option' element in a form .I think drupal_render not applying #default_value.every thing is ok except #default_value not applied.
where is the problem?anybody know how i can do this? do #default_value accept string value?

this is pseudo of my codes:

function test_menu(){
$items=array();

    $items['admin/config/regional/test']=array(
    'title' => 'test',
    'description' => t('test'),
    'page callback' =>'drupal_get_form',
    'page arguments' => array('test_function'),

);
$items[]=array();
return $items;
}


function test_function(){
 $header = array
  (
  'test1' => t('test1'),
  'test2'=> t('test2'),
  );
 $a=(1,2,3);
 $$options=array();
 foreach($a as $i=>$v)
  {
    $f['type'] = array(
   '#type' => 'select',
   '#options' => array(1,2,3,4),
   '#default_value'=>1,
    );
$options += array($name=>array( 'test1' => $v,
   'test2'=> drupal_render($f['type']) ,
  }
   $form['table'] = array
   (
   '#type' => 'tableselect',
   '#header' => $header,
   '#options' => $options,
   '#multiple' => FALSE
   //'#empty' => t('No users found'),
   );
   $form['submit'] = array
       (
   '#type' => 'submit',
   '#value' => t('Submit'),
    );
   return $form;
 }    

I test textfield but its also not work and not accept #default_value in drupal_render

    $f['test3']=array(
    '#type'=>'textfield',
    '#title'=>'test3',
    '#default_value' =>'aaa',
);

I suppose this is beacuse using drupal_render .anybody have a solution?

like image 271
Yuseferi Avatar asked Jul 19 '12 05:07

Yuseferi


2 Answers

In Drupal_render 's used in drupal_get_form , #default_value not set use must use #value instaed of it.

$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#value'=> '1',
);
like image 161
Yuseferi Avatar answered Sep 25 '22 19:09

Yuseferi


The following code doesn`t work:

$form['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));


$form['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));

return $form;

But then i did the following:

$form['group'] = array('#tree' => TRUE);

$form['group']['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));


$form['group']['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));

return $form;

And default values now works.

like image 41
Iaroslav Avatar answered Sep 22 '22 19:09

Iaroslav