Fixing duplicate taxonomy terms in a node view using hook_views_pre_render()

9 +
NonProfit's picture

SWEET

Sweet, this looks to be just what I need!

RedTop's picture

Could you help out?

I'm trying to get a list of distinct terms of all nodes of a certain content type. The terms link to their (customized) term pages, for example allowing a listing of all "articles" (content type) about "disability" (term).

Right now the list contains duplicates as the view simply lists the terms of every node that passes the filtercriteria (being content type=article and published=yes). I can't use taxonomy views as those do not allow you to filter content types.

Your snippet seems to do want I'm looking for, but it somehow filters out all terms (no listing at all).

My view has three fields:

Taxonomy: Vocabulary name [name_1], excluded - used for grouping.

Taxonomy: Term ID [tid], excluded - used for linking to the term page (need the [tid] replacement pattern)

Taxonomy: Term [name], displayed - link customized

I pasted your snippet in my site specific custom module, changed the view name and subsituted YOUR_FIELD_ with "name". (for example: $result->node_node_data_name_term_data_tid)

Do you have an idea what I may have done wrong or what a possible solution may be?

himerus's picture

double underscore...

Look closely at the example...

There is a double underscore ( __ ) between your field name and the last portion... Try that first, and let me know!

Jake Strawn
Drupal Rockstar

RedTop's picture

Wow! Thanks for the quick

Wow! Thanks for the quick reply! :)

I had noticed the double underscore but because that didn't seem to work I figured it was either a (highly consistent) typo or your placeholder was called YOUR_FIELD_ . :P

Nonetheless I tried again with a double underscore this time... but it didn't work. I also tried using the two other fields with a double underscore, just to see if that would change anything. When that didn't work I removed the two fields which are just in there for grouping and url construction. I wanted to see whether having more fields messed up the script somehow... no change. I also tried removing the first 'node_' as I have seen quite a few scripts using node_data_ ... but that obviously didn't change anything.

As you can tell I'm not a PHP coder, although your script does make sense to me. I still feel it has to do with picking the correct name pattern.

This is the query of the view:

SELECT node.nid AS nid,vocabulary.name AS vocabulary_name,term_data.tid AS term_data_tid,term_data.name AS term_data_name,term_data.vid AS term_data_vidFROM drupal_node node LEFT JOIN drupal_term_node term_node ON node.vid = term_node.vidLEFT JOIN drupal_term_data term_data ON term_node.tid = term_data.tidLEFT JOIN drupal_vocabulary vocabulary ON term_data.vid = vocabulary.vidWHERE (node.type in ('article')) AND (node.status <> 0)ORDER BY term_data_name ASC

I exported the view to a file: www.richardvankooten.nl/webspace/view.txt

Thanks again!

himerus's picture

Okay, a few things to check...

Okay, not sure... the view export, at a quick glance, looks fine...

Make sure in the upper portion of this function the line

if ($view->name == "your_view_name")

is replaced with:
if ($view->name == "articles_taxonomy")

and from what I can see of your example, you should be right, making sure that any references to node_node_data_YOUR_FIELD__term_data_name become node_node_data_name__term_data_name and node_node_data_YOUR_FIELD__term_data_tid become node_node_data_name__term_data_tid

That seems like that should be all the adjustments....
If you continue to have issues after those are checked... make sure your filters (node type & status) are actually returning true... that there are in fact article nodes that ARE published... (silly I know, but who knows!!)

If you continue to have trouble, I'll see if I can set up the exported view on a sandbox and play around for a minute. I will be pressed for time however between now & Friday when I leave for Drupalcon.

Good Luck!

Jake Strawn
Drupal Rockstar

himerus's picture

One other thing...

Also... another thing I like to do when I'm not sure things are running right... potentially right after the line:

name == "articles_taxonomy")

put something like

print_r('Just making sure my code is actually being ran over. if this appears, the problem must be further down...');

I do that a lot when developing something to just "make sure" that the code I expect to run is at least getting looked at...

Jake Strawn
Drupal Rockstar

RedTop's picture

Eureka! I did some more

Eureka!

I did some more debugging and played around with:

drupal_set_message('<pre>'. print_r($result->node_node_data_name__term_data_tid, TRUE) .'</pre>');

after

foreach($view->result AS $k => $result) {

As that didn't return any results I dropped all the

node_node_data_name__

prefixes and that did the trick. :)

Apparently

$result->term_data_tid

and

$view->result[$k]->term_data_name

is all we needed.

Thanks for getting me on track! :D

himerus's picture

Awesome!

Nice work... that was going to be my last thing was suggest dumping the $views varible out, and ensuring how the results were named...

Glad it works now!

Jake Strawn
Drupal Rockstar

RedTop's picture

Added to Drupal's documentation

http://drupal.org/node/770782

Hope you don't mind. :)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <h2> <h3> <h4> <h5> <h6> <img> <p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Twitter-style @usersnames are linked to their Twitter account pages.

More information about formatting options