I know this has been asked but so far none of the accepted answers for this issue are working for me. I am attempting to add placeholder text to the default search form block. I am using a theme we'll call MyTheme, which is a copy of Bartik and is otherwise working exactly as expected.
I have tried adding the following functions to MyTheme.theme:
function MyTheme_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$form['edit-keys']['#attributes']['placeholder'][] = t('Search');
}
And
function MyTheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'search_block_form') {
// Add placeholder text
$form['keys']['#attributes']['placeholder'] = t('Search');
}
}
And many, many variations on those. I have tried both functions with and without "\Drupal\Core|form..." included. I have tried both with 'keys' and 'edit-keys'. I have tried both with and without the empty '[]' after placeholder. Nothing has worked.
In every case, using hook_form_FORM_ID_alter simply fails to do anything at all, while using hook_form_alter causes a 'white screen of death'.
I don't understand what I'm doing wrong.
UPDATE: Now out of the blue Drupal has decided to change the id for the form input to 'edit-keys--2' for some reason. I've tried using that in the functions as well.