Hello, I`m trying to add a default value to an exposed search form.

This code (in my template.php) works for the Drupal`s core search form:

function sgtheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    $form['search_block_form']['#default_value'] = t('Buscar');
  }
}

But I can´t get it to work for my exposed form.
My form id is (if I`m not wrong): views-exposed-form-buscador-page

Replacing "search_block_form" for "views-exposed-form-buscador-page" (in both cases) doesn't seems to work.

function sgtheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views-exposed-form-buscador-page') {
    $form['views-exposed-form-buscador-page']['#default_value'] = t('Buscar');
  }
}

Any advice?

Comments

dawehner’s picture

Status: Active » Fixed

It should be possible if you use some method from the devel module to have look how your variable are really look like, for example such kind of $form_id is unlikely. Use dpm($form_id) etc. to reach your goal.

eltioseba’s picture

Thanks, really.

Following your advise I end up in the Drupal API hook_from_alter from where I use these code (in template.php) to discover the variables:

function example_form_alter(&$form, &$form_state, $form_id) {
  dsm($form_id);  // print form ID to messages
  dsm($form);  // pretty print array using Krumo to messages
}

I was using the Themer module with no luck struggling to find the right variable.

My final code looks like these:

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $form['keys']['#default_value'] = t('Custom text before you type');
  }
}

Next step is to switch visibility when you focus the text field.

Status: Fixed » Closed (fixed)

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