1

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.

1

1 Answer 1

1

You should use Automatic Entity Label:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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