2

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(&$form, $form_state, $form_id) {
$form['author']['name']['#required'] = TRUE;
}

I also tried this:

function demo4_form_alter(&$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(&$form, $form_state, $form_id) {
if ($form_id == 'page_node_form') {

$form['author']['name']['#required'] = TRUE;
 }
   return $form;
}
  • You don't need to return the $form in a form alter, it is passed by reference. Second, you have to type hint $form_state with FormStateInterface. You need the use statement as well. – Kevin Feb 7 '17 at 19:22
  • So do it like this? use Drupal\Core\Form\FormStateInterface; function demo4__form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $form['author']['name']['#required'] = TRUE; } – paulcap1 Feb 7 '17 at 20:49
  • By using the use statement you don't have to specify the fully qualified name - only the name itself, so just FormStateInterface $form_state is needed. See php.net/manual/en/language.namespaces.importing.php – Kevin Feb 7 '17 at 20:50
  • I tried this but its still not required- use Drupal\Core\Form\FormStateInterface; function demo4__form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { $form['author']['name']['#required'] = TRUE; } – paulcap1 Feb 7 '17 at 20:58
  • Yes I was mainly saying the syntax of your code needed correction. Are you certain that's the form field key? Have you debugged your code to see it add #required? – Kevin Feb 7 '17 at 22:25
1

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(&$form, $form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    $form['uid']['widget'][0]['target_id']['#required'] = TRUE;
  }
}
  • Thank you that works. I enabled Devel and Devel Kint. Then in devel settings I checked Kint and display page array. Then placed this inside hook form alter- dpm($form); And then I went to create a basic page and I see the Input array of 41 items but I dont see the elements for the admin settings box, like author by, revision...? Where can I find information on how to use devel and kint to find elements? – paulcap1 Feb 8 '17 at 14:55
  • 1
    Incase anyone needs this code here it is, it may be hard to read. 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; } – paulcap1 Feb 8 '17 at 15:07
  • 1
    THanks again CliveCleaves. With dpm I did find all the elements inside the admin box like - uid, author, revision, and inside those I found the widget and title – paulcap1 Feb 8 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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