Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyword Path in SDL Tridion

Could someone please give some idea on how this can be done? This might be very simple and basics, but i couldn't figure this out.

Here is my requirement.

I have a category A with child keyword B and B got another Child Keyword C.

I want to get the exact path of selected keyword in my component template,Say for eg, if user selects keyword C, i need the value with path like A\B\C and not just as C. But Tridion always gives me the value as C and not as A\B\C . Component Schema is using "Tree" view to select the keywords.

Should I be writing dreamweaver custom functions to handle this? Or does tridion comes with some handler for this?

Any help would be highly appreciated. Thank you!

Thanks, KK

like image 507
Krishnakumar Avatar asked Jun 25 '12 14:06

Krishnakumar


2 Answers

As you just found out, the Tridion Keyword Hierarchy is "fake" - Keywords are stored as a flat list, not as a hierarchical list (like you would have with folders). The information about the parent and children keywords is stored in the keyword itself.

There are solutions for this - of course, for instance you can use this in a C# TBB:

Keyword keyword = new Keyword(new TcmUri("tcm:28-3368-1024"), session);
string hierarchy = keyword.Title;
bool done = false;
while(!done)
{
    if (keyword.ParentKeywords.Count > 0)
    {
        foreach (Keyword k in keyword.ParentKeywords)
        {
            hierarchy = k.Title + " > " + hierarchy;
        }
        keyword = keyword.ParentKeywords[0];
    }
    else
        done = true;
}
// Include Category
hierarchy = keyword.OrganizationalItem.Title + " > " + hierarchy;

EDIT: Updated to recursively "go up" the hierarchy. HOWEVER a keyword can have multiple parents, I'll leave that up to you to fix...

like image 196
Nuno Linhares Avatar answered Oct 19 '22 20:10

Nuno Linhares


Keywords within a category are unique, so Tridion can safely refer to them by their name (and/or their TCM URI of course). And since a Keyword can have multiple parents, there may not be a single path leading from the root to your Keyword.

If in your situation the category can be represented as a tree, you can of course build a single path to each keyword. In that case you'll need some (C#) code that walks up the parents axis and concatenates the names. You can put this code either:

  1. in a TBB that you put into your template before the DWT OR
  2. in a custom Dreamweaver function.

Either way will work fine.

like image 5
Frank van Puffelen Avatar answered Oct 19 '22 19:10

Frank van Puffelen