By vishalkhialani on
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
hi, any one i am stuck here ?
hi, any one i am stuck here ? Thanks vishal
redcrackle.com
Does the value match?
Does the "All" value match the possible values in the list? What is the contents of the field_color_value listing?
Re: Does the value match ?
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.
redcrackle.com
Set the default value using the key and value
Try setting the default value as an array using the key and value like this:
$form['field_color_value']['#default_value'] = array('All' => '- Any -');The Oatmeal: 8 Websites You Need to Stop Building
thank you for your help. I
thank you for your help. I used another workaround but I will keep this in mind.
redcrackle.com
Could you please post the workaround you used?
Please!
Thanks
if(array_key_exists("filter
if(array_key_exists("filter-field_name", $selected_filters) && !isset($_GET['field_name'])){
$form_state['input']['field_name'] = value_to_set;
}
for me working
$form['field_color_value']['und']['#default_value'][0] = $value_data;Tnx!
Tnx!
Re: How to set a select fields default value using hook_form_alt
Please use this code:
Note: All is your key value on select list.
Hope this helps you.
Thanks!
Hi,
Hi,
You can set the value of a "select" field inside "hook_form_alter" with the following code:
I hope this helps.
HELP!
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">Something like this might
Something like this might work (adding it here in case anybody has the same question):
Also, check that 'field_event_city' should not be 'event_city'.