AnswerBun.com

How to change search placeholder?

Drupal Answers Asked by Tritof on January 6, 2022

So far I have tried this code in mytheme.theme:

function mytheme_form_search_block_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
    $form['keys']['#attributes']['placeholder'] = t('Custom wording');
}

And this one:

function mytheme_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  if ($form_id == 'search_block_form') { 
    $form['keys']['#attributes']['placeholder'] = t('Custom wording');
  }
}

Also this one:

function mytheme_form_search_block_form_alter(&$form, $form_state) {
    $form['keys']['#attributes']['placeholder'] = t('Custom wording');
}

Site is not crashing, but nothing happens on the placeholder.

I have based these 3 snippets on these pages:

One Answer

This code should create the placeholder:

function mytheme_form_search_block_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  $form['keys']['#attributes']['placeholder'] = t('The new placeholder');
}

So if that does not work for you, it might be because you need to clear the cache.

Answered by user72672 on January 6, 2022

Add your own answers!

Related Questions

Changing library path with hook_library_info_alter

1  Asked on September 8, 2020 by extect

   

How can execute batch api over cron jobs

3  Asked on September 6, 2020 by johntang

     

How can I programatically add a shipping method to a store?

1  Asked on September 4, 2020 by vintorg

   

Local Dev .theme file not loading

1  Asked on September 4, 2020 by jmichael

   

Webform submitted data in views

5  Asked on August 31, 2020 by user3783391

 

Export and Import Taxonomies and Extra Fields

1  Asked on August 21, 2020 by monkeyuser

   

How to resolve lazy_builder error?

1  Asked on August 19, 2020 by usernameabc

     

redirect whole site to HTTPS

3  Asked on August 11, 2020 by naomisl

     

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir