0

I am using Views 3 in Drupal 7 to show parent taxonomy terms using nested relationships.

This involves setting up a parent relationship, then a parent relationship of that parent, and then a parent relationship of that parent - this goes five levels deep and appears to be working well.

I have set the view to output the term name associated with each relationship, and with Views set to not render anything if the result is empty, I am being left with exactly what i need - a list of all (well, five levels right now) hierarchical parent terms. I have also set views to link each of these term names to their respective pages - and this is what is causing me problems...

Thing is, I want to see these fields as an unordered list. Setting the format option in Views to HTML list obviously sees all of my fields as part of the same result, and thus makes them the same list item.

The first thing I tried was to hide each field from display and rewrite everything using a custom text field:

<ul>
<li>[Term_Name_Level_1]</li>
<li>[Term_Name_Level_2]</li>
<li>[Term_Name_Level_3]</li>
<li>[Term_Name_Level_4]</li>
<li>[Term_Name_Level_5]</li>
</ul>

This works, but when I'm not five levels deep in the hierarchy (which is most of the time), I end up with empty li tags, as so:

(I put periods in here to show the effect, as the blank list items don't show here otherwise)

  • .
  • .
  • .
  • Grandparent term name
  • Parent term name

The next thing I tried was to rewrite each individual term name field, adding li tags around each one

<li>[Term_Name_Level_1]</li>

then rewriting the output as follows

<ul>
    [Term_Name_Level_1]
    [Term_Name_Level_2]
    [Term_Name_Level_3]
    [Term_Name_Level_4]
    [Term_Name_Level_5]
</ul>

This almost worked, as if there is no result, the whole field is left out including the tags. The problem here is that the link to the term page is wrapping around the li tags, like so:

<ul>
<a href"#whatever"><li>Grandparent term name</li></a>
<a href"#whatever"><li>Parent term name</li></a>
</ul>

Thats obviously not what I need, so can anyone think of a way to get Views to output each field as a list item, but without printing empty tags when there is no result? I must say, I have a horrible feeling that I'm overlooking something!

Is there perhaps a way to get Views to output a raw link to each term page, so I can rewrite the lot? I see that there is a 'Taxonomy term: Term edit link' field available, but not a link to the actual term!

Thanks!

  • Forget the tokens, not going to work like that. – Alex Gill Jun 11 '13 at 15:22
  • 1
    Create a specific template for this view views-view-fields--[BLAH_BLAH].tpl.php and add your html list there. – Alex Gill Jun 11 '13 at 15:25
0

You can add the Term:ID and set the link to the term in 'rewrite results' as:

taxonomy/term/[tid]

If you want to keep the aliased URLs, you can use Global Redirect module.

Once enabled, an alias provides a nice clean URL for a path on a site. However Drupal does not remove the old path (eg node/1234). The problem is that you now have two URLs representing the same content. This is dangerous territory for duplicate pages which can get you sandboxed by the search engines!

  • Unfortunately this breaks the illusion of uri hierarchy I have cultivated with these nested terms. Really good idea though! – PUncle Jun 11 '13 at 18:38
  • If you mean you have set different paths for the terms, the internal url of a term is always valid. You can use Global Redirect module to set Drupal always use ONLY one path for a page. This way, you can always use internal paths of the terms, nodes or whatever entity but users will always see the aliased URL. – Елин Й. Jun 11 '13 at 19:32
  • Yours really is a creative way to get around this issue, but I ended up using a views-fields template with a bunch of conditionals to get to a similar result in a cleaner (in my opinion) way. Thanks for sharing though! – PUncle Jun 12 '13 at 11:46
0

The credit really goes to Alex Gill above for suggesting this, but as he didn't leave it as an answer (or give any details ;), I figured that I should.

In Views 3, the last option under 'Advanced' (the third column) is 'Theme: Information' - clicking this get you a whole bunch of override suggestions. In my case I created a new template file called 'views-view-fields--parent-items.tpl.php', with the 'parent-items' part being the name of my view. You'll see already customised file names for your particular view under 'Theme: Information'.

In this file (placed in the root of your theme directory) I used conditionals to check if each field existed, before printing it out with the li tags inside those conditionals. See below:

<ul id="parent-items">
    <?php if (!empty($fields['name_5']->content)): ?>
        <li><?php print $fields['name_5']->content; ?></li>
    <?php endif; ?>
    <?php if (!empty($fields['name_4']->content)): ?>
        <li><?php print $fields['name_4']->content; ?></li>
    <?php endif; ?>
    <?php if (!empty($fields['name_3']->content)): ?>
        <li><?php print $fields['name_3']->content; ?></li>
    <?php endif; ?>
    <?php if (!empty($fields['name_2']->content)): ?>
        <li><?php print $fields['name_2']->content; ?></li>
    <?php endif; ?>
    <?php if (!empty($fields['name_1']->content)): ?>
        <li><?php print $fields['name_1']->content; ?></li>
    <?php endif; ?>
</ul>

[name_1] to [name_5] are my field names, which can be found under the replacement patterns section for each field.

In order to have this work well I also disabled all markup generated by views.

Thanks Alex for the idea, and Елин Й. for a pretty clever suggested workaround.

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.