Add URLs for referencing NAT nodes by TID

smokris - February 7, 2009 - 06:10
Project:Node Auto Term [NAT]
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

I needed a way to easily construct URL references to the NAT node, given only the corresponding term's TID, so I patched `nat.module` as follows:

<?php
Index
: nat.module
===================================================================
---
nat.module    (revision 5014)
+++
nat.module    (working copy)
@@ -
57,10 +57,25 @@
    
'access arguments' => array('administer NAT configuration'),
    
'type' => MENU_LOCAL_TASK
  
);
$items['taxonomy/term/%/nat'] = array(
+   
'title' => 'NAT for term',
+   
'page callback' => 'nat_tid_to_node',
+   
'page arguments' => array(2),
+   
'access arguments' => array('access content'),
+   
'type' => MENU_CALLBACK,
+  );

   return
$items;
}

+function
nat_tid_to_node($tid)
+{
$nids = array_keys(nat_get_nids(array($tid), FALSE));
+  if (!empty(
$nids)) {
+   
drupal_goto("node/$nids[0]");
+  }
+}
+
/**
  * Implementation of hook_views_api().
  *
?>

(After adding this code you need to "Clear cached data" on `/admin/settings/performance` for it to work.)

#1

nonsie - February 10, 2009 - 21:36

+2 for this one. I've added one minor addition - if the term is not nat term redirect to term page.
Patch against 6.x-1.1-beta2 attached

AttachmentSize
nat.module_term_nat.patch 1.13 KB

#2

Zen - February 15, 2009 - 06:03
Status:needs review» needs work

Please spin off the URL block into a helper function - it can also be reused in hook_link_alter and possibly by other modules. Return FALSE if not a NAT node.

Cheers,
-K

#3

smokris - February 16, 2009 - 01:38
Status:needs work» needs review

I've attempted Zen's suggestion from #2, and modified hook_link_alter() to make use of it.

AttachmentSize
nat.module_term_nat-01.patch 1.45 KB

#4

smokris - February 16, 2009 - 01:42

(oops.. try this patch instead of the above.)

AttachmentSize
nat.module_term_nat-02.patch 1.55 KB

#5

Zen - February 16, 2009 - 06:21
Status:needs review» needs work
  • Shouldn't it be nat_goto_node() and nat_get_path_for_node()?
  • Avoid cuddling the 'else' on the same line.
  • Please add doxygen comments for both functions.
  • Please shove them both down - there should be a section for menu callbacks and another further down for other helper functions.

Cheers smokris :)

-K

#6

smokris - April 27, 2009 - 19:49
Version:6.x-1.1-beta2» 6.x-1.x-dev
Status:needs work» needs review

I've addressed your four issues in #5, and re-rolled this patch against the current 6.x-1.x-dev version.

AttachmentSize
nat.module_term_nat-03.patch 1.97 KB

#7

jbomb - May 26, 2009 - 16:52

Thanks smokris! i'm using the patch from #6 with a hook_menu_alter implementation as Zen described in #2.

All of my terms with NAT relationships always link to the NAT node rather than the taxonomy page. It's exactly what I needed.

#8

TKS - August 20, 2009 - 02:31

+1 for this feature.

The content-type-specific nat_link_alter option that's part of the current module is helpful, but I really need to have NAT-related terms always point to the associated node, regardless of where they appear. In a perfect world, this would be another checkbox under each content type at admin/settings/nat.

It seems like the patch from #6 has been through several rounds of revisions. Zen, is this something that's likely headed for the dev version or the beta-4 release? (I don't want to patch locally and then lose the functionality on the next module update.) Or would I be better off dropping this functionality into a custom module for now?

 
 

Drupal is a registered trademark of Dries Buytaert.