| 1 |
<?php
|
| 2 |
// $Id: excerpt.module,v 1.14 2009/10/08 17:44:44 hanoii Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_nodeapi().
|
| 6 |
*/
|
| 7 |
function excerpt_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 8 |
if (variable_get('excerpt_'. $node->type, 1)) {
|
| 9 |
switch ($op) {
|
| 10 |
case 'submit':
|
| 11 |
// Due to the presence of the teaser field in the node form,
|
| 12 |
// node.module doesn't generate a teaser from the body upon submit.
|
| 13 |
// In case the field has been left empty, it's up to us to generate one.
|
| 14 |
if (trim($node->teaser) == '') {
|
| 15 |
$node->teaser = isset($node->body) ? node_teaser($node->body, isset($node->format) ? $node->format : NULL) : '';
|
| 16 |
}
|
| 17 |
break;
|
| 18 |
|
| 19 |
case 'load':
|
| 20 |
// Support for nodes saved with earlier versions of excerpt. The teaser
|
| 21 |
// will now be created when submitting the node form to take some load
|
| 22 |
// from the server.
|
| 23 |
if (trim($node->teaser) == '' && $node->body != '') {
|
| 24 |
$node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
|
| 25 |
}
|
| 26 |
// node.module just checks whether the length of the teaser is less
|
| 27 |
// than the body length to decide whether a "read more" link is to be
|
| 28 |
// shown. With excerpts however, teasers can be even longer than the
|
| 29 |
// body field, which is why we need to overwrite that property with our
|
| 30 |
// own condition.
|
| 31 |
$excerpt = db_fetch_object(db_query("SELECT * FROM {excerpt} WHERE nid = %d AND vid = %d", $node->nid, $node->vid));
|
| 32 |
return array(
|
| 33 |
'excerpt' => (bool)strcmp($node->teaser, $node->body),
|
| 34 |
'teaser_format' => $excerpt->format,
|
| 35 |
);
|
| 36 |
|
| 37 |
case 'delete revision':
|
| 38 |
db_query("DELETE FROM {excerpt} WHERE nid = %d and vid = %d", $node->nid, $node->vid);
|
| 39 |
break;
|
| 40 |
|
| 41 |
case 'delete':
|
| 42 |
db_query("DELETE FROM {excerpt} WHERE nid = %d", $node->nid);
|
| 43 |
break;
|
| 44 |
|
| 45 |
case 'update':
|
| 46 |
// Existing nodes may not have an excerpt information stored,
|
| 47 |
// Check to see if we need to update one or create a new one.
|
| 48 |
$excerpt = db_fetch_object(db_query("SELECT * FROM {excerpt} WHERE nid = %d AND vid = %d", $node->nid, $node->vid));
|
| 49 |
if (empty($node->revision) && $excerpt) {
|
| 50 |
$primary_keys = array('vid');
|
| 51 |
}
|
| 52 |
case 'insert':
|
| 53 |
$excerpt = array();
|
| 54 |
$excerpt['vid'] = $node->vid;
|
| 55 |
$excerpt['nid'] = $node->nid;
|
| 56 |
$excerpt['format'] = $node->teaser_format;
|
| 57 |
drupal_write_record('excerpt', $excerpt, $primary_keys);
|
| 58 |
break;
|
| 59 |
|
| 60 |
case 'view':
|
| 61 |
$node->readmore = $node->excerpt;
|
| 62 |
|
| 63 |
// Because now we control the format of the teaser, we should check
|
| 64 |
// its markup against it
|
| 65 |
// See @node_prepare, @node_build_content
|
| 66 |
// As we are 'setting' the teaser, and not adding content,
|
| 67 |
// we want this module's hook to be called first of all,
|
| 68 |
// that's why the module's weight is set to -10 in the install file
|
| 69 |
$node->teaser = check_markup($node->teaser, $node->teaser_format, FALSE);
|
| 70 |
break;
|
| 71 |
}
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Implementation of hook_form_alter().
|
| 77 |
*/
|
| 78 |
function excerpt_form_alter(&$form, $form_state, $form_id) {
|
| 79 |
if ($form_id == 'node_type_form') {
|
| 80 |
$form['workflow']['excerpt']['excerpt'] = array(
|
| 81 |
'#type' => 'radios',
|
| 82 |
'#title' => t('Teaser'),
|
| 83 |
'#default_value' => variable_get('excerpt_'. $form['#node_type']->type, 1),
|
| 84 |
'#options' => array(t('Auto-generated'), t('Manual excerpt')),
|
| 85 |
'#description' => t('Choose whether the node teaser must be generated automatically or manually entered by the author.'),
|
| 86 |
);
|
| 87 |
if (!module_exists('content')) {
|
| 88 |
$form['workflow']['excerpt']['excerpt_wt'] = array(
|
| 89 |
'#type' => 'weight',
|
| 90 |
'#title' => t('Weight of Teaser field'),
|
| 91 |
'#default_value' => variable_get('excerpt_wt_'. $form['#node_type']->type, 0),
|
| 92 |
);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
else if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && variable_get('excerpt_'. $form['type']['#value'], 1) == 1) {
|
| 96 |
$form['teaser_field'] = array(
|
| 97 |
'#weight' => module_exists('content') ? content_extra_field_weight($form['type']['#value'], 'teaser') : variable_get('excerpt_wt_'. $form['type']['#value'], 0),
|
| 98 |
);
|
| 99 |
$form['teaser_field']['teaser'] = array(
|
| 100 |
'#type' => 'textarea',
|
| 101 |
'#title' => t('Teaser'),
|
| 102 |
'#default_value' => $form['#node']->teaser,
|
| 103 |
'#cols' => 60,
|
| 104 |
'#rows' => $form['body_field']['body']['#rows'],
|
| 105 |
'#description' => t('Enter an excerpt for this item. It will be shown on listing pages along with a <em>read more</em> link which leads to the full view. Leave empty to auto-generate one from the body.'),
|
| 106 |
);
|
| 107 |
$form['teaser_field']['format'] = filter_form($form['#node']->teaser_format, 1, array('teaser_format'));
|
| 108 |
|
| 109 |
$form['body_field']['body']['#default_value'] = $form['#node']->body;
|
| 110 |
|
| 111 |
if(isset($form['body_field']['teaser_js'])) {
|
| 112 |
unset($form['body_field']['teaser_js']);
|
| 113 |
unset($form['body_field']['teaser_include']);
|
| 114 |
|
| 115 |
if(!empty($form['body_field']['#after_build'])) {
|
| 116 |
if ($id = array_search('node_teaser_js', $form['body_field']['#after_build'])) {
|
| 117 |
unset($form['body_field']['#after_build'][$id]);
|
| 118 |
}
|
| 119 |
if($id = array_search('node_teaser_include_verify', $form['body_field']['#after_build'])) {
|
| 120 |
unset($form['body_field']['#after_build'][$id]);
|
| 121 |
}
|
| 122 |
}
|
| 123 |
}
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Implementation of hook_content_extra_fields.
|
| 130 |
*/
|
| 131 |
function excerpt_content_extra_fields($type_name) {
|
| 132 |
$type = node_get_types('type', $type_name);
|
| 133 |
$extra = array();
|
| 134 |
|
| 135 |
if (variable_get('excerpt_'. $type_name, 1)) {
|
| 136 |
$extra['teaser'] = array(
|
| 137 |
'label' => t('Excerpt'),
|
| 138 |
'description' => t('Manual excerpt'),
|
| 139 |
'weight' => variable_get('excerpt_wt_'. $type_name, 0),
|
| 140 |
);
|
| 141 |
}
|
| 142 |
|
| 143 |
return $extra;
|
| 144 |
}
|