Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kitzinger on
Hi everyone,
I managed to use the hook_form_alter to make some changes to the Search Block, search_block_form, like adding placeholder/changing text. I want to do the same for the main search box (not the block) included on the search/node page, and the search/users page. I can't figure out how to do this though! Any clues? Thanks!
Comments
Main search form also has a
Main search form also has a form ID. Just do the same you did with the search block for the main search form.
What's new and changing in PHP 8
Thanks Ayesh, but I'm stumped
Thanks Ayesh, but I'm stumped as to what the form ID for the main search is. Any ideas?
I should probably be more
I should probably be more specific. Here is what I have added to my template.php:
The above code doesn't seem to work. It doesn't break the search page, but when I reload the search page there is no placeholder in the search input box.
Figured it out!
Figured it out!
It's
$form['basic']['keys']['#attributes']['placeholder']
I've used this $form['basic']
I've used this $form['basic']['keys']['#attributes']['placeholder'] = t( 'Ask a Question' ); . But the placeholder for the search has not changed. Is any there way to achieve this ?
Change Placeholder using form Alter Drupal 7 and 8
following code is for Drupal 7 Go To theme to template.php add this code then change theme name.
the following code is for Drupal 8 goto .theme file in Drupal 8 and applies theme code
Best regards,
Omprakash Mankar
Senior Drupal Developer
I open notepad++ and select
I open notepad++ and select Search in Files ... un Search menu.
then enter placeholder for search and select theme folder for directory
for my theme placehoder was in bootstrap\includes\alter.inc
function templatename_form
function templatename_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_block_form') {
$form['search_block_form']['#attributes']['placeholder'] = t('search');
}
}
Drupal 8.9.3
As this is the first item in Google search for adding placeholder in search, and already has the code for Drupal 8; in Drupal 8.9.3 the code bellow worked for me: