Adding WISIWYG Support To Drupal 7 Node Summaries

31st December 2011 - 6 minutes read time

I often get asked a simple request during a project, and the solution to the problem is sometimes more complex than I originally thought. One of these problems was adding a WYSIWYG editor to the summary field on the node edit form. There isn't an easy way to do this, but it is possible to get a good solution working.

Using the hook_form_alter() hook we can intercept and change the node edit form to change the type of the summary element from a textarea to a text_format element. In order to get the WYSIWYG component of the form working we will need to also add a format to the form element. For the purposes of this example I have created a module called wysiwyg_summary, so the hook is called wysiwyg_summary_form_alter().

  1. function wysiwyg_summary_form_alter(&$form, &$form_state, $form_id) {
  2. if ($form_id == 'page_node_form' && isset($form['nid']) && isset($form['body'])) {
  3. $form['body'][$form['language']['#value']][0]['summary']['#format'] = 'filtered_html';
  4. $form['body'][$form['language']['#value']][0]['summary']['#type'] = 'text_format';
  5. }
  6. }

This seems to work ok, but the first thing I noticed is that clicking on "Show summary" has a few weird effects on certain elements of text, adding multiple "Hide summary" labels. It turns out that the culprit of the problems is the text.js file from the node field text module (located at /modules/field/modules/text/text.js). This JavaScript file is included on the node form and needs to be updated slightly to allow for the difference in the edit summary form element. All we need to do to fix this is to change the find() function so that it only finds the first label of the parent element, rather than every label. Here is the new file in full.

  1. (function ($) {
  3. /**
  4.  * Auto-hide summary textarea if empty and show hide and unhide links.
  5.  */
  6. Drupal.behaviors.textSummary = {
  7. attach: function (context, settings) {
  8. $('.text-summary', context).once('text-summary', function () {
  9. var $widget = $(this).closest('div.field-type-text-with-summary');
  10. var $summaries = $widget.find('div.text-summary-wrapper');
  12. $summaries.once('text-summary-wrapper').each(function(index) {
  13. var $summary = $(this);
  14. var $summaryLabel = $summary.find('label:first');
  15. var $full = $widget.find('.text-full').eq(index).closest('.form-item');
  16. var $fullLabel = $full.find('label:first');
  18. // Create a placeholder label when the field cardinality is
  19. // unlimited or greater than 1.
  20. if ($fullLabel.length == 0) {
  21. $fullLabel = $('').prependTo($full);
  22. }
  24. // Setup the edit/hide summary link.
  25. var $link = $('(' + Drupal.t('Hide summary') + ')').toggle(
  26. function () {
  27. $summary.hide();
  28. $(this).find('a').html(Drupal.t('Edit summary')).end().appendTo($fullLabel);
  29. return false;
  30. },
  31. function () {
  32. $;
  33. $(this).find('a').html(Drupal.t('Hide summary')).end().appendTo($summaryLabel);
  34. return false;
  35. }
  36. ).appendTo($summaryLabel);
  38. // If no summary is set, hide the summary field.
  39. if ($(this).find('.text-summary').val() == '') {
  40. $;
  41. }
  42. return;
  43. });
  44. });
  45. }
  46. };
  48. })(jQuery);

To add this to the node form in the place of the original we need to alter the attached attribute of the summary field, therefore replacing the existing file. Here is the updated code.

  1. function wysiwyg_summary_form_alter(&$form, &$form_state, $form_id) {
  2. if ($form_id == 'page_node_form' && isset($form['nid']) && isset($form['body'])) {
  3. $form['body'][$form['language']['#value']][0]['summary']['#format'] = 'filtered_html';
  4. $form['body'][$form['language']['#value']][0]['summary']['#type'] = 'text_format';
  5. $form['body'][$form['language']['#value']][0]['summary']['#attached']['js'] = array(drupal_get_path('module', 'wysiwyg_summary') . '/text.js');
  6. }
  7. }

