In Drupal 7, when the Body field is defined as "Long text with a summary", the summary field is displayed if the user clicks on the link labeled "Edit summary."

There's no apparent way to either display the summary field by default or make it required.

The problem is that if the Summary field isn't displayed, it will get ignored. On virtually every site that my company builds, we use required custom teaser fields so we can create a teaser that is not limited by the severe constraint of producing a teaser from the first part of the body field. The "Long text with summary" field type partially addresses this same need, but it's too easy to ignore, which leads to page summaries made by clipping the body copy, which we regard as really problematic.

So, I'd like to be able to do either or both of two things:

  1. Make the summary required (which would presumably kick the user back and display the summary field if they didn't click to edit it the first time);
  2. display the summary field by default. (This is my preferred solution.)

Any suggestions?

Comments

escoles’s picture

Workaround: Add   as default text to the Summary field, which will:

  1. force the Summary to display;
  2. over-ride display of the teaser as a truncation of the body field.

Not a very satisfactory solution, as it prevents failover to a truncation-summary. Better solution would be to have the summary field visible at all times, and not visible only when requested.

alexweber’s picture

This is BY FAR the best workaround! Just edit the field settings, click on the summary button and add a blank space as the default value! Works like a charm :)

charles belov’s picture

I've been using this. The downside is that hardly anyone thinks to delete the non-breaking space when adding content. And staff still overlook the field.

Actually, even if I put a prompt to add some text, many staff don't enter anything, and the prompt shows up on the website.

The only real solution that is guaranteed to get content into that field is to leave it empty, always show it, and make it required.

Charles Belov
SFMTA Webmaster
http://www.sfmta.com/

acbramley’s picture

Anyone come up with a viable fix for this? Defaulting summary text to a space is not really an option for me.

kenwest’s picture

The showing and hiding of the summary is done by the code in /modules/field/modules/text/text.js

One could hack core (shudder) or write some js to reverse the initial hiding of the summary.

jonathan_hunt’s picture

Try hook_js_alter() to override the path for modules/field/modules/text/text.js.
In your local version of text.js you can just comment out the $link.click(); on line 41 - this will stop the initial collapse of an empty field.

See more at http://drupal.org/node/756722

acbramley’s picture

Thanks a lot!

alltooeasy’s picture

Is this Naughty? Or is this okay to do? So will this force just the summary empty field from collapsing or all of them?

If so is there a means to select solely the summary field on. Or just have it forced on by default. Seems like a common problem.

kristougher’s picture

// Trigger click on summary link.
  if (arg(0) == 'node' && arg(1) == 'add') {
    $script = 'jQuery(document).ready(function(){ jQuery(".link-edit-summary").click(); });';
    drupal_add_js($script, 'inline');
  }

.. did it for me :)

dan.mantyla’s picture

Worked perfect for me, thank you!

badjava’s picture

If you want display the summary and just get rid of the show/hide functionality and make it a required field, create a custom module with the following code (replace mymodule with your module name):

/**
 * Implementation of hook_js_alter();
 */
function mymodule_js_alter(&$javascript) {
  // remove the JS that does the show/hide summary
  unset($javascript['modules/field/modules/text/text.js']);
}

/**
 * Implementation of hook_node_validate();
 */
function mymodule_node_validate($node, $form, &$form_state) {
  if (!isset($node->body['und'][0]['summary']) || !$node->body['und'][0]['summary']) {
    form_set_error('Node', t('You must enter a summary!'));
  } 
}

The above assumes you don't have any languages set up, otherwise the hook_node_validate code will be slightly different.

~Chad

kcoonce’s picture

Adding a space may be a cheap trick, but it gets the job done. It's solved quite a few issues with my site by doing this. Thank you thank you thank you!

mrpauldriver’s picture

Yes we use the summary field and it is important for the mechanics of our sites and is usually populated by site owners/editors. However, I want the summary to be closed by default as it confuses site contributors and often results in them deleting or changing the summary when we don't want them to

There is not a one size fits all solution here. But there is a good case for a custom module and I wonder whether one exists already??? It should behave as below.

Set defaults for node summary

  • Always closed by default
  • Always open by default
  • Only open if populated

It should probably exist as a conditional field which is revealed when 'Summary input' is selected on a content type.

If anyone can point me to a module that does this. I'd very very happy indeed :-)

---
Paul Driver
www.easable.uk
Ilkley, West Yorkshire, UK

mstrelan’s picture

I've just created a new module for this. Please see https://drupal.org/project/summary_settings

