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

Ayesh’s picture

Main search form also has a form ID. Just do the same you did with the search block for the main search form.


function mymodule_form_alter($form, $form_state, $form_id) {
  drupal_set_message($form_id);
}
kitzinger’s picture

Thanks Ayesh, but I'm stumped as to what the form ID for the main search is. Any ideas?

kitzinger’s picture

I should probably be more specific. Here is what I have added to my template.php:

function TEMPLATE_form_alter( &$form, &$form_state, $form_id ) {
if (in_array( $form_id, array( 'search_form')))
    {
        // Adding placeholders to fields
	$form['keys']['#attributes']['placeholder'] = t( 'Enter search terms...' );
    }
}

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.

kitzinger’s picture

Figured it out!

It's

$form['basic']['keys']['#attributes']['placeholder']

Logeshvaran’s picture

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 ?

omrmankar’s picture

following code is for Drupal 7 Go To theme to template.php add this code then change theme name.

function your_theme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if (in_array($form_id, ['search_block_form', 'search_form'])) {
        $form['keys']['#attributes']['placeholder'] = t('Search...');
    }
   }

the following code is for Drupal 8 goto .theme file in Drupal 8 and applies theme code

function  TEMPLATE_form_alter( &$form, &$form_state, $form_id ) {
if (in_array( $form_id, array( 'search_form')))
    {
        // Adding placeholders to fields
	$form['basic']['keys']['#attributes']['placeholder'] = t( 'How can we Help !!!!!' );
    }
}

Best regards,

Omprakash Mankar
Senior Drupal Developer

namjoo’s picture

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

websaz’s picture

function templatename_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'search_block_form') {
        $form['search_block_form']['#attributes']['placeholder'] = t('search');
    }
}
 

deltaag’s picture

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:

function TEMPLATE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    $form['keys']['#attributes']['placeholder'] = t('search');
  }
}