rlhawk

Create a node's summary when the node is saved

Jan 23rd, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Implements hook_node_presave().
  4.  */
  5. function MODULENAME_node_presave($node) {
  6.   if ($node->type == 'news') {
  7.     $summary = $node->body[$node->language][0]['summary'];
  8.     if (empty($summary)) {
  9.       $text = $node->body[$node->language][0]['value'];
  10.       $size = variable_get('teaser_length', 600) + 200;
  11.       $summary = truncate_utf8($text, $size);
  12.       $search = array(
  13.         '#[\r\n\t]#', // Remove line breaks and tabs
  14.         '#<img[^>]*?>#is', // Remove images
  15.         '#<h\d>(.*?)</h\d>#is', // Change headings to strong
  16.         '#<a name="[^"]*"></a>#is', // Remove named anchors
  17.         '#</?p>#is' // Remove paragraph tags
  18.       );
  19.       $replace = array(
  20.         '',
  21.         '',
  22.         '<strong>\1</strong> — ',
  23.         '',
  24.         ' ',
  25.       );
  26.       $summary = preg_replace($search, $replace, $summary);
  27.       $node->body[$node->language][0]['summary'] = text_summary($summary, $node->body[$node->language][0]['format']);
  28.     }
  29.   }
  30. }
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment