? .custom_links.admin.inc.swp
? .custom_links.module.swp
Index: custom_links.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/custom_links/custom_links.admin.inc,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 custom_links.admin.inc
--- custom_links.admin.inc	15 Jan 2009 21:22:37 -0000	1.1.2.2
+++ custom_links.admin.inc	15 Jan 2009 22:50:37 -0000
@@ -5,21 +5,22 @@
 function custom_links_page() {
   $links = _custom_links_load_all_links(TRUE);
 
-  $header = array(t('key'), t('link'), '');
+  $header = array(t('Key'), t('Link'), t('Action'), '');
 
   $rows = array();
   foreach ($links as $link) {
     $row = array();
     $row[] = $link->link_key;
-    $row[] =  $link->title;
-    $row[] =  l(t('edit'), 'admin/build/custom_links/' . $link->lid . '/edit') . ' ' . l(t('delete'), 'admin/build/custom_links/' . $link->lid . '/delete');
+    $row[] = $link->title;
+    $row[] = $link->action == CUSTOM_LINK_ACTION_ALTER ? t('Add/Replace') : t('Remove'); 
+    $row[] = l(t('Edit'), 'admin/build/custom_links/' . $link->lid . '/edit') . ' ' . l(t('Delete'), 'admin/build/custom_links/' . $link->lid . '/delete');
     $rows[] = $row;
   }
   if (count($rows) == 0) {
-    $rows[] = array(array('data' => t('No custom links have been defined.'), 'colspan' => 3));
+    $rows[] = array(array('data' => t('<em>No custom links have been defined.</em>'), 'colspan' => 4));
   }
 
-  $rows[] = array(array('data' => l(t('Add a new custom link'), 'admin/build/custom_links/add'), 'colspan' => 3));
+  $rows[] = array(array('data' => l(t('Add a new custom link'), 'admin/build/custom_links/add'), 'colspan' => 4));
 
 
   return theme('table', $header, $rows);
@@ -49,6 +50,17 @@ function custom_links_form(&$form_state,
     '#default_value' => $lid ? $link->link_key : NULL
   );
 
+  $form['link']['action'] = array(
+    '#type' => 'radios',
+    '#title' => t('Action'),
+    '#options' => array(
+      CUSTOM_LINK_ACTION_ALTER => t('Add/Replace existing link'),
+      CUSTOM_LINK_ACTION_REMOVE => t('Remove existing link'),
+    ),
+    '#description' => t("This controls the way this link will interact with the links from other modules. If you select Add/Replace this link will overwrite other module's links with the link key. If you select remove the links with the key will be removed when the conditions are met."),
+    '#default_value' => $lid ? $link->action : NULL
+  );
+
   $form['link']['title'] = array(
     '#type' => 'textfield',
     '#title' => t('Title'),
@@ -110,16 +122,16 @@ function custom_links_form(&$form_state,
   );
 
   $modes = array(
-    0 => t('In full node views'),
-    1 => t('In teaser node views'),
-    2 => t('In both teaser and full node views'),
-    3 => t('In a sidebar block'),
+    CUSTOM_LINK_DISPLAY_NODE_FULL   => t('In full node views'),
+    CUSTOM_LINK_DISPLAY_NODE_TEASER => t('In teaser node views'),
+    CUSTOM_LINK_DISPLAY_NODE_BOTH   => t('In both teaser and full node views'),
+    CUSTOM_LINK_DISPLAY_BLOCK       => t('In a sidebar block'),
   );
   $form['filters']['display'] = array(
     '#type' => 'select',
     '#title' => t('Display'),
     '#options' => $modes,
-    '#default_value' => $lid ? $link->display : 2,
+    '#default_value' => $lid ? $link->display : CUSTOM_LINK_DISPLAY_NODE_BOTH,
   );
 
   $options['*all*'] = t('All node types');
Index: custom_links.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/custom_links/custom_links.install,v
retrieving revision 1.2
diff -u -p -r1.2 custom_links.install
--- custom_links.install	27 Jan 2008 09:16:01 -0000	1.2
+++ custom_links.install	15 Jan 2009 22:50:37 -0000
@@ -8,6 +8,16 @@ function custom_links_install() {
   drupal_install_schema('custom_links');
 }
 
+/**
+ * Implementation of hook_uninstall().
+ */
+function custom_links_uninstall() {
+  drupal_uninstall_schema('custom_links');
+}
+
+/**
+ * Implementation of hook_schema().
+ */
 function custom_links_schema() {
   $schema['custom_link'] = array(
     'description' => t('Stores custom links to be added to nodes.'),
@@ -18,6 +28,13 @@ function custom_links_schema() {
         'not null' => TRUE,
         'description' => t('Unique identifier for the {custom_link}.'),
       ),
+      'action' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'size' => 'small',
+        'default' => 0,
+        'description' => t('An integer indicating what action should be taken for the link.'),
+      ),
       'link_key' => array(
         'type' => 'varchar',
         'length' => 64,
@@ -109,6 +126,21 @@ function custom_links_update_1() {
   return $ret;
 }
 
-function custom_links_uninstall() {
-  drupal_uninstall_schema('custom_links');
+/**
+ * Add an action field.
+ */
+function custom_links_update_6000() {
+  $ret = array();
+
+  $action_field = array(
+    'type' => 'int',
+    'not null' => TRUE,
+    'size' => 'small',
+    'default' => 0,
+    'initial' => 0,
+    'description' => t('An integer indicating what action should be taken for the link.'),
+  );
+  db_add_field($ret, 'custom_link', 'action', $action_field);
+
+  return $ret;
 }
Index: custom_links.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/custom_links/custom_links.module,v
retrieving revision 1.5.2.2
diff -u -p -r1.5.2.2 custom_links.module
--- custom_links.module	15 Jan 2009 21:22:37 -0000	1.5.2.2
+++ custom_links.module	15 Jan 2009 22:50:37 -0000
@@ -1,6 +1,18 @@
 <?php
 // $Id: custom_links.module,v 1.5.2.2 2009/01/15 21:22:37 eaton Exp $
 
+// Display link in full node views.
+define('CUSTOM_LINK_DISPLAY_NODE_FULL', 0);
+// Display link in teaser node views.
+define('CUSTOM_LINK_DISPLAY_NODE_TEASER', 1);
+// Display link in both full and teaser views.
+define('CUSTOM_LINK_DISPLAY_NODE_BOTH', 2);
+// Display link in custom block.
+define('CUSTOM_LINK_DISPLAY_BLOCK', 3);
+
+define('CUSTOM_LINK_ACTION_ALTER', 0);
+define('CUSTOM_LINK_ACTION_REMOVE', 1);
+
 /**
  * Implementation of hook_menu().
  */
@@ -62,11 +74,20 @@ function custom_links_theme() {
 }
 
 /**
- * Implementation of hook_link().
+ * Implementation of hook_link_alter().
  */
-function custom_links_link($type, $node = NULL, $teaser = FALSE) {
-  if ($type == 'node') {
-    return _custom_links_build_links($node, $teaser);
+function custom_links_link_alter(&$links, $node) {
+  foreach (_custom_links_load_all_links() as $link) {
+    // Check visibility.
+    if ($link->display != CUSTOM_LINK_DISPLAY_NODE_BOTH) {
+      if ($node->teaser && $link->display != CUSTOM_LINK_DISPLAY_NODE_TEASER) {
+        continue;
+      }
+      elseif (!$node->teaser && $link->display != CUSTOM_LINK_DISPLAY_NODE_FULL) {
+        continue;
+      }
+    }
+    _custom_links_customize_link($links, $link, $node);
   }
 }
 
@@ -89,7 +110,15 @@ function custom_links_block($op = 'list'
   }
   else if ($op == 'view' && arg(0) == 'node' && is_numeric(arg(1))) {
     $node = node_load(arg(1));
-    $links = _custom_links_build_links($node, FALSE, TRUE);
+    $links = array();
+    foreach (_custom_links_load_all_links() as $link) {
+      // Check visibility.
+      if ($link->display != CUSTOM_LINK_DISPLAY_BLOCK) {
+        continue;
+      }
+      _custom_links_customize_link($links, $link, $node);
+    }
+
     if (count($links)) {
       $block['subject'] = t('Links');
       $block['content'] = theme('custom_links_block', $links);
@@ -98,34 +127,40 @@ function custom_links_block($op = 'list'
   }
 }
 
-function _custom_links_build_links($node, $teaser = FALSE, $block = FALSE) {
-  $links = array();
-  $custom_links = _custom_links_load_all_links();
-
-  foreach ($custom_links as $link) {
-    if (($block && $link->display != 3) || (!$block && $link->display == 3)) {
-      continue;
-    }
-    if (($link->display == 0 && $teaser) || ($link->display == 1 && !$teaser)) {
-      continue;
-    }
-    if ($link->node_type && $node->type != $link->node_type) {
-      continue;
-    }
-    if ($link->viewer_perm && !user_access($link->viewer_perm)) {
-      continue;
-    }
-    if ($link->author_perm) {
-      $author = user_load(array('uid' => $node->uid));
-      if (!user_access($link->author_perm, $author)) {
-        continue;
-      }
+/**
+ * Customize a link.
+ *
+ * The caller is responsible for checking the link's display property before calling this.
+ *
+ * @param $links
+ *   Array of links to affect.
+ * @param $link
+ *   The custom link object to build.
+ * @param $node
+ */
+function _custom_links_customize_link(&$links, $link, $node) {
+  // Check type.
+  if ($link->node_type && ($node->type != $link->node_type)) {
+    return;
+  }
+
+  // Check permissions.
+  if ($link->viewer_perm && !user_access($link->viewer_perm)) {
+    return;
+  }
+  if ($link->author_perm) {
+    $author = user_load(array('uid' => $node->uid));
+    if (!user_access($link->author_perm, $author)) {
+      return;
     }
+  }
 
+  // Carry out the desired action. 
+  if ($link->action == CUSTOM_LINK_ACTION_ALTER) {
     $links[$link->link_key]['title'] = $link->title;
     if (!empty($link->path)) {
       $links[$link->link_key]['href'] = $link->path;
-    }
+      }
     if (!empty($link->query)) {
       $links[$link->link_key]['query'] = $link->query;
     }
@@ -139,7 +174,9 @@ function _custom_links_build_links($node
 
     $links[$link->link_key]['html'] = $link->check_html;
   }
-  return $links;
+  elseif ($link->action == CUSTOM_LINK_ACTION_REMOVE) {
+    unset($links[$link->link_key]);
+  }
 }
 
 /**
