Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By leisurman on
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
=-=
The question is better served in the 'module development and code questions' forum. Please edit the post and move it. Thank you.
This works only in drupal 7
This works only in drupal 7 not in drupal 8
I found it this works in drupal 8