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

Ajax callback does not replace the form but add a new field

1  Asked on January 6, 2022 by billyjoe

 

How to change search placeholder?

1  Asked on January 6, 2022 by tritof

       

Using computed field value in Rules

2  Asked on January 4, 2022

     

Unable to save content language configuration

2  Asked on January 4, 2022 by philipp-michael

     

Workbench: How can I reorder display of Workbench Tabs?

1  Asked on January 2, 2022 by cocoskin

 

Getting the image URL from a field_image on a node

16  Asked on January 2, 2022 by rias

   

Drupal 8 CKEeditor license/pricing

1  Asked on January 2, 2022 by donquixote

   

Retain Guzzle Session During Batch Operations

1  Asked on January 2, 2022 by tyler-frankenstein

   

Fatal error while editing and deleting menu items

1  Asked on December 31, 2021 by ashutoshsngh

   

Field collection item not visible to anonymous user

1  Asked on December 28, 2021 by kevindeleon

 

Get Checkbox Value From Another Form

1  Asked on December 28, 2021

   

How do I change the uploading image file name

1  Asked on December 28, 2021 by i-am-batman

     

get value from content type B, in a view of content type A

1  Asked on December 28, 2021 by bapafe

   

Twig Template print field – if not node number

1  Asked on December 28, 2021 by juc1

 

Ask a Question

Get help from others!

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