1

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

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

And this one:

function mytheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $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:

0

This code should create the placeholder:

function mytheme_form_search_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $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.

  • It didn't work and crashed the site – Tritof Apr 19 '18 at 11:46
  • Try to use the updated code and clear the cache. – Jdrupal Apr 19 '18 at 16:56
  • Didn't work. This is basically my first snippet without the $form_id parameter included in the function. – Tritof Apr 20 '18 at 13:25
  • Oh, that's right the $form_id parameter shall be there, now it should work, just remember to clear the cache. – Jdrupal Apr 20 '18 at 14:59

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.