Index: relatedlinks.module
===================================================================
--- relatedlinks.module (revision 1664)
+++ relatedlinks.module (working copy)
@@ -3,11 +3,12 @@
/**
* @file
- * Related links are defined in 2 ways:
- * 1) For certain content types, HTML links are automatically discovered and included.
- * 2) The author may manually add HTML links that will appear at the top of the
- * 'Related links' block.
- * 3) An optional block can also list related taxonomy nodes as related links.
+ * Related links are defined in 3 ways:
+ * 1) Parsed Links: For certain content types, HTML links are
+ * automatically discovered and included.
+ * 2) Manual Links: The author may manually add HTML links that will
+ * appear at the top of the 'Related Links' block.
+ * 3) Taxonomy Links: Related taxonomy nodes can also be listed.
*
* When a node is viewed alone, a block is provided to authorized users that
* displays a complete list of links. If no links are defined, the block will
@@ -16,8 +17,9 @@
// Define mnemonics to indicate the types of links stored in the relatedlinks
// table [type field].
-define('RELATEDLINKS_PARSED', 1);
-define('RELATEDLINKS_MANUAL', 2);
+define('RELATEDLINKS_PARSED', 1);
+define('RELATEDLINKS_MANUAL', 2);
+define('RELATEDLINKS_TAXONOMY', 3);
/**
* Implementation of hook_help().
@@ -27,7 +29,21 @@
case 'admin/modules#description':
return t('Provides a block with related links.');
case 'admin/help#relatedlinks':
- return t('Related links are defined in 2 ways: in certain input formats, HTML links are automatically discovered and included; when publishing certain content types, the author may manually add HTML links that will appear at the top of the Related links block. When a node is viewed alone, a block is provided to authorized users that displays a complete list of links. If no links are defined, the block will disappear.');
+ return t('Related links are defined in 3 ways:
+
+ - Parsed Links
+ - For certain content types, HTML links
+ are automatically discovered and included.
+ - Manual Links
+ - The author may manually add HTML links
+ that will appear at the top of the "Related Links"
+ block.
+ - Taxonomy Links
+ - Related taxonomy nodes can also be listed.
+
+ When a node is viewed alone, a block is provided to authorized
+ users that displays a complete list of links. If no links are
+ defined, the block will disappear.');
}
}
@@ -67,7 +83,9 @@
*
*/
function relatedlinks_nodeapi(&$node, $op, $arg) {
- if ((user_access('add related links') || user_access('administer related links') && in_array($node->type, variable_get('relatedlinks_node_types', array())))) {
+ if (user_access('add related links') ||
+ user_access('administer related links') &&
+ in_array($node->type, variable_get('relatedlinks_node_types', array()))) {
switch ($op) {
case 'load':
$links = _relatedlinks_get_links($node->nid, RELATEDLINKS_MANUAL);
@@ -112,7 +130,10 @@
* Implementation of hook_form_alter().
*/
function relatedlinks_form_alter($form_id, &$form) {
- if (user_access('add related links') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && in_array('manual', variable_get('relatedlinks_types', array('parsed')))) {
+ if (user_access('add related links') &&
+ isset($form['type']) &&
+ ($form['type']['#value'] .'_node_form' == $form_id) &&
+ in_array('manual', variable_get('relatedlinks_types', array('parsed')))) {
$node = $form['#node'];
if (!in_array($node->type, variable_get('relatedlinks_node_types', array()))) {
@@ -145,8 +166,7 @@
*/
function relatedlinks_block($op = 'list', $delta = 0) {
if ($op == 'list') {
- $blocks[0]['info'] = t('Related links');
- $blocks[1]['info'] = t('Related taxonomy terms');
+ $blocks[0]['info'] = t('Related Links');
return $blocks;
}
if ($op == 'view') {
@@ -155,22 +175,36 @@
if ($node = node_load(arg(1))) {
switch ($delta) {
case 0:
- if (in_array($node->type, variable_get('relatedlinks_node_types', array()))) {
- // _relatedlinks_get_links also takes care of links filtering and validation.
+ // Only display the block if the current node type has related
+ // links enabled.
+ if (in_array(
+ $node->type,
+ variable_get('relatedlinks_node_types', array())
+ )) {
+
+ // Get links arrays from all sources.
$links = _relatedlinks_get_links($node->nid);
-
+ $terms = _relatedlinks_get_terms(
+ array_keys($node->taxonomy),
+ $node->nid
+ );
+
+ // If there are any links, display them.
+ if (!empty($links) || !empty($terms)) {
+ $block['subject'] = t('Related Links');
+ }
if (!empty($links)) {
- $block['subject'] = t('Related links');
$block['content'] = theme('relatedlinks', $links);
}
+ if (!empty($terms)) {
+ $block['content'] .= theme(
+ 'relatedlinks',
+ $terms,
+ 'Related Terms'
+ );
+ }
}
break;
- case 1:
- $links = _relatedlinks_get_terms(array_keys($node->taxonomy), $node->nid);
- if (!empty($links)) {
- $block['subject'] = t('Related terms');
- $block['content'] = theme('relatedlinks_terms', $links);
- }
}
}
}
@@ -181,18 +215,11 @@
/**
* Theme the relatedlinks block output.
*/
-function theme_relatedlinks($links = array()) {
- return theme('item_list', $links);
+function theme_relatedlinks($links = array(), $title = NULL) {
+ return theme('item_list', $links, $title);
}
/**
- * Theme the relatedlinks block output.
- */
-function theme_relatedlinks_terms($links = array()) {
- return theme('item_list', $links);
-}
-
-/**
* Menu Callback: relatedlinks module settings form. Not using hook_settings
* due to the checkboxes element.
*/
@@ -206,7 +233,7 @@
$form['relatedlinks']['relatedlinks_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node associations'),
- '#description' => t('Select the node types to associate with the related links module [parsed and manually added links]. This is not applicable to the taxonomy terms block.'),
+ '#description' => t('Select the node types to associate with the related links module [parsed and manually added links]. This is not applicable to the taxonomy terms listing.'),
'#options' => node_get_types(),
'#default_value' => variable_get('relatedlinks_node_types', array()),
);
@@ -214,11 +241,17 @@
'#type' => 'checkboxes',
'#title' => t('Link types'),
'#description' => t('Select the link types to enable.'),
- '#options' => array('parsed' => t('Parsed links'),
- 'manual' => t('Manually added links')),
+ '#options' => array(
+ 'parsed' => t('Parsed links'),
+ 'manual' => t('Manually added links'),
+ 'taxonomy' => t('Taxonomy terms'),
+ ),
'#default_value' => variable_get('relatedlinks_types', array('parsed')),
);
- $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save configuration'),
+ );
return drupal_get_form('_relatedlinks_settings_form', $form);
}
@@ -257,7 +290,8 @@
}
/**
- * Retrieve related links from the database.
+ * Retrieve related links from the database. This function also takes care
+ * of links filtering and validation.
*/
function _relatedlinks_get_links($nid, $type = NULL) {
if (!isset($type)) {
@@ -268,12 +302,13 @@
$result = db_query('SELECT link FROM {relatedlinks} WHERE nid = %d AND type = %d ORDER BY TYPE DESC', $nid, $type);
}
+ // Fetch the data into an array.
$links = array();
-
while ($link = db_fetch_array($result)) {
$links[] = filter_xss($link['link'], array('a'));
}
+ // Return it.
return $links;
}
@@ -281,17 +316,32 @@
* Retrieve related taxonomy terms.
*/
function _relatedlinks_get_terms($tids, $nid) {
- // The following query is likely to prove expensive when the numbers involved are large.
- // pgSQL compliant?
- $result = module_invoke('taxonomy', 'select_nodes', $tids, 'or', 0, FALSE, 'RAND()');
+ $links = array();
- $links = array();
- while ($node = db_fetch_array($result)) {
- // Exclude the current nid.
- if ($node['nid'] != $nid) {
- $links[] = l($node['title'], 'node/'. $node['nid']);
+ // Only get the terms if this link type is enabled.
+ if (in_array(
+ 'taxonomy',
+ variable_get('relatedlinks_types', array('parsed'))
+ )) {
+
+ // The following query is likely to prove expensive when the numbers
+ // involved are large.
+ // pgSQL compliant?
+ $result = module_invoke(
+ 'taxonomy', 'select_nodes', $tids, 'or', 0, FALSE, 'RAND()'
+ );
+
+ // Fetch the data into an array.
+ $link_number = 0;
+ while ($node = db_fetch_array($result)) {
+ // Exclude the current nid.
+ if ($node['nid'] != $nid) {
+ $links[$link_number] = l($node['title'], 'node/'. $node['nid']);
+ }
+ $link_number++;
}
}
+ // Return it.
return $links;
}