How to make author field required? In Drupal 8. I noticed that I can create a new node or edit a current node and delete the "Authored By" name, it's blank and when I hit save the node is authored by Anonymous.

How can I make this Author field required to be a current user in the system?

And the page will show a message and will not save until a name is entered. I tried this but it does not work:

use Drupal\Core\Form\FormStateInterface;

function demo4_form_alter(&amp;$form, $form_state, $form_id) {
$form['author']['name']['#required'] = TRUE;
}


I also tried this:

function demo4_form_alter(&amp;$form, $form_state, $form_id) {
if ( $form_id == 'page_node_form'){
$form['author']['name']['#required'] = TRUE;
}
return $form;
}


This works only in Drupal 7 not in Drupal 8, And I changed page_node_form to node_page_form for Drupal 8

function demo4_form_alter(&amp;$form, $form_state, $form_id) {
if ($form_id == 'page_node_form') {

$form['author']['name']['#required'] = TRUE;
 }
   return $form;
}
What you're looking for is this:

$form['uid']['widget'][0]['target_id']['#required'] = TRUE;

(Devel is your friend!)

You were on the right track as $form['author']['#required'] = TRUE; will make the "Authoring Information" have a red star (*), but won't make your authored by field required.

Complete code:

function hook_form_alter(&amp;$form, $form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    $form['uid']['widget'][0]['target_id']['#required'] = TRUE;
  }
}