For a given content type is it possible to make creating a summary mandatory?
6 Answers
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.
I come after battle but you can have a look to the summary settings module : https://www.drupal.org/project/summary_settings
-
Even though this module is in beta release, I find it quick and safe to use. Commented Jul 22, 2014 at 10:46
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";
}
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.
-
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).– atwixtorCommented Sep 5, 2017 at 20:08 -
1Yeah, 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_1Commented Sep 6, 2017 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. Commented Dec 16, 2019 at 20:29
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.
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.