1

For a given content type is it possible to make creating a summary mandatory?

1
3

Short Answer

Yes, you can require the summary field be populated before it can be sumbitted.

Details

Implement hook_form_FORM_ID_alter() in a custom module.

hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
   $form['body'][LANGUAGE_NONE][0]['summary']['#required'] = TRUE;
}

This assumes that you are taking the standard "body" field and requiring that it's summary be entered before submission.

0
3

I come after battle but you can have a look to the summary settings module : https://www.drupal.org/project/summary_settings

1
  • Even though this module is in beta release, I find it quick and safe to use. – Sumeet Pareek Jul 22 '14 at 10:46
1

Based on Citricguy's answer...

/**
 * Implements hook_form_FORM_ID_alter().
 */
function summary_required_form_form_id_alter(&$form) {
  // Make the field required.
  $form['body'][LANGUAGE_NONE][0]['summary']['#required'] = TRUE;
  // Remove the class from the wrapper class that triggers
  // the hiding of the summary input.
  $form['body'][LANGUAGE_NONE][0]['summary']['#prefix'] = "<div>";
  // Amend the help text.
  $form['body'][LANGUAGE_NONE][0]['summary']['#description'] = "Enter a short summary";
}
1

I was looking to do the same thing on Drupal 8 and after reading through this post and looking at the new documentation I was able to accomplish it.

I used drupal_set_message($form_id) to get my form ID. Then I used hook_form_FORM_ID_alter.

function hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form['body']['widget'][0]['summary'] = array(
    '#type' => 'textarea',
    '#title' => t("Summary"),    
    '#rows' => 3,
    '#attached' => array('library' => array('text/drupal.text')),
    '#description' => t("Enter trimmed value of full text."),
    '#attributes' => array('class' => array('js-text-summary', 'text-summary')),
    '#weight' => -10,
    '#required' => true,    
  ); 

}

I'm fairly new to Drupal so I don't know if this is the correct way or if there's a easier solution. But this worked for me.

3
  • This worked for me, but I'm curious as to exactly which of these properties triggers it (just setting ['#required'] => true; didn't do it while I was experimenting). – atwixtor Sep 5 '17 at 20:08
  • 1
    Yeah, I struggled with that at first too; I was thinking just by adding '#required'=> true it would work. If I remember correctly it only works if you add all of them. I may be wrong, but I think what is being done is that we're basically recreating the field. – chavab_1 Sep 6 '17 at 20:44
  • @chavab_1 it's not actually the addition of any of those fields that are getting set here. It's the removal of the #prefix and #suffix properties that occurs when you replace the value of $form['body']['widget'][0]['summary'] above. – David Stinemetze Dec 16 '19 at 20:29
0

You could create a separate field on the content type for a summary, and enforce a minimum length so that the user is required to enter something. You probably then want to hide the trimmed output of the main body from the teaser view, and show the separate summary field only in teaser view.

0

Another solution would be to use the field_validation module. You can assign a validation rule to the Body and select the 'Summary' column, and check the length of the entered value for example.

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.