| 1 |
<?php
|
| 2 |
/* $Id$ */
|
| 3 |
/**
|
| 4 |
* whatsrelated.module
|
| 5 |
*
|
| 6 |
* Adds a new node type called "Whatsrelated link", which contains link details
|
| 7 |
* and a reference to an existing node ID
|
| 8 |
*
|
| 9 |
* Based on node-example.module
|
| 10 |
*
|
| 11 |
* Database definition:
|
| 12 |
* @code
|
| 13 |
* CREATE TABLE whatsrelated (
|
| 14 |
* nid int(10) unsigned NOT NULL default '0',
|
| 15 |
* relatedto int(10) unsigned NOT NULL default '0',
|
| 16 |
* linkaddress varchar(255) NOT NULL default '',
|
| 17 |
* PRIMARY KEY (nid)
|
| 18 |
* )
|
| 19 |
* @endcode
|
| 20 |
*/
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_help().
|
| 24 |
*
|
| 25 |
* Throughout Drupal, hook_help() is used to display help text at the top of
|
| 26 |
* pages. Some other parts of Drupal pages get explanatory text from these hooks
|
| 27 |
* as well. We use it here to provide a description of the module on the
|
| 28 |
* module administration page.
|
| 29 |
*/
|
| 30 |
function whatsrelated_help($section) {
|
| 31 |
switch ($section) {
|
| 32 |
case 'admin/modules#description':
|
| 33 |
// This description is shown in the listing at admin/modules.
|
| 34 |
return t('A module to create links and assign them as related to nodes.');
|
| 35 |
case 'node/add#whatsrelated':
|
| 36 |
// This description shows up when users click "create content."
|
| 37 |
return t('You must add a node ID as well as the link title and destination. Make sure to include the http:// part.');
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_node_name().
|
| 43 |
*
|
| 44 |
* This is a required node hook. Since our module only defines one node
|
| 45 |
* type, we won't implement hook_node_types(), and our hook_node_name()
|
| 46 |
* implementation simply returns the translated name of the node type.
|
| 47 |
*/
|
| 48 |
function whatsrelated_node_name($node) {
|
| 49 |
return t('Whatsrelated related link');
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Implementation of hook_access().
|
| 54 |
*
|
| 55 |
* Node modules may implement node_access() to determine the operations
|
| 56 |
* users may perform on nodes. This example uses a very common access pattern.
|
| 57 |
*/
|
| 58 |
function whatsrelated_access($op, $node) {
|
| 59 |
global $user;
|
| 60 |
|
| 61 |
if ($op == 'create') {
|
| 62 |
// Only users with permission to do so may create this node type.
|
| 63 |
return user_access('create whatsrelated links');
|
| 64 |
}
|
| 65 |
|
| 66 |
// Users who create a node may edit or delete it later, assuming they have the
|
| 67 |
// necessary permissions.
|
| 68 |
if ($op == 'update' || $op == 'delete') {
|
| 69 |
if (user_access('edit own whatsrelated links') && ($user->uid == $node->uid)) {
|
| 70 |
return TRUE;
|
| 71 |
}
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Implementation of hook_perm().
|
| 77 |
*
|
| 78 |
* Since we are limiting the ability to create new nodes to certain users,
|
| 79 |
* we need to define what those permissions are here. We also define a permission
|
| 80 |
* to allow users to edit the nodes they created.
|
| 81 |
*/
|
| 82 |
function whatsrelated_perm() {
|
| 83 |
return array('create whatsrelated links', 'edit own whatsrelated links');
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_link().
|
| 88 |
*
|
| 89 |
* This is implemented so that an edit link is displayed for users who have
|
| 90 |
* the rights to edit a node.
|
| 91 |
*/
|
| 92 |
function whatsrelated_link($type, $node = 0, $main) {
|
| 93 |
$links = array();
|
| 94 |
|
| 95 |
if ($type == 'node' && $node->type == 'whatsrelated') {
|
| 96 |
// Don't display a redundant edit link if they are node administrators.
|
| 97 |
if (whatsrelated_access('update', $node) && !user_access('administer nodes')) {
|
| 98 |
$links[] = l(t('edit this whatsrelated link'), "node/$node->nid/edit");
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
return $links;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Implementation of hook_menu().
|
| 107 |
*
|
| 108 |
* In order for users to be able to add nodes of their own, we need to
|
| 109 |
* give them a link to the node composition form here.
|
| 110 |
*/
|
| 111 |
function whatsrelated_menu($may_cache) {
|
| 112 |
$items = array();
|
| 113 |
|
| 114 |
if ($may_cache) {
|
| 115 |
$items[] = array('path' => 'node/add/whatsrelated', 'title' => t('whatsrelated link'),
|
| 116 |
'access' => user_access('create whatsrelated links'));
|
| 117 |
}
|
| 118 |
|
| 119 |
return $items;
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Implementation of hook_form().
|
| 124 |
*
|
| 125 |
* Now it's time to describe the form for collecting the information
|
| 126 |
* specific to this node type. This hook requires us to return some HTML
|
| 127 |
* that will be later placed inside the form.
|
| 128 |
*/
|
| 129 |
function whatsrelated_form(&$node) {
|
| 130 |
$output = '';
|
| 131 |
|
| 132 |
// In order to be able to attach taxonomy terms to this node, we need
|
| 133 |
// to display the appropriate form elements.
|
| 134 |
if (function_exists('taxonomy_node_form')) {
|
| 135 |
$output .= implode('', taxonomy_node_form('node_example', $node));
|
| 136 |
}
|
| 137 |
|
| 138 |
// Now we define the form elements specific to our node type.
|
| 139 |
|
| 140 |
$output .= form_textfield(t('Listed under which article number?'), 'relatedto', $node->relatedto, 10, 10);
|
| 141 |
$output .= form_textfield(t('Link address'), 'linkaddress', $node->linkaddress, 60, 128);
|
| 142 |
$output .= form_textarea(t('Description'), 'body', $node->body, 60, 20);
|
| 143 |
$output .= filter_form('format', $node->format);
|
| 144 |
|
| 145 |
return $output;
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Implementation of hook_validate().
|
| 150 |
*
|
| 151 |
* Our "quantity" field requires a number to be entered. This hook lets
|
| 152 |
* us ensure that the user entered an appropriate value before we try
|
| 153 |
* inserting anything into the database.
|
| 154 |
*
|
| 155 |
* Errors should be signaled with form_set_error().
|
| 156 |
*/
|
| 157 |
function whatsrelated_validate(&$node) {
|
| 158 |
if ($node->relatedto) {
|
| 159 |
if (!is_numeric($node->relatedto)) {
|
| 160 |
form_set_error('relatedto', t('The node ID must be a number.'));
|
| 161 |
}
|
| 162 |
}
|
| 163 |
else {
|
| 164 |
// Let an empty field mean "zero."
|
| 165 |
$node->relatedto = 0;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Implementation of hook_insert().
|
| 171 |
*
|
| 172 |
* As a new node is being inserted into the database, we need to do our own
|
| 173 |
* database inserts.
|
| 174 |
*/
|
| 175 |
function whatsrelated_insert($node) {
|
| 176 |
db_query("INSERT INTO {whatsrelated} (nid, relatedto, linkaddress) VALUES (%d, %d, '%s')", $node->nid, $node->relatedto, $node->linkaddress);
|
| 177 |
}
|
| 178 |
|
| 179 |
/**
|
| 180 |
* Implementation of hook_update().
|
| 181 |
*
|
| 182 |
* As an existing node is being updated in the database, we need to do our own
|
| 183 |
* database updates.
|
| 184 |
*/
|
| 185 |
function whatsrelated_update($node) {
|
| 186 |
db_query("UPDATE {whatsrelated} SET relatedto = %d, linkaddress = '%s' WHERE nid = %d", $node->relatedto, $node->linkaddress, $node->nid);
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Implementation of hook_delete().
|
| 191 |
*
|
| 192 |
* When a node is deleted, we need to clean up related tables.
|
| 193 |
*/
|
| 194 |
function whatsrelated_delete($node) {
|
| 195 |
db_query('DELETE FROM {whatsrelated} WHERE nid = %d', $node->nid);
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* Implementation of hook_load().
|
| 200 |
*
|
| 201 |
* Now that we've defined how to manage the node data in the database, we
|
| 202 |
* need to tell Drupal how to get the node back out. This hook is called
|
| 203 |
* every time a node is loaded, and allows us to do some loading of our own.
|
| 204 |
*/
|
| 205 |
function whatsrelated_load($node) {
|
| 206 |
$additions = db_fetch_object(db_query('SELECT relatedto, linkaddress FROM {whatsrelated} WHERE nid = %d', $node->nid));
|
| 207 |
return $additions;
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Implementation of hook_view().
|
| 212 |
*
|
| 213 |
* This is a typical implementation that simply runs the node text through
|
| 214 |
* the output filters.
|
| 215 |
*/
|
| 216 |
function whatsrelated_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 217 |
$showlink = theme('whatsrelated_showlink', $node);
|
| 218 |
$node->body = $showlink . $node->body;;
|
| 219 |
$node = node_prepare($node, $teaser);
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* A custom theme function.
|
| 224 |
*
|
| 225 |
* By using this function to format our node-specific information, themes
|
| 226 |
* can override this presentation if they wish. We also wrap the default
|
| 227 |
* presentation in a CSS class that is prefixed by the module name. This
|
| 228 |
* way, style sheets can modify the output without requiring theme code.
|
| 229 |
*/
|
| 230 |
function theme_whatsrelated_showlink($node) {
|
| 231 |
$output = '<div class="whatsrelated_showlink">';
|
| 232 |
$output .= t('<a href=%linkaddress>%linkname</a>: ', array('%linkaddress' => "\"" . $node->linkaddress . "\"", '%linkname' => $node->title));
|
| 233 |
$output .= '</div>';
|
| 234 |
return $output;
|
| 235 |
}
|
| 236 |
|
| 237 |
?>
|