The final step in this is to allow the saving of the new structure of the data from the summary element. The best way to do this is to convert the data back to a format that Drupal expects using a submit function. The first thing to do is add the submit hook to the wysiwyg_summary_form_alter(). We use the array_unshift() function here so that the submit function is called before any other submit functions (specifically the core ones).

  1. /**
  2.  * Implements hook_form_alter().
  3.  */
  4. function wysiwyg_summary_form_alter(&$form, &$form_state, $form_id) {
  5. if ($form_id == 'page_node_form' && isset($form['nid']) && isset($form['body'])) {
  6. $form['body'][$form['language']['#value']][0]['summary']['#format'] = 'filtered_html';
  7. $form['body'][$form['language']['#value']][0]['summary']['#type'] = 'text_format';
  8. $form['body'][$form['language']['#value']][0]['summary']['#attached']['js'] = array(drupal_get_path('module', 'wysiwyg_summary') . '/text.js');
  9. array_unshift($form['#submit'], 'wysiwyg_summary_form_alter_summary_submit');
  10. }
  11. }

The job of the submit function is to change the value from the format summary element to a non-formatted summary element. One important thing to consider here is that although this text is entered using a filtered text element, it is not saved or printed using any specific text format. This therefore means that any text entered will be printed, including any malicious scripts. To get around this we can pass the entered text through the filter_xss() function to remove any of these problems. The tags we allow through here are enough to get rudimentary support for bold and links in text, which is what the original request was all about.

  1. /**
  2.  * Submit function defined by wysiwyg_summary_form_alter().
  3.  */
  4. function wysiwyg_summary_form_alter_summary_submit($form, &$form_state) {
  5. $allowed_tags = array('a', 'em', 'strong', 'cite', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'p');
  6. if (isset($form_state['input']['body'][$form['language']['#value']][0]['summary']['value'])) {
  7. $form_state['input']['body'][$form['language']['#value']][0]['summary'] = filter_xss($form_state['input']['body'][$form['language']['#value']][0]['summary']['value'], $allowed_tags);
  8. }
  10. if (isset($form_state['values']['body'][$form['language']['#value']][0]['summary']['value'])) {
  11. $form_state['values']['body'][$form['language']['#value']][0]['summary'] = filter_xss($form_state['values']['body'][$form['language']['#value']][0]['summary']['value'], $allowed_tags);
  12. }
  13. }

With this module in place the summary field now has partial WYSIWYG support. The only real problem is that it doesn't save or update filter formats at all so you won't have full control over how the content of the teaser is processed. To stop user confusion over the fact that the filter formats section is there but not usable, it might be a good idea to hide the format section using CSS. All that is needed is to put a display:none; rule on the ID edit-body-und-0-summary-format.

Update 01/10/2012: Alternatively, you could use the check_markup() function if the filter_xss() function is too restrictive. The good thing with using check_markup() is that it ties into your text filters (in this case it uses 'filtered_html') and therefore give you better control over what happens to the text.


Thank's, your blogpost "Adding WISIWYG Support To Drupal 7 Node Summaries" was very helpfull. Good luck!

David (Mon, 11/05/2012 - 19:11)

Why haven't you made this a contributed module?

Ray (Tue, 05/07/2013 - 20:08)

Mainly because it was a very specific solution to the problem and didn't seem generic enough for a full blown module. Do you think it would be useful as a module in it's own right? I don't mind collating things and sandboxing it :)

Thank you very much Phil, this post still relevant today and fortunately everything works as expected :) A couple of comments:

- There's no need to add the custom javascript file anymore, it looks like recent versions of text.js have been changed to look for the first label element only:

var $fullLabel = $full.find('label').first();

- When using this code in multi language sites, it's more reliable to look for the language of the body field rather than the one in the form, like this:


Proteo (Wed, 09/19/2018 - 17:12)

Add new comment

The content of this field is kept private and will not be shown publicly.