Drupal 6 form textfield #default_value not working

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
asif.mulla's picture

Surprisingly textfield #default_value is not working in views exposed form. I have enabled search field exposed form and using hook_form_alter I am setting its #default_value property. But while rendering it doesn't show default value. However I found that inside views_handler_filter_search.inc when I change form type textfield to textarea as shown below:

function exposed_form(&$form, &$form_state) {
    if (isset($this->options['expose']['identifier'])) {
      $key = $this->options['expose']['identifier'];
      $form[$key] = array(
        '#type' => 'textfield',
        '#size' => 15,
        '#default_value' => $this->value,
        '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
      );
    }
  }

TO

  function exposed_form(&$form, &$form_state) {
    if (isset($this->options['expose']['identifier'])) {
      $key = $this->options['expose']['identifier'];
      $form[$key] = array(
        '#type' => 'textarea',
        '#size' => 15,
        '#default_value' => t('Search'),
        '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
      );
    }
  }

default value start appearing.

Could you please help me out to rectify this issue.

EDIT:

Along with Search textfield I have taxonomy select menu filter to narrow search results. When I set #after_built function and inside function when I printed form variable, found that #post is by default set as shown below:

Array
(
    [#type] => textfield
    [#size] => 15
    [#default_value] => Search
    [#attributes] => Array
        (
            [title] => Voer hier een berichtnummer of tekst in..
            [class] => inputbox
        )

    [#after_build] => Array
        (
            [0] => after_built_search
        )

    [#post] => Array
        (
            [term] => All
            [form_build_id] => form-d08424eee7a527d39d7569acb028599f
            [form_id] => views_exposed_form
        )

    [#programmed] => 
    [#tree] => 
    [#parents] => Array
        (
            [0] => keys
        )

    [#array_parents] => Array
        (
            [0] => keys
        )

    [#weight] => 0
    [#processed] => 1
    [#description] => 
    [#required] => 
    [#input] => 1
    [#maxlength] => 128
    [#autocomplete_path] => 
    [#process] => Array
        (
            [0] => form_expand_ahah
        )

    [#name] => keys
    [#id] => edit-keys
    [#value] => 
    [#needs_validation] => 1
    [#defaults_loaded] => 1
    [#sorted] => 1
)

Comments

default_value doesn't work

merlinofchaos's picture

default_value doesn't work very well in GET based forms, because fapi was never designed for the situation.

Views usually checks $form_state['input'][$identifier'] and if it is not set, it puts the default value there; that makes fapi think the value was properly posted. Otherwise it usually thinks that a null value was posted and uses that over the default. But only put it there if the input is NOT set, so you don't override an actual post.

#post is by default set

asif.mulla's picture

Thanks merlinofchaos for quick comment. Also I have edited my question and found that #post is by default set to taxonomy term "All" even though I haev not submitted my form. May be because of this #default_value is not getting set. Is it so? Do you have any solution?

Possible relationship

ndewhurst's picture

It appears that I may have solved a similar problem I was having, by turning off the "#defaults_loaded" flag for the form element in question (in my case, a Select). It seems that if that value is TRUE/1, any default value settings are ignored under the assumption that they've all been set and finalized. I haven't explored this in detail and can't guarantee it will solve your issue, but I hope it helps.

Edit It appears that the new, desired result was actually achieved by directly setting the element's '#value' setting, and the applicability of the '#default_value', unfortunately, seems to be unaffected by the '#defaults_loaded' value.