Score:0

hook_form_alter() to add placeholder to a text field via custom module

vn flag

It should be very straightforward and yet, I can't identify the reason why this doesn't add a placeholder to a field in a form:

function posts_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == "node_post_form" || $form_id == "node_post_edit_form") {
    $form['field_post_subject']['#attributes']['placeholder'] = t("blah");
    dpm($form['field_post_subject']);
  }
}

This confirms that the key placeholder is being set:

enter image description here

The hook_form_alter() works as expected for other things I need to change, except the placeholder. I looked everywhere and it's always the same solution so I'm wondering what I'm missing?

cn flag
Did you rebuild the cache? Also, check your theme template and any preprocessing functions; you may be removing or ignoring the placeholder down the line.
vn flag
Thanks @PatrickKenny. Yes I did rebuild it + tried with other themes including Bartik. I also tried with other forms but with no luck so far.
cn flag
What type of field is it?
leymannx avatar
ne flag
It looks like you set the placeholder on a `'#type' => 'container'` element. You need to go into the `widget`.
vn flag
Thanks for your guidance @leymannx - the solution 4uk4 suggested worked
vn flag
It's a simple plain text field @PatrickKenny
Score:2
cn flag

You are now setting the placeholder attribute to a container, which should be visible in the source HTML, but doesn't have any effect on the form.

You need to add the placeholder to a form element. You find at least one, for multi-value or multi-property fields multiple, inside the widget key (in your debug output a children of the container).

Text fields have already a configurable option for placeholders. If you need to overwrite it dynamically you have to look for the form element and set a new #placeholder.

This is the form element you are looking for:

StringTextfieldWidget::formElement

$element['value'] = $element + [
  '#type' => 'textfield',
  '#default_value' => $items[$delta]->value ?? NULL,
  '#size' => $this->getSetting('size'),
  '#placeholder' => $this->getSetting('placeholder'),
  '#maxlength' => $this->getFieldSetting('max_length'),
  '#attributes' => ['class' => ['js-text-full', 'text-full']],
];
vn flag
Thanks so much for your complete response! For future references, `$form['field_name']['widget'][0]['value']['#placeholder']` is where the placeholder is set.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.