Similar to the 'view' option in the _nodeapi hook, the attached small patch adds a 'view' option to the _taxonomy hook, enabling modules to add to the display when a taxonomy is being viewed.

While the existing _taxonomy hook responds to insert, update, and delete actions, it has no way to interact with taxonomy display. This is a significant limitation, as there are numerous cases in which responding to a taxonomy display might be needed.

Specifically, the current term display is limited to nodes, being composed of the following call:

      $output = taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE));

This patch simply puts a hook call before the node call, allowing insertion of prior content:

      $output = implode('', module_invoke_all('taxonomy', 'view', 'term', $tids));
      $output .= taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE));

An example of the patch in action:

function test_taxonomy  ($op, $type, $object) {
  switch($op) {
    case 'view':
      if (($type == 'term') && is_array($object) && (count($object) == 1)) {
        $count = taxonomy_term_count_nodes($object[0]);
        switch ($count) {
          case 0:
            $message = t('Sorry, there are no postings yet in this category.');
            break;
          case 1:
            $message = t('There is one posting in this category.');
            break;
          default:
            $message = t('There are %count postings in this category.', array('%count' => $count));
            break;
        }
        return '<p>' . $message . '</p>';
      }
      break;
  }
}

function test_node_name() {
  return("test");
}

When enabled with the proposed patch applied, this test.module will add text displayed between the page title and the node display of a taxonomy term. Only if there is a single term being viewed (count($object) == 1), the hook tests the number of nodes in the term and displays different messages for none, one, or more than one. Incidentally, I've taken this example from an identified need for messaging on empty terms, see issue at http://drupal.org/node/view/10279.

One current need for this functionality is the taxonomy_context module, which presents the description for a term being viewed and for its child terms. For lack of an available hook, in the 4.4 version this module used drupal_set_message() to render content between the page title and the node listings for a taxonomy. This approach led to problems, e.g., theme-specific ways of presenting messages. Having a hook available would be a much better solution. Taxonomy_context provides significant functionality and has a significant user base; this small addition to the taxonomy.module would enable seamless display in Drupal 4.5.

Other contexts in which adding to term information would be desirable include:

  • Presenting additional information about the taxonomy, e.g., number of nodes.
  • Presenting external information linked to a taxonomy. For example, I'm working on a module that will make statistical calculations on data linked to terms (specifically, for agricultural products, the average import distance). A 'view' option in the taxonomy hook would allow me to look up and present information from these other sources on the term being viewed.

Comments

mhutch’s picture

Please get this in for 4.5, as it would make taxonomy_context much more usable. It's a really useful module.

nedjo’s picture

Any issues with this proposal? While I know this comes after the code freeze, it is a very small change (one new line of code) with significant advantages--in the short term, enabling taxonomy_context to work (display of description of current term plus subterms, a key functionality for many applications)--hence, improving Drupal usability. There is little or no potential for interference with other code, and so no risk to introducing this change during the freeze. It would require additional documentation as it introduces a new hook; I am happy to take on this work.

On a personal note, while I have contributed several modules, a theme, documentation etc., I have yet to have even a very minor core patch approved (ironically, perhaps, a spur to my writing documentation on "Contributing to Drupal"). The relative difficulty of getting changes in is for me a discouragement to Drupal development, as it makes my own use of and module contribution to the software difficult.

nedjo’s picture

This patch would also be useful for term_statistics, a module I've recently drafted and will release for Drupal 4.5. Term_statistics adds statistics for categories, parallel to (and based on) the node statistics support in the core statistics module. This patch would enable the display of a "reads" count when a term is viewed, parallel to the "reads" optionally displayed for nodes by the statistics module.

Bèr Kessels’s picture

http://drupal.org/node/5162 might be usefull for reference.

killes@www.drop.org’s picture

Doesn't apply anymore.

moshe weitzman’s picture

I wonder if a Contrib module could now use the new block regions functionality (coming in 4.7) to inject this description. hook_help is another option (use a $stat like 'page description') and format is as you wish.

bdragon’s picture

Version: x.y.z » 5.x-dev
Status: Active » Fixed

This can now be done with Drupal 5's hook_link / hook_link_alter.

Anonymous’s picture

Status: Fixed » Closed (fixed)
olivo.marco’s picture

Category: feature » task

After talking with the creator of this patch and after needing such a patch for one of my projects, I would like to ask for this patch to be re-opened and re-considered once again for inclusion in Drupal core.
In fact, the motivation for which it was closed (This can now be done with Drupal 5's hook_link / hook_link_alter) does not seem to apply:

  1. hook_link can be applied to nodes and comments (from the API page)
  2. hook_link_alter does not seem the way to do that

Moreover, this patch is useful in some cases and it patches a probably faulty design due to the fact that taxonomies cannot be styled from a module when being viewed, but only at theme level (see: http://drupal.org/node/131039)

If I am wrong in something please correct me.

Thanks. Bye,
Marco Olivo

robloach’s picture

Version: 5.x-dev » 7.x-dev
Status: Closed (fixed) » Active

Re-opening, as a 'view' $op for hook_taxonomy would be very helpful. Doesn't really seem like using hook_link is a nice solution to this. Nor does handing it over to the themeing layer. What if we want a category browser to appear on top of the taxonomy that's being displayed? What if we want to do this without using Views?

The 'view' option for hook_taxonomy is the solution here.

robloach’s picture

Status: Active » Needs review
StatusFileSize
new1.09 KB

The demonstration by nedjo still applies:

function mymodule_taxonomy($op, $type, $object = NULL) {
  switch($op) {
    case 'view':
      if (($type == 'term') && is_array($object) && (count($object) == 1)) {
        $count = taxonomy_term_count_nodes($object[0]);
        switch ($count) {
          case 0:
            $message = t('Sorry, there are no postings yet in this category.');
            break;
          case 1:
            $message = t('There is one posting in this category.');
            break;
          default:
            $message = t('There are %count postings in this category.', array('%count' => $count));
            break;
        }
        return '<p>' . $message . '</p>';
      }
      break;
  }
}
robin monks’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.13 KB

Reroll against HEAD, no functionality changes. Everything looks OK and the taxonomy simpletest ran fine after applying the patch.

RTBC.

Robin

moshe weitzman’s picture

So this is a way to prepend to the taxonomy page? Seems a bit of an anachronism with Views and all. Why would you use a taxonomy listing provided by core? I'm not opposed to this, just a bit uninspired.

catch’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

I wish I'd seen this four weeks ago then I wouldn't have opened a new issue for #306224: EOL Taxonomy sprint: add proper taxonomy term hooks. However that new issue moves taxonomy terms into first class drupal objects - which lets you do a lot more than append stuff to the taxonomy listing page (and removes $op as well), so I'm going to mark this older one as duplicate.