4

I have a free tagging taxonomy term field. How can I make it so on the node page the term only links to a term page if there are multiple nodes with the same term?

Otherwise this link and page are pointless, and are bad UX as the user will be expecting to see other similar nodes. So if there is only 1 node with a term then I would like the term to not be a link.

UPDATE ive tried creating a custom module called taxonomy_link_my.

In my taxonomy_link_my.info

name = Taxonomy link my 
description = Stop taxonomy term being a link if no other nodes are tagged with the term.
core = 7.x

In my taxonomy_link_my.module (topic is the vocabulary name).

<?php

/**
 * Implements hook_node_view
 */
function taxonomy_link_my_node_view($node, $view_mode, $langcode) {
  if (isset($node->content['topic'])) {
    foreach ($node->content['topic']['#items'] as $delta => $item) {
       if (count(taxonomy_select_nodes($item['taxonomy_term']->tid)) < 2) {
         $title = $node->content['topic'][$delta]['#title'];
         $node->content['topic'][$delta] = array();
         $node->content['topic'][$delta]['#markup'] = $title;
       }
    }
  }
}

However it seems to have no effect.

  • To get this work, you should replace ['topic'] to ['your_field_mashine_name']. So if your taxonomy term field mashine name is field_topic, then just replace topic to field_topic. – Jack-PL Nov 11 '13 at 16:39
  • and do not forget to clear the cache :) – Jack-PL Nov 11 '13 at 16:52
  • I think the machine name of vocabulery is topic. When I edit the vocbulery the url is: /admin/structure/taxonomy/topic/edit – Evanss Nov 11 '13 at 16:52
  • Ive tried both topic and field_topic and neither do anything. – Evanss Nov 11 '13 at 16:52
  • No, it shouldn't be a vocabulary name. Go to admin/structure/types/manage/[your content type]/fields there you can see what your term reference field name is. – Jack-PL Nov 11 '13 at 16:55
0
+50

It can be achieved with the help of hook_node_view().

Assuming your taxonomy term field is "field_tags":

/**
 * Implements hook_node_view
 */
function YOURMODULE_node_view($node, $view_mode, $langcode) {
  if (isset($node->content['field_tags'])) {
    foreach ($node->content['field_tags']['#items'] as $delta => $item) {
       if (count(taxonomy_select_nodes($item['taxonomy_term']->tid)) < 2) {
         $title = $node->content['field_tags'][$delta]['#title'];
         $node->content['field_tags'][$delta] = array();
         $node->content['field_tags'][$delta]['#markup'] = $title;
       }
    }
  }
}
  • The code doesnt seem to do anything. Ive updated my question with my implementation. – Evanss Nov 11 '13 at 14:53
  • This code works exactly as you expect. First it grabs the renderable data from the field, then checks all terms associated with the viewed content. If some of these terms has less than 2 nodes associated with, then the renderable data of this term is beeing reduced. Only the #markup remains, so the term name is being displayed, but without the link. – Jack-PL Nov 11 '13 at 16:35
  • Its probably my implementation thats wrong then, but its not doing anything on my site. Ive updated by question with my attempt. – Evanss Nov 11 '13 at 16:43
1

Looks possible implementing hook_entity_info_alter()

Modules may implement this hook to alter the information that defines an entity. All properties that are available in hook_entity_info() can be altered here.

Here is some sample code

function mymodule_entity_info_alter(&$entity_info) {
  $entity_info['taxonomy_term']['uri callback'] = 'mymodule_taxonomy_term_uri';
}

/**
* Entity uri callback for taxonomy terms. Add special exception to redirect users away
* from taxonomy term pages to the associated program node page.
*/
function mymodule_taxonomy_term_uri($term) {
  if (count(taxonomy_select_nodes($term->tid)) > 1) {
    return array(
      'path' => 'taxonomy/term/' . $term->tid,
    );
  }
  else {
    return array(
      'path' => '',
    );
  }
}

You need to make some tweaks to get term id by term name.I have not checked about returning the path if node count is 1.

  • Does your approach stop the term being a link altogether when only 1 node is tagged with the term? Or does it change the path url of the link? I need to remove the link altogether. Ive updated my question to make it clearer. – Evanss Oct 2 '13 at 11:04
  • I just checked it is not, I'll check if I can make it work – arpitr Oct 2 '13 at 11:37
  • Have you had any progress on updating your answer? – Evanss Nov 11 '13 at 12:17
0

I suppose it can be achieved with Views. You can can get node count which tagged with that term and if it is below 2 you can rewrite results and make a text without link. I am nearly sure those are possible with Views but to test, make sure and explain step by step can take several pages/hours. I suggest you to make searches about how to do. You may need additional Views modules and other things and hard code somethings with PHP.

0

I think the best way would be to create a custom formatter for the taxonomy field. Take a look at how the taxonomy module itself does it, in the function taxonomy_field_formatter_view. In there they have both the link formatter and the plain formatter.

In your custom formatter, you'll check whether there are other nodes that have each term (you'll use taxonomy_select_nodes() or something like this, depending on how your taxonomy field is set up). If there are no terms, you'll format it just as text, using the taxonomy module's plain formatter as an example. If it has nodes, you'll format it like the taxonomy module's link formatter.

  • I know its down to my limitations with PHP but i cant make sense of those links. Could you provide some example code? – Evanss Nov 11 '13 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.