Hello, this one is driving me crazy because every time I think I found the answer(google, google, google) I end up realizing the "solution" skip what I precisely need ... call it karma
Ok
I need to add custom classes to the link generated for terms.
I want to inject some classes (in that case some chosen ancestor ids to allow highlighting of the ancestor term when hovering over a descendant tagged term node ) in the generated <a> </a> of a term link.
My researches so far
First
I know how to have a handle in qef_zen_preprocess_node on $node_taxonomy_object (but I have a hard time knowing it's full structure besides what I found in code snippets, having a clear description on drupal docs of such objects and arrays would be VERY useful by the way), so I can inspect my terms and, for some terms I need to inject new classes in the link that is generated.
I understood that the taxonomy module extend the hook_link() in function taxonomy_link($type, $node = NULL) to allow the theming of ''taxonomy terms'' type that are somehow given to terms links ...
but I don't want to copy / duplicate the whole function JUST to add some classes on some terms, besides it would require awkward code messing general purpose treatment and add hoc codes for some terms of some vocabulary id's.. how would I do that anyway ? Create a mytheme_link function in my template.php file ?
Second
I, among other things, found this nice resource http://11heavens.com/putting-some-order-in-your-terms that explains how to rebuild the whole $terms template variable, is is something I should do ? And even in that code snippet it end up calling $terms = theme('links', $terms, array('class' => 'links inline')); and no way to inject inside the link the classes I want ("links inline" is injected in the wrapper around the links in the html code not inside the links)
Third and last
Since terms links all have a class of taxonomy_term_xx where xx is the tid of the term I looked for that and I found in function taxonomy_link($type, $node = NULL) in taxonomy module the following snippet
$links['taxonomy_term_'. $term->tid] = array(
'title' => $term->name,
'href' => taxonomy_term_path($term),
'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
);
So the $links array is build on the 'taxonomy_term_'. $term->tid key and somehow this key become a class in the generated link ...
I say somehow because I failed to find the function where it happens, I suppose it is in a "theme" function or in the "l" function but WHERE ?
Call for help
I keep thinking there must be a simple way to inject classes in links but fail to see how ..
if not I would love to understand better link generation to inject minimal code to achieve my goals...
It is important for me to interfere the least possible with drupal core, code duplication seems always the worst solution to me and some functionnality of my them rely on the coherence of classes injected by drupal, so I'd rather not mess with it, just nicely add my informations...
Comments
Something to help you get started.
You want to create a module that implements hook_link_alter(). In the sample files below I called the module 'somemodule', you will will probably want a different name. The example code (based on the code in the API docs) looks for links where the module name contains 'taxonomy_term'. It builds a hardcoded list of additional classes (this is the part you want to modify), adds the classes to the link.
Example Files
somemodule.module
somemodule.info
thanks I'll look into it that
thanks I'll look into it
that means I can't do it in my theme functions but need to do it in a module.
Seem to me that I mix presentation and functionality.
I will need to access the term part of the node and transfer my news classes conditions there.
I need to carefully read and understand your code before, there is lots of implied convention in drupal and that makes it really hard to get into.
Is the $node in a module the exact same than the $node in the preprocess_var theme function ? So it means I access the terms array the same way with the same attribute?
thanks a lot for your time and answer anyway.
I'll report on success
Actually this approach
Actually this approach separates presentation and functionality. It adds the ability (functionality) to change the look (presentation) by adding new classes.
These are the links that end up n $term in node.tpl.php
And regarding "Is the $node in a module the exact same than the $node in the preprocess_var theme function ?" the answer is yes you mean hook_preprocess_node($var).
Something fishy under the bed :)
Ok I felt something fishy in that approach and now I know what it was :)
I did what you suggested, refactoring a good deal of code in the process, so thanks for that, but I ran into the fundamental problem I badly expressed (out of intuition) with "mixing presentation and functionality".
Now I am given links and their attributes in the $link but i can't tell what they relate too unless I rely on the title or some other attribute inserted by another module (in that case taxonomy_vtn). The later is not a solution since it means an hidden dependency to a module and a reliance on call order.
I need to insert classes into the link but I am handled, on one hand, the $link proper without knowledge of what he relates too and, on the other hand, the $node who is "global" to every link in the node.
And the $link as no reliable information about what term it was generated for.. so ... I feel stuck.. :)
What I need is to inject my classes WHEN the link is generated, so that I have context about the term it is generated for AND I can then decide to inject such or such classes ...
I know I have a knack for complicated needs that look simple and obvious functionality that end up being hardly doable ...
Help ? :)
Do you can expand on "What I
Do you can expand on "What I need is to inject my classes WHEN the link is generated, so that I have context about the term it is generated for AND I can then decide to inject such or such classes ...". I suspect all the context you need is available in hook_link_alter() but hard pressed to provide more concrete help.
From this "I want to inject some classes (in that case some chosen ancestor ids to allow highlighting of the ancestor term when hovering over a descendant tagged term node ) in the generated link of a term link." I would give each term link an 'id' that reflects the terms name (needs to be css safe) and classes that reflect the parent term names. At the same time I would add a class for query, lets call it 'show-ancestors' and use the class to a jQuery function that triggers on the hover event, walks through the elements class names and highlight and element that has an id that matches the class name(s).
Thanks for your continuous help, it is appreciated
I'll explain below what I want to do exactly, but I would not know how to do what you say because right now I would not know how to get to the ancestor of the term that the link I am acting upon represent, since I don't know how to associate that link back to a term in a safe manner.
Here is th print_r() of a $links element
$link : Array ( [title] => Brève [href] => taxonomy_vtn/term/2 [attributes] => Array ( [rel] => tag [title] => ) )the "taxonomy_vtn/term/2" is NOT a reliable information since it is injected by another module that will probably not be on the final site (but it seems to know how to do that I want to do by the way, I must look into that)
So all I have is the "title" who is also not reliable to get back to the tid of the term (because of possible translations and such).
As for what I want to do :
Nodes are associated to a taxonomy hierarchy and I have the "root" terms (in my hierarchy they are level 2 terms, but conceptually they are the root, see schema below) displayed in blocks.
I already have a jquery snippet that highlight the root term a node is associated with when one mouse over it or its teaser... right now it use the
<a>text value in both links to highlight the termto be specific a term link is always generated like this :
<li class="taxonomy_term_13"><a href="/drupal6/taxonomy_vtn/term/13" rel="tag" title="" >XXXXXYYYYYXXXX</a></li>on the taxonomy_blocks I use to display my "root terms" terms are generated like this :
<a href="/drupal6/taxonomy/term/13">XXXXXYYYYYXXXX</a>of course this is a mock up solution but not the right one because relying on XXXXXYYYYYXXXX being the content of both
<a>tag is not reliable.The right one should inject classes both in generated term link in the taxonomy-bloc and in the term link in the node and the jquery snippet should use the same "class" names that are generated when emitting terms related links.
Moreover since a node can be associated to the descendant term of one of my root terms displayed in my taxonomy-blocs, I need to inject in the node the ancestor class to have it properly associated with the root term he relates too.
A more "graphic" explanation
.....................................................................................................................................OURIDEAS
.............................................................................................................................................|
...............................................THEGOOD.......................................................................THE BAD......................................................THEUGLY
.......................................................|.....................................................................................|.....................................................................|
.......................................MAINIDEA01_____MAINIDEA02...................MAINIDEA03___________MAINIDEA04.....................MAINIDEA05_______MAINIDEA06
............................................|..................................|................................................................................................................|..................................|
..................................|-------------|......................|--------------|..................................................................................................|..................................-------------|
..................SUBIDEA11.........SUBIDEA12......SUBIDEA21..SUBIDEA22...............................................................................SUBIDEA51......................................SUBIDEA61
The Taxonomyblocs contains "MAINIDEA01,MAINIDEA02"
the nodes are associated with any of MAINIDEA0, MAINIDEA02, SUBIDEA11, SUBIDEA22 for instance.
a node associated with SUBIDEA22 must highlight the MAINIDEA02 in the taxonomybloc that represents THEGOOD (and contains MAINIDEA01 and MAINIDEA02 terms link).