| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* adds a second title field (subtitle)
|
| 5 |
* based on nodeapi_example module
|
| 6 |
* @endcode
|
| 7 |
*/
|
| 8 |
|
| 9 |
function subtitle_help($section) {
|
| 10 |
switch ($section) {
|
| 11 |
case 'admin/modules#description':
|
| 12 |
// This description is shown in the listing at admin/modules.
|
| 13 |
return t('adds a subtitle field to nodes');
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
function subtitle_nodeapi(&$node, $op, $teaser, $page) {
|
| 18 |
switch ($op) {
|
| 19 |
|
| 20 |
// First, we need to have a way for administrators to indicate which content
|
| 21 |
// types can have our subtitle field added. The "settings" operation allows us to
|
| 22 |
// place this option on the content type configuration page. Note that the
|
| 23 |
// variable we use to store this information needs to be named using both the
|
| 24 |
// name of this module (to avoid namespace conflicts) and the name of the node
|
| 25 |
// type the setting is for.
|
| 26 |
case 'settings':
|
| 27 |
$settings = array();
|
| 28 |
$settings[t('enable subtitle')] = form_checkbox('enable subtitle field', 'subtitle_'. $node->type, 1, variable_get('subtitle_'. $node->type, FALSE));
|
| 29 |
return $settings;
|
| 30 |
|
| 31 |
// Next we provide a way for users to enter the information for our new field.
|
| 32 |
// The "form pre" operation allows us to insert information in front of the
|
| 33 |
// rest of the form; we could instead use "form post" or "form admin" to place
|
| 34 |
// the input element elsewhere.
|
| 35 |
case 'form pre':
|
| 36 |
$form = '';
|
| 37 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 38 |
$form .= form_textfield(t('Subtitle'), 'subtitle', $node->subtitle, 80, 255);
|
| 39 |
}
|
| 40 |
return $form;
|
| 41 |
|
| 42 |
// Now that the form has been properly completed, it is time to commit the new
|
| 43 |
// data to the database.
|
| 44 |
case 'insert':
|
| 45 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 46 |
db_query("INSERT INTO {subtitle} (nid, subtitle) VALUES (%d, '%s')", $node->nid, db_escape_string($node->subtitle));
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
|
| 50 |
// If the form was called to edit an existing node rather than create a new
|
| 51 |
// one, this operation gets called instead. We use a DELETE then INSERT rather
|
| 52 |
// than an UPDATE just in case the subtitle didn't exist for some reason.
|
| 53 |
case 'update':
|
| 54 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 55 |
db_query('DELETE FROM {subtitle} WHERE nid = %d', $node->nid);
|
| 56 |
if ($node->subtitle) {
|
| 57 |
db_query("INSERT INTO {subtitle} (nid, subtitle) VALUES (%d, '%s')", $node->nid, db_escape_string($node->subtitle));
|
| 58 |
}
|
| 59 |
}
|
| 60 |
break;
|
| 61 |
|
| 62 |
// If the node is being deleted, we need this opportunity to clean up after
|
| 63 |
// ourselves.
|
| 64 |
case 'delete':
|
| 65 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 66 |
db_query('DELETE FROM {subtitle} WHERE nid = %d', $node->nid);
|
| 67 |
}
|
| 68 |
break;
|
| 69 |
|
| 70 |
// Now we need to take care of loading one of the extended nodes from the
|
| 71 |
// database. An array containing our extra field needs to be returned.
|
| 72 |
case 'load':
|
| 73 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 74 |
$object = db_fetch_object(db_query('SELECT subtitle FROM {subtitle} WHERE nid = %d', $node->nid));
|
| 75 |
return array('subtitle' => $object->subtitle);
|
| 76 |
}
|
| 77 |
break;
|
| 78 |
|
| 79 |
// Finally, we need to take care of displaying our subtitle when the node is
|
| 80 |
// viewed. This operation is called after the node has already been prepared
|
| 81 |
// into HTML and filtered as necessary, so we know we are dealing with an
|
| 82 |
// HTML teaser and body. We will inject our additional information at the front
|
| 83 |
// of the node copy.
|
| 84 |
//
|
| 85 |
// Using nodeapi('view') is more appropriate than using a filter here, because
|
| 86 |
// filters transform user-supplied content, whereas we are extending it with
|
| 87 |
// additional information.
|
| 88 |
case 'view':
|
| 89 |
if (variable_get('subtitle_'. $node->type, FALSE)) {
|
| 90 |
$node->body = theme('subtitle', $node->subtitle) . $node->body;
|
| 91 |
$node->teaser = theme('subtitle', $node->subtitle) . $node->teaser;
|
| 92 |
}
|
| 93 |
break;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
|
| 98 |
function theme_subtitle($subtitle) {
|
| 99 |
$output = '<h2 class="subtitle">'.check_plain($subtitle).'</h2>';
|
| 100 |
return $output;
|
| 101 |
}
|
| 102 |
?>
|