Score:1

Make Title field "not required" or workaround how to prefill and hide it (drupal 9.2.8)

ug flag

On my site (drupal 9.2.8) users add content and there is no need to put any information to 'Title' field. I was trying to make this field not required via form_alter in custom module but it did not work:

function yourmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  if ($form['#form_id'] == 'node_somenode_form' or $form['#form_id'] == 'node_somenode_form'){
    $form['title']['widget']['0']['#required'] = FALSE;    // not working
    $form['title']['widget']['#required'] = FALSE;    // not working
    $form['title']['#required'] = FALSE;    // not working
  }
}

As a workaround in my module I prefill Title using form_alter:

$form['title']['widget']['0']['value']['#default_value'] = 'any default title';

and then just hide this field from form. There are a few ways of doing that (In form alter what is the different way of hiding fields and what is the difference between them? ), I decided to use hide:

hide($form['title']);

but I am curies why ['#required'] = FALSE; doesn't work for Title field but it works fine with other fields.

Kuldeep K avatar
in flag
You can try this https://drupal.stackexchange.com/questions/183477/how-to-programmatically-create-a-title-for-a-new-node
Score:1
bd flag

You should use Automatic Entity Label:

"Automatic Entity Label" is a small and efficient module that allows hiding of entity label fields.

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.