1

I am using the code found on https://www.drupal.org/node/154137, and I added this code to my template.php file.

function sample_theme_form_search_block_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#value'] = t('GO!'); // Change the text on the submit button
}

The search box label is successfully changed from "Search" to "Go!" However, when a search is executed, the search results page (/search/node/search_term) displays the Search label (even after a page refresh). I'm certain template.php should be overriding this.

Why isn't the Search label overridden from my code?

2
  • It's worth noting, The edits in template.php are firing on the search term page, but an advanced search box is also displaying with the improper text.
    – ConFused
    Commented May 30, 2016 at 2:52
  • Check whether the same theme is getting applied on the Search Result page or not. Commented May 30, 2016 at 5:12

2 Answers 2

1

With sample_theme_form_search_block_form_alter() you are just altering the form whose ID is search_block_form, which is different from the form you see in that page, whose ID is search_form. The Advanced search part is added from node_form_search_form_alter() (another implementation of hook_form_FORM_ID_alter()), so your hook needs to run after it, or you will not see the Advanced search part.

I would use the following code.

function sample_theme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    $form['actions']['submit']['#value'] = t('GO!');
  elseif ($form_id == 'search_form') {
    $form['basic']['submit']['#value'] = t('GO!');
    // The Node module could not have added its form fields, so 
    // check its submit button does exist.
    if (isset($form['advanced']['submit'])) {
      $form['advanced']['submit']['#value'] = t('GO!');
    }
  }
}

Keep in mind that, since you are implementing the alter hook in a theme:

  • It is invoked after all the module hooks, so also node_form_search_form_alter() has been invoked, and you are surely going to alter its submission button too

  • alter hooks implemented by themes are invoked only for the theme currently used for that page, and its base themes; this is different from the hooks implemented by the modules, since all the hooks implemented by enabled modules are going to be invoked.

The last point is particularly important if there is a module that is changing the theme used for a page by implementing hook_custom_theme(), or when the theme for which you are implementing that alter hook is used for the administrative pages, and not the search page.

1
  • Thank you, thank you! I ended up modifying it a bit, and placing brackets after each statement, but this help me do much! function sample_theme_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'search_block_form') { $form['actions']['submit']['#value'] = t('GO!'); } elseif ($form_id == 'search_form') { $form['basic']['submit']['#value'] = t('GO!'); if (isset($form['advanced']['submit'])) { $form['advanced']['submit']['#value'] = t('GO!'); } } } Any future readers, this code was placed in template.php.
    – ConFused
    Commented May 31, 2016 at 4:52
1

Instead of trying the code in template file try the following code in a custom module.

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "your_required_form_id") {
    $form['actions']['submit']['#value'] = t('GO!');
  }
}

Hope this will help you

2
  • 1
    The problem is not running the hook from a theme; in fact, alter hooks are invoked also for themes.
    – avpaderno
    Commented May 30, 2016 at 5:53
  • Thanks much, Abin, I ended up going with the other solution, but very much appreciate your help!
    – ConFused
    Commented May 31, 2016 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.