tart0’s picture

Hi,
If you wanna to let the summaries openned by default in field_collection, you could use this :

function MODULE_form_FORMID_node_form_alter(&$form, &$form_state, $form_id) {
  // By default, add a blank space to each summaries in order to force display them.
  $delta = 0;
  $max_delta = $form['YOUR FIELD COLLECTION'][LANGUAGE_NONE]['#max_delta'];

  while ($delta <= $max_delta) {
      $form['YOUR FIELD COLLECTION'][LANGUAGE_NONE][$delta]['THE BODY WITH SUMMARY FIELD'][LANGUAGE_NONE][0]['summary']['#default_value'] = '&nbsp;';
      $delta++;
  }
}

Regards, Benftwc ;)

Kukulcan’s picture

All other ideas where nice but have their drawbacks

Road Kill’s picture

One would think that such a simple but obvious requirement would be standard core. I ran into this problem when D7 was first launched and solved the problem by creating and additional body called summary which was time intensive but gave control over character limitation display and required.

Thank you Mstrelan for this great module will make my job and many others much more simpler.

Works like a charm :)

charles belov’s picture

The downside of a separate field is that it will not show up in the teaser.

Charles Belov
SFMTA Webmaster
http://www.sfmta.com/

susan5in7’s picture

If you want to show up the separate field in teaser, you have to use DS module.

carnau’s picture

May be a little late, but I leave it as reference for future.

function mymodule_js_alter(&$javascript) {
  // Removes the js that hides the summary field in node add form.
  unset($javascript['modules/field/modules/text/text.js']);
}
aerozeppelin’s picture

Thanks for posting this.

dan.mantyla’s picture

To make the summary field both required AND visible by default, use the following code in your custom module:

function CUSTOMMODULE_form_alter(&$form, &$form_state, $form_id){
  if ($form_id == "article_node_form") {
    $form['body'][LANGUAGE_NONE][0]['summary']['#required'] = TRUE;
    $script = 'jQuery(document).ready(function(){ jQuery(".link-edit-summary").click(); });';
    drupal_add_js($script, 'inline');
  }
Bagz’s picture

text.js works off the summary field having class "text-summary".
If you change the class to something else then the code in text.js won't execute, and it won't show the "Edit summary" or "Hide summary" links either. The result is that the summary and body fields will show as two separate normal fields.

To achieve this effect add this code to a custom module:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form') {
    $form['body'][LANGUAGE_NONE][0]['summary']['#attributes']['class'] = array('text-summary-no-links');
  }
}
AndyThornton’s picture

In true Drupal style, here is another way to solve it. I took the best of the solutions from above (thanks), but in my case I wanted to be able to have this setting as an option on all summary fields. This can be improved on to disable the choice if the summary field is disabled ... but here you go ...

If you use this, you will need to go to your Manage Fields page and check the 'Summary is required' field. When checked, the field is made visible by default and required (the description text is tweaked a little too).

function MYMODULE_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (isset($form['#field']['type']) && $form['#field']['type'] == 'text_with_summary') {
    $form['instance']['settings']['summary_is_required'] = array(
        '#type' => 'checkbox',
        '#title' => t('Summary is required'),
        '#default_value' => isset($instance['settings']['summary_is_required']) ? $instance['settings']['summary_is_required'] : '0',
        '#description' => t('When checked the summary field will be made visible by default and will be a mandatory field.'),
    );
  }
}

function MYMODULE_field_widget_form_alter(&$element, &$form_state, &$context) {
  if (isset($context['instance']['settings']['summary_is_required']) && $context['instance']['settings']['summary_is_required'] && (!isset($form_state['input']['form_id']) || $form_state['input']['form_id'] != 'field_ui_field_edit_form')) {
    $element['summary']['#required'] = TRUE;
    $element['summary']['#description'] = t("You must provide a summary value.");

    if(($key = array_search('text-summary',$element['summary']['#attributes']['class'])) !== false) {
      unset($element['summary']['#attributes']['class'][$key]);
    }
  }
}
altcom_alan’s picture

This works great, there's one error in the code though: it's using a variable $instance that's not be initialised, here's the correct version:

function MYMODULE_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (isset($form['#field']['type']) && $form['#field']['type'] == 'text_with_summary') {
    $instance = $form['#instance'];
    $form['instance']['settings']['summary_is_required'] = array(
        '#type' => 'checkbox',
        '#title' => t('Summary is required'),
        '#default_value' => isset($instance['settings']['summary_is_required']) ? $instance['settings']['summary_is_required'] : '0',
        '#description' => t('When checked the summary field will be made visible by default and will be a mandatory field.'),
    );
  }
}

