By rubenski on
Hi,
I have a taxonomy with a number of articles under each term and I'd like to alter the behavior of taxonomy term pages (/taxonomy/term/%term_id).
I have overridden hook_taxonomy_term_view_alter in my module, hoping to get access to the array of nodes that is printed on the term page.
/**
* override hook_taxonomy_term_view_alter
*/
function mymodule_taxonomy_term_view_alter(&$build){
Logger::log("viewing taxonomy term");
}
However, no log message shows up when I view a taxonomy term page. Any idea why this method isn't firing? Thanks.
Comments
In what way do you want to
In what way do you want to alter the page displayed? I ask because there are several approaches, both views and panels for example are capable of overriding the path but may not appropriate based on your need.
Hi Nevets, I'd like to
Hi Nevets,
I'd like to select nodes from (a configurable number of) subcategories when on a term page. I don't want to rely on other modules like views.
basically what I need to do in my method is...
- find out what term page I am on
- find out if there are subterms present in the vocabulary under this term
- get nodes from the database for these subterms
- add nodes found to the array for display
Views would be a good choice
Views would be a good choice since it already can handle depth (so can the default path but not without an extra argument).
I'm afraid I don't understand
I'm afraid I don't understand what you mean, Nevets, and I don't want to rely on views at this time.
I am at the point where I can create and maintain a menu that is based on a taxonomy (like the taxonomy_menu module) and now I want to allow the user to configure the number of sublevels to show on a taxonomy term page (taxonomy/term/$term_id).
I was expecting that hook_taxonomy_term_view_alter(&$build) would enable me to hook into the proces that creates the array of nodes that are displayed on a term page, but it doesn't fire when I'm on a term page.
Is there any way to hook into the process that generates the node list for a term page?
Almost there!
I don't know when hook_taxonomy_term_view_alter runs, so I've abandoned that..
Right now I am using hook_page_alter to get the extra nodes I need on the taxonomy term page. Here's the code.
All I need now is to add the extra nodes I have in $nodes to the $page, but how? The $page array is structured in a way I didn't expect. The actual nodes are nested pretty deeply like this. The nodes in the $page array start at [n], for example [15], and the nodes I have in my $nodes array are objects that start at "[#object] => stdClass Object". How to bridge this gap? There must be a more elegant way to do this..
Might try
http://drupal.org/project/devel
It will help you drill down what exactly you want to override and how, grate for theming and module development.
http://drupal.org/node/190815
I'm guessing taxonomy-term.tpl.php means it's a new template file you can drop into your theme directory like most other tpl.php files.
Hope it helps.
Solved
I solved this by using the hook_query_alter.
I am taking the number of sublevels I want to show nodes from from the config I built myself. Then, with that value I am overriding the query that is used by drupal to fetch nodes on a term page.
Works like a charm. The user can select the number of sublevels he wants to display nodes from on a term page and the query is dynamically altered to include nodes from those sublevels.
Next step is to set conditional child levels like "when term page has less than 5 nodes, fetch 5 most recent nodes from subterm pages", etc.