? custom_links_117511_0.patch
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 16 Jan 2009 01:25:47 -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('No custom links have been defined.'), '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,55 +50,74 @@ function custom_links_form(&$form_state,
'#default_value' => $lid ? $link->link_key : NULL
);
- $form['link']['title'] = array(
- '#type' => 'textfield',
- '#title' => t('Title'),
- '#required' => TRUE,
- '#description' => t("The visible text of the link seen by the user."),
- '#default_value' => $lid ? $link->title : 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']['check_html'] = array(
- '#type' => 'checkbox',
- '#title' => t('Title uses HTML'),
- '#return_value' => 1,
- '#default_value' => $lid ? $link->check_html : 0
+ $form['link_path'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Link path'),
+ '#description' => t("The Drupal path for the link. The first field is the path, the second a HTML anchor, and the third a query string, e.g.: !sample_url)", array('!sample_url' => $base_url .'/ a/path # an_anchor ? query=1&string=2')),
);
- $form['link']['path'] = array(
+ $form['link_path']['path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
- '#description' => t("The Drupal path for the link. (!sample_url)", array('!sample_url' => $base_url .'/node/1#comment')),
+ '#size' => 10,
+ '#prefix' => '
',
+ '#suffix' => '#',
'#default_value' => $lid ? $link->path : NULL
);
+ $form['link_path']['fragment'] = array(
+ '#type' => 'textfield',
+ '#size' => 10,
+ '#suffix' => '?',
+ '#default_value' => $lid ? $link->fragment : NULL,
+ );
- $form['link']['querystring'] = array(
+ $form['link_path']['querystring'] = array(
'#type' => 'textfield',
- '#title' => t('Querystring'),
- '#description' => t("The optional querystring for the link. (!sample_url)", array('!sample_url' => $base_url .'/article?id=1')),
- '#default_value' => $lid ? $link->query : NULL
+ '#suffix' => '
',
+ '#size' => 10,
+ '#default_value' => $lid ? $link->query : NULL,
);
- $form['link']['fragment'] = array(
+ $form['link_path']['title'] = array(
'#type' => 'textfield',
- '#title' => t('Anchor'),
- '#description' => t("The optional HTML anchor for the link. (!sample_url)", array('!sample_url' => $base_url .'/node/1#comment')),
- '#default_value' => $lid ? $link->fragment : NULL
+ '#title' => t('Title'),
+ '#required' => TRUE,
+ '#description' => t("The visible text of the link seen by the user."),
+ '#default_value' => $lid ? $link->title : NULL
);
- $form['help'] = array(
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#title' => t('Placeholder tokens'),
- '#description' => t("The following placeholder tokens can be used in paths, titles, querystrings, and anchors. they will be replaced with the appropriate values."),
+ $form['link_path']['check_html'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Title uses HTML'),
+ '#return_value' => 1,
+ '#default_value' => $lid ? $link->check_html : 0
);
if (module_exists('token')) {
- $form['help']['tokens'] = array(
+ $form['link_path']['help'] = array(
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#title' => t('Placeholder tokens'),
+ '#description' => t("The following placeholder tokens can be used in paths, titles, querystrings, and anchors. they will be replaced with the appropriate values."),
+ );
+
+ $form['link_path']['help']['tokens'] = array(
'#value' => theme('token_help', 'node'),
);
}
+
else {
$form['help']['#description'] = t("To use dynamic placeholder tokens in your paths and titles (the ID or title of the current node, for example), download and install the Token module from Drupal.org.", array('@token' => 'http://www.drupal.org/project/token'));
$form['help']['#collapsible'] = FALSE;
@@ -110,16 +130,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 16 Jan 2009 01:25:47 -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 16 Jan 2009 01:25:47 -0000
@@ -1,6 +1,18 @@
custom_links_teaser = $teaser;
+ }
+}
+/**
+ * Implementation of hook_link_alter().
+ */
+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->custom_links_teaser && $link->display != CUSTOM_LINK_DISPLAY_NODE_TEASER) {
+ continue;
+ }
+ elseif (!$node->custom_links_teaser && $link->display != CUSTOM_LINK_DISPLAY_NODE_FULL) {
+ continue;
+ }
+ }
+ _custom_links_customize_link($links, $link, $node);
}
}
@@ -89,7 +121,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,30 +138,36 @@ 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;
@@ -139,7 +185,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]);
+ }
}
/**