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.

  • 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 '17 at 10:09
  • i have tried that too --- its not picking it up... its a text list on a contact form – The Naga Tanker Jan 9 '17 at 10:59
  • Better you need kint($form) and show it on question. It's better for your question. – Jonh Jan 9 '17 at 11:05
  • 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';
|improve this answer|||||

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.

|improve this answer|||||
  • 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. – The Naga Tanker Jan 9 '17 at 8:41
  • Just to be sure: you're using Drupal 8, and not Drupal 7? – Wim Mostrey Jan 9 '17 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.

|improve this answer|||||
  • i think in your select list not have 'all' key and value. like: <option value="all"> All </option> – Anil Tiwari Jan 9 '17 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'; } }` – The Naga Tanker Jan 9 '17 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!!

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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