In Drupal 8. I noticed that i can create a new node or edit a current node and delete the "Authored By" name, its 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(&$form, $form_state, $form_id) {
$form['author']['name']['#required'] = TRUE;
}

I also tried

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

Comments

VM’s picture

The question is better served in the 'module development and code questions' forum. Please edit the post and move it. Thank you.

leisurman’s picture

This works only in drupal 7 not in drupal 8

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

$form['author']['name']['#required'] = TRUE;
 }
   return $form;
}

I found it this works in drupal 8

<?php

use Drupal\Core\Form\FormStateInterface;

function demo4_form_alter(array &$form, FormStateInterface $form_state, 

$form_id) {
if ($form_id == 'node_page_form') {

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