Also for my use case I decided to split the options to always show and make required into two options and added a max word count option.

I created a simple module called text_summary and here's the full text_summary.module for anyone that wants to use it:

function text_summary_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (isset($form['#field']['type']) && $form['#field']['type'] == 'text_with_summary') {
    $instance = $form['#instance'];
    
    $form['instance']['settings']['summary_is_required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Summary is required'),
      '#default_value' => isset($instance['settings']['summary_is_required']) ? $instance['settings']['summary_is_required'] : '0',
      '#description' => t('When checked the summary field will be a mandatory field.'),
    );
    $form['instance']['settings']['summary_always_visible'] = array(
      '#type' => 'checkbox',
      '#title' => t('Summary always visible'),
      '#default_value' => isset($instance['settings']['summary_always_visible']) ? $instance['settings']['summary_always_visible'] : '0',
      '#description' => t('When checked the summary field will be made visible by default.'),
    );
    $form['instance']['settings']['summary_max_words'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum words'),
      '#default_value' => isset($instance['settings']['summary_max_words']) ? $instance['settings']['summary_max_words'] : '0',
      '#description' => t('Enter the maximum number of words for the summary, leave blank or enter 0 for unlimited.'),
    );
  }
}

function text_summary_field_widget_form_alter(&$element, &$form_state, &$context) {
  if (!isset($element['summary']) || (isset($form_state['input']['form_id']) && $form_state['input']['form_id'] == 'field_ui_field_edit_form')) {
    return;
  }
  if (isset($context['instance']['settings']['summary_is_required']) && $context['instance']['settings']['summary_is_required']) {
    $element['summary']['#required'] = TRUE;
    $element['summary']['#description'] = t("You must provide a summary value.");
  }
  if (isset($context['instance']['settings']['summary_always_visible']) && $context['instance']['settings']['summary_always_visible']) {
    if (($key = array_search('text-summary', $element['summary']['#attributes']['class'])) !== false) {
      unset($element['summary']['#attributes']['class'][$key]);
    }
  }
  if (isset($context['instance']['settings']['summary_max_words']) && $context['instance']['settings']['summary_max_words']) {
    $element['summary']['#element_validate'][] = 'text_summary_max_words_validate';
    $element['summary']['#max_words'] = $context['instance']['settings']['summary_max_words'];
    $element['summary']['#description'] .= ' Maximum '. $context['instance']['settings']['summary_max_words'] .' words.';
  }
}

function text_summary_max_words_validate(&$element, $form, $form_state) {
  $num_words = str_word_count(strip_tags($element['#value']));
  
  if ($num_words > $element['#max_words']) {
    $over = $num_words - $element['#max_words'];
    form_error($element, $element['#title'] .' field has a maximum of '. $element['#max_words'] .' words, please remove at least '. $over. ' words.');
  }
}
shane birley’s picture

I noticed this thread was a bit older but still active. There is a module for most of what has been suggested in this thread:

https://www.drupal.org/project/text_summary_options

---
Shane Birley
Left Right Minds
https://www.leftrightminds.com

millionleaves’s picture

I agree. This module will display the Summary field by default, and also allows you to make the Summary field required. Available for both D7 and D8.

I provide Drupal, Drupal Commerce and CiviCRM development services for customers in New Zealand and beyond

kerby70’s picture

In a hook_form_FORM_ID_alter() or hook_form_alter()
To remove the js attachment.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MY_MODULE_form_MY_FORM_ID_alter(&$form, $form_state) {
  if (isset($form['body'][$language][0]['summary']['#attached'])) {
    unset($form['body']['und'][0]['summary']['#attached']);
  }
}
bduell’s picture

That worked great - thank you!

johnhanley’s picture

I discovered unsetting the #attributes property achieves the same result by removing the classes from the wrapper div:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (isset(unset($form['body']['widget'][0]['summary']['#attributes'])) {
    unset($form['body']['widget'][0]['summary']['#attributes']);
  }
}

This technique is also compatible with Inline Entity Form, where unsetting #attached failed to disable the jQuery collapsible behavior:

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function hook_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  if (isset($entity_form['body']['widget'][0]['summary']['#attributes'])) {
    unset($entity_form['body']['widget'][0]['summary']['#attributes']);
  }
}
hobare’s picture

I used [node:field_summary] and it worked perfectly. Could there be any downside to using that?