| 1 |
<?php
|
| 2 |
// $Id: nodeapi_example.module,v 1.8 2008/09/15 07:26:20 davereid Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is an example outlining how a module can be used to extend existing
|
| 7 |
* content types.
|
| 8 |
*
|
| 9 |
* We will add the ability for each node to have a "rating," which will be a
|
| 10 |
* number from one to five.
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_form_alter().
|
| 15 |
*
|
| 16 |
* By implementing this hook, we're able to modify any form. We'll only make
|
| 17 |
* changes to two types: a node's content type configuration and edit forms.
|
| 18 |
*
|
| 19 |
* We need to have a way for administrators to indicate which content types
|
| 20 |
* should have our rating field added. This is done by inserting a checkbox in
|
| 21 |
* the node's content type configuration page.
|
| 22 |
*/
|
| 23 |
function nodeapi_example_form_alter(&$form, $form_state, $form_id) {
|
| 24 |
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
|
| 25 |
// Alter the node type's configuration form to add our setting. We don't
|
| 26 |
// need to worry about saving this value back to the variable, the form
|
| 27 |
// we're altering will do it for us.
|
| 28 |
$form['workflow']['nodeapi_example'] = array(
|
| 29 |
'#type' => 'radios',
|
| 30 |
'#title' => t('NodeAPI Example Rating'),
|
| 31 |
'#default_value' => variable_get('nodeapi_example_'. $form['#node_type']->type, 0),
|
| 32 |
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
|
| 33 |
'#description' => t('Should this node have a rating attached to it?'),
|
| 34 |
);
|
| 35 |
}
|
| 36 |
// If the type and node field are set this may be a node edit form.
|
| 37 |
elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
|
| 38 |
// If the rating is enabled for this node type, we insert our control
|
| 39 |
// into the form.
|
| 40 |
$node = $form['#node'];
|
| 41 |
if (variable_get('nodeapi_example_'. $form['type']['#value'], 0)) {
|
| 42 |
$form['nodeapi_example_rating'] = array(
|
| 43 |
'#type' => 'select',
|
| 44 |
'#title' => t('Rating'),
|
| 45 |
'#default_value' => isset($node->nodeapi_example_rating) ? $node->nodeapi_example_rating : '',
|
| 46 |
'#options' => array(0 => t('Unrated'), 1, 2, 3, 4, 5),
|
| 47 |
'#required' => TRUE,
|
| 48 |
'#weight' => 0,
|
| 49 |
);
|
| 50 |
}
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Implementation of hook_nodeapi().
|
| 56 |
*
|
| 57 |
* We will implement several node API operations here. This hook allows us to
|
| 58 |
* act on all major node operations, so we can manage our additional data
|
| 59 |
* appropriately.
|
| 60 |
*/
|
| 61 |
function nodeapi_example_nodeapi(&$node, $op, $teaser, $page) {
|
| 62 |
switch ($op) {
|
| 63 |
// When the content editing form is submitted, we need to validate the input
|
| 64 |
// to make sure the user made a selection, since we are requiring the rating
|
| 65 |
// field. We have to check that the value has been set to avoid showing an
|
| 66 |
// error message when a new blank form is presented. Calling form_set_error()
|
| 67 |
// when the field is set but zero ensures not only that an error message is
|
| 68 |
// presented, but also that the user must correct the error before being able
|
| 69 |
// to submit the node.
|
| 70 |
case 'validate':
|
| 71 |
if (variable_get('nodeapi_example_'. $node->type, TRUE)) {
|
| 72 |
if (isset($node->nodeapi_example_rating) && !$node->nodeapi_example_rating) {
|
| 73 |
form_set_error('nodeapi_example_rating', t('You must rate this content.'));
|
| 74 |
}
|
| 75 |
}
|
| 76 |
break;
|
| 77 |
|
| 78 |
// Now we need to take care of loading one of the extended nodes from the
|
| 79 |
// database. An array containing our extra field needs to be returned.
|
| 80 |
case 'load':
|
| 81 |
$rating = db_result(db_query('SELECT rating FROM {nodeapi_example} WHERE nid = %d', $node->nid));
|
| 82 |
return array('nodeapi_example_rating' => $rating);
|
| 83 |
break;
|
| 84 |
|
| 85 |
// Insert is called after the node has been validated and saved to the
|
| 86 |
// database. It gives us a chance to create our own record in the database.
|
| 87 |
case 'insert':
|
| 88 |
db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
|
| 89 |
break;
|
| 90 |
|
| 91 |
// Update is called when an existing node has been changed. Here, we use a
|
| 92 |
// DELETE then an INSERT rather than an UPDATE. The reason is that a node
|
| 93 |
// created before this module was installed won't already have a rating
|
| 94 |
// saved so there would be nothing to update.
|
| 95 |
case 'update':
|
| 96 |
db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
|
| 97 |
db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
|
| 98 |
break;
|
| 99 |
|
| 100 |
// Delete is called whn the node is being deleted, it gives us a chance
|
| 101 |
// to delete the rating too.
|
| 102 |
case 'delete':
|
| 103 |
db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
|
| 104 |
break;
|
| 105 |
|
| 106 |
// Finally, we need to take care of displaying our rating when the node is
|
| 107 |
// viewed. This operation is called after the node has already been prepared
|
| 108 |
// into HTML and filtered as necessary, so we know we are dealing with an
|
| 109 |
// HTML teaser and body. We will inject our additional information at the front
|
| 110 |
// of the node copy.
|
| 111 |
//
|
| 112 |
// Using nodeapi('view') is more appropriate than using a filter here, because
|
| 113 |
// filters transform user-supplied content, whereas we are extending it with
|
| 114 |
// additional information.
|
| 115 |
case 'view':
|
| 116 |
$node->content['nodeapi_example'] = array(
|
| 117 |
'#value' => theme('nodeapi_example_rating', $node->nodeapi_example_rating),
|
| 118 |
'#weight' => -1,
|
| 119 |
);
|
| 120 |
break;
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Implementation of hook_theme().
|
| 126 |
*
|
| 127 |
* This lets us tell Drupal about our theme functions and their arguments.
|
| 128 |
*/
|
| 129 |
function nodeapi_example_theme() {
|
| 130 |
return array(
|
| 131 |
'nodeapi_example_rating' => array(
|
| 132 |
'arguments' => array('rating'),
|
| 133 |
),
|
| 134 |
);
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* A custom theme function.
|
| 139 |
*
|
| 140 |
* By using this function to format our rating, themes can override this presentation
|
| 141 |
* if they wish; for example, they could provide a star graphic for the rating. We
|
| 142 |
* also wrap the default presentation in a CSS class that is prefixed by the module
|
| 143 |
* name. This way, style sheets can modify the output without requiring theme code.
|
| 144 |
*/
|
| 145 |
function theme_nodeapi_example_rating($rating) {
|
| 146 |
$options = array(
|
| 147 |
0 => t('Unrated'),
|
| 148 |
1 => t('Poor'),
|
| 149 |
2 => t('Needs improvement'),
|
| 150 |
3 => t('Acceptable'),
|
| 151 |
4 => t('Good'),
|
| 152 |
5 => t('Excellent'));
|
| 153 |
$output = '<div class="nodeapi_example_rating">';
|
| 154 |
$output .= t('Rating: %rating', array('%rating' => $options[(int) $rating]));
|
| 155 |
$output .= '</div>';
|
| 156 |
return $output;
|
| 157 |
}
|
| 158 |
|