-
Notifications
You must be signed in to change notification settings - Fork 0
/
excerpt.module
107 lines (95 loc) · 4.05 KB
/
excerpt.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
// $Id: excerpt.module,v 1.13 2009/03/31 01:25:09 weitzman Exp $
/**
* Implementation of hook_nodeapi().
*/
function excerpt_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (variable_get('excerpt_'. $node->type, 1)) {
switch ($op) {
case 'submit':
// Due to the presence of the teaser field in the node form,
// node.module doesn't generate a teaser from the body upon submit.
// In case the field has been left empty, it's up to us to generate one.
if (trim($node->teaser) == '') {
$node->teaser = isset($node->body) ? node_teaser($node->body, isset($node->format) ? $node->format : NULL) : '';
}
break;
case 'load':
// Support for nodes saved with earlier versions of excerpt. The teaser
// will now be created when submitting the node form to take some load
// from the server.
if (trim($node->teaser) == '' && $node->body != '') {
$node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
}
// node.module just checks whether the length of the teaser is less
// than the body length to decide whether a "read more" link is to be
// shown. With excerpts however, teasers can be even longer than the
// body field, which is why we need to overwrite that property with our
// own condition.
return array('excerpt' => (bool)strcmp($node->teaser, $node->body));
case 'view':
$node->readmore = $node->excerpt;
break;
}
}
}
/**
* Implementation of hook_form_alter().
*/
function excerpt_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form') {
$form['workflow']['excerpt']['excerpt'] = array(
'#type' => 'radios',
'#title' => t('Teaser'),
'#default_value' => variable_get('excerpt_'. $form['#node_type']->type, 1),
'#options' => array(t('Auto-generated'), t('Manual excerpt')),
'#description' => t('Choose whether the node teaser must be generated automatically or manually entered by the author.'),
);
if (!module_exists('content')) {
$form['workflow']['excerpt']['excerpt_wt'] = array(
'#type' => 'weight',
'#title' => t('Weight of Teaser field'),
'#default_value' => variable_get('excerpt_wt_'. $form['#node_type']->type, 0),
);
}
}
else if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && variable_get('excerpt_'. $form['type']['#value'], 1) == 1) {
$form['teaser'] = array(
'#type' => 'textarea',
'#title' => t('Teaser'),
'#default_value' => $form['#node']->teaser,
'#cols' => 60,
'#rows' => 6,
'#weight' => module_exists('content') ? content_extra_field_weight($form['type']['#value'], 'teaser') : variable_get('excerpt_wt_'. $form['type']['#value'], 0),
'#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.'),
);
$form['body_field']['body']['#default_value'] = $form['#node']->body;
if(isset($form['body_field']['teaser_js'])) {
unset($form['body_field']['teaser_js']);
unset($form['body_field']['teaser_include']);
if(!empty($form['body_field']['#after_build'])) {
if ($id = array_search('node_teaser_js', $form['body_field']['#after_build'])) {
unset($form['body_field']['#after_build'][$id]);
}
if($id = array_search('node_teaser_include_verify', $form['body_field']['#after_build'])) {
unset($form['body_field']['#after_build'][$id]);
}
}
}
}
}
/**
* Implementation of hook_content_extra_fields.
*/
function excerpt_content_extra_fields($type_name) {
$type = node_get_types('type', $type_name);
$extra = array();
if (variable_get('excerpt_'. $type_name, 1)) {
$extra['teaser'] = array(
'label' => t('Excerpt'),
'description' => t('Manual excerpt'),
'weight' => variable_get('excerpt_wt_'. $type_name, 0),
);
}
return $extra;
}