Hi,
I am trying to use hook_form_alter to change my forms default values which was created in views. I am successful at changing the buttons label but when I try to change the select fields then it does not seem to work.

What am I doing wrong ? I can't seem to figure it out.

<?php

function customfab_form_alter(&$form, &$form_state, $form_id)
{
	
	
	if($form['#id'] == 'views-exposed-form-pol-block') 
{
	$form['submit']['#value'] = t('Search'); // this works and the submit button text changes to search
	
	
	$form['field_color_value']['#default_value'] = "All"; // This does not seem to work.  
	$form['field_color_value']['#value'] = "All"; // This does not seem to work. 
	
	
	}	
	
	}

Cheers,
vishal

Comments

vishalkhialani’s picture

hi, any one i am stuck here ? Thanks vishal

redcrackle.com

spovlot’s picture

Does the "All" value match the possible values in the list? What is the contents of the field_color_value listing?

vishalkhialani’s picture

Hi Spovlot,
Yes "All" is one of the values present and I have attached the below code with the value listings.

Strange it won't work.


<select id="edit-field-color-value" name="field_color_value" class="form-select">

<option value="All">- Any -</option><option value="Black" selected="selected">Black</option><option value="Blue">Blue</option><option value="Brown">Brown</option><option value="Gray">Gray</option><option value="Green">Green</option><option value="Orange">Orange</option><option value="Pink">Pink</option><option value="Purple">Purple</option><option value="Red">Red</option><option value="White">White</option><option value="Yellow">Yellow</option><option value="Gold">Gold</option><option value="Silver">Silver</option></select>

redcrackle.com

hitfactory’s picture

Try setting the default value as an array using the key and value like this:

$form['field_color_value']['#default_value'] = array('All' => '- Any -');

vishalkhialani’s picture

thank you for your help. I used another workaround but I will keep this in mind.

redcrackle.com

vivdrupal’s picture

Please!

Thanks

rohittiwari’s picture

if(array_key_exists("filter-field_name", $selected_filters) && !isset($_GET['field_name'])){
$form_state['input']['field_name'] = value_to_set;
}

jomarocas’s picture

$form['field_color_value']['und']['#default_value'][0] = $value_data;

miguel andrade’s picture

Tnx!

Subhransu.addweb’s picture

Please use this code:

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

Note: All is your key value on select list.

Hope this helps you.

Thanks!

MmMnRr’s picture

Hi,

You can set the value of a "select" field inside "hook_form_alter" with the following code:

  $form['your_field_name'][LANGUAGE_NONE]['#default_value'] = 'the_select_key_value_to_set';
  $form_state['input']['your_field_name'] = 'the_select_key_value_to_set';

I hope this helps.

stuiegreen’s picture

I can't get this to work at all!

I have an exposed form in a view in a node template in Drupal8.

I've tried to set a select default text value to '- Alle -'  from  '- All -' but nothing works.
I have similar issues in another form block too.

Ive tried

      $form['field_event_city']['#default_value'] = array('- Any -' => '- Alle -');
     $form['field_event_city'][LANGUAGE_NONE]['#default_value'] = '- Alle -';
     $form_state['input']['field_event_city'] = 'the_select_key_value_to_set';
     $form['field_event_city']['und']['#default_value'][0] = '- Alle -';

<select data-drupal-selector="edit-field-event-city" id="edit-field-event-city" name="field_event_city" class="form-select">

rhip’s picture

Something like this might work (adding it here in case anybody has the same question):

$form['elements']['field_event_city']['#default_value'] = '- Any -';

Also, check that 'field_event_city' should not be 'event_city'.