0
$results = db_select('users', 'u')->fields('u', ['name', 'uid', 'mail'])->execute();
$options = [];
foreach ($results as $result) {
  $options[$result->uid] = $result->name . ' | ' . $result->mail;
}
unset($options[array_search(0, $options)]);
$form['user'] = [
  '#type' => 'select',
  '#title' => t('Select User'),
  '#options' => $options,
  '#default_value' => ('select a value'),
  '#states' => [
    'visible' => [
      ':input[name="data_set"]' => ['value' => 'education'],
    ],
  ],
];
$form['submit'] = [
  '#type' => 'submit',
  '#value' => t('Expire'),
];

I have this dropdown , how can I get the default value like Select a value or something

1 Answer 1

0

For required items a -select- item is added automatically. If the form field is not a required one, you'll have to add it manually, for example with the following code.

  $options = array('_none' => '-select item-') + $options; //Places an extra option at the start

If it's the first item it should be selected automatically but if you want to be safe.

  // Use the array key, not the value.
  '#default_value' => '_none', 

You'll have to filter it out the '_none' value in the submit handler afterwards.

2
  • Sorry @Suranga, I did not get your answer, should I use Soptions=array('_none'=>'select item-') + $options;? or #default_value=>'_none'?
    – nomeer
    May 13, 2015 at 3:54
  • You need to use both.
    – apaderno
    May 13, 2015 at 5:32

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.