I want to change the default -select a value- from a text list to a something like -please select a choice-

enter image description here

This is what I have:

  function project_form_alter(&$form, &$form_state, $form_id){
    if($form['#id'] == 'edit-field-gen-list'){
    $form['field_gen_list']['#default_value'] = 'all';

It's not picking up the value.

enter image description here

This is the HTML.

  • 1
    Are you sure that $form_id is edit-field-gen-list. I think it's edit_field_gen_list. Try kint($form_id) to see it.
    – Jonh
    Jan 9, 2017 at 10:09
  • i have tried that too --- its not picking it up... its a text list on a contact form Jan 9, 2017 at 10:59
  • 1
    Better you need kint($form) and show it on question. It's better for your question.
    – Jonh
    Jan 9, 2017 at 11:05

4 Answers 4

  • In Drupal 8 type-hint $form_state with the FormStateInterface & do not pass it by reference:

    hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)

    See hook_form_alter() for further reference.

  • I see some confusion on your usage of the $form_id / $form['#id']. Dump your $form_id in the hook & see which is the correct form you want to alter. Because as others have mentioned already: edit-field-gen-list is not your form id but your field_gen_list field id. For example: the form id of the contact Website feedback is contact_message_feedback_form.

  • Given the HTML in your question, your $form['field_gen_list']['widget']['#options'] will not have a key for 'all'. So adding that as default value will not work unless you add such a key value to the options array

    So instead try this:

    function project_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
        kint($form_id, 'Find correct form id');
        if ($form_id == 'contact_message_feedback_form') { // Change this
            // Add an 'all' key value option
            $form['field_gen_list']['widget']['#options']['all'] = '- All -';
            // Set newly added key 'all' as default value
            $form['field_gen_list']['widget']['#default_value'] = 'all';

Try this:

function project_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'edit-field-gen-list') {
    $form['field_gen_list']['#default_value'] = 'all';

I'm not quite sure if the form element is in $form['field_gen_list'] though.

  • I tried it as you posted and also without the $form['field_gen_list'] -- still not working ... Btw is a text list created for contact form. Jan 9, 2017 at 8:41
  • Just to be sure: you're using Drupal 8, and not Drupal 7? Jan 9, 2017 at 12:59

Can you try this:

$form['field_gen_list']['und']['#default_value'] = 'all';

Note: All is your key value on select list.

Hope this helps you.

  • i think in your select list not have 'all' key and value. like: <option value="all"> All </option> Jan 9, 2017 at 9:24
  • tried it still no result... ` function project_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'edit-field-gen-list') { $form['field_gen_list']['und']['#default_value'] = 'all'; } }` Jan 9, 2017 at 9:48

In your code, replace the line $form['field_gen_list']['#default_value'] = 'all'; by:

// save the array of field options in a var.
$field_options = $form['field_gen_list']['und']['#options'];
// change the value of the '_none' key to the one you need.
$field_options['_none'] = '- please select a choice -';
// set the modified array of options as the options for this field
$form['field_gen_list']['und']['#options'] = $field_options;

That's it!!

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.