I'm trying to insert a form element with hook_form_alter(), and then i'm trying to set this element as default value.
This is the code:

function custom_form_alter(&$form, &$form_state, $form_id) {


  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-test-page') {
    $form['tid']['#after_build'] = array('_add_default_option_select');
}

if($form['#id'] == 'views-exposed-form-test-page') {
  if (empty($_GET['tid']))  {
    $form_state['input']['tid'] = 'Select a value';
    }
  }

   
 }
        
function _add_default_option_select($element, &$form_state) {
    $element['#options'] = array('' => 'Select a value') + $element['#options'];
    return ($element);
}

It seems to work but a red frame appears around the new item "Select a value".

This message is also shown:
An illegal choice has been detected. Please contact the site administrator.

Can anyone let me know how to do ?

Thank you

Comments

Redart’s picture

Issue summary: View changes

New code

dawehner’s picture

Status: Active » Fixed
$form_state['input']['tid'] = 'Select a value';

This should be

$form_state['input']['tid'] = '';

There might be some caching going on, so what about hook_form_alter directly.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

new code