2

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.

4
  • I assume your theme name is actually "MyTheme" as that part needs to match exactly as your theme's machine name – Leigh Feb 23 '18 at 17:04
  • I used "MyTheme" as an example here but yes I do understand that those need to match and they do. – Anonymous Man Feb 23 '18 at 17:07
  • Hey did you find out? I am interested because I have the same issue. I temporarily use some css (.form-search::placeholder {opacity:0}) and Jquery (new placeholder then opacity 1) to change it on the fly but this is not a solution. – Tritof Apr 19 '18 at 13:54
  • @Tritof Yes, I did get it working. I added my solution and accepted it below. I hope it helps you. – Anonymous Man Apr 20 '18 at 17:13
2

This is what eventually worked for me. Posting because other people seem to be having the same issue.

I placed this at the very end of the MyTheme.theme file.

/**
 * Add placeholder text to the search forms.
 */
function MyTheme_form_search_block_form_alter(&$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['keys']['#attributes']['placeholder'] = t('Search this site...');
}

Hopefully this will be helpful for some.

1

So, I've tried this on my local install and had the exact same issue.

Please try changing $form['keys']['#attributes']['placeholder'] to $form['keys']['#title']

Your function would be -

function MyTheme_form_search_block_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    $form['keys']['#title'] = t("Search");
}
2
  • Welcome to Drupal Answers! This answer doesn't help much the OP, since it isn't telling him with what he should replace 'search_block_form' in $form['search_block_form']['#attributes']. – kiamlaluno Feb 24 '18 at 18:34
  • See what the OP said in the question. He tried $form['keys']['#attributes']['placeholder'] and $form['edit-keys']['#attributes']['placeholder']. Without telling him what exactly to use instead of edit-keys or keys, this answer is telling him something he already know. – kiamlaluno Feb 25 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.