When using the field "Taxonomy: All Terms" the terms are ordered by weight and title. I wanted terms ordered instead by their hierarchical position in their relevant vocabularies. e.g:

If I have a hierarchy for cars being "Make > Model" and I had a node that was tagged with "Audi" and "A2" I would want the resulting terms displayed to be in the order "Audi , A2" and not "A2, Audi" which is what happens currently.

I could not find a way to do this so have submitted the attached patch. It adds a field to the form allowing you to specify whether you want your terms re-ordered for each vocabulary. If checked it will present them in hierarchical order.

There is a problem which might be considered a feature or a bug (don't know yet) and that is if you miss a level from the hierarchy then only the terms from the root level down to the point that you missed will be displayed e.g. In a hierarchy of "Make > Model > Type > Colour" if your node is not tagged with "Type" then only "Make, Model" will be shown and "Colour" will be ignored.

Comments

merlinofchaos’s picture

Status: Needs review » Needs work

This has at least two style problems:

1) { should never be on a line by itself.
2) $ary is not a good variable name. Nor is $filt -- Drupal coding style chooses readable names for variables most of the time. $ary is also bad because it has very little meaning. It should probably be something like $terms.

Beyond that: This patch is pretty crude because, really, it is just one of many ways that someone might want to re-order the terms, and it only works if you have your terms set up a particular way. So it satisfies your use case and pretty much only your use case.

What would be better would be to create some kind of hook that would allow an external entity to control the ordering. Possibly some kind of ordering plugin though the complexity rises. This is mostly a presentation issue, so doing it as a theme function might be acceptable, though it wouldn't actually change the output, just re-order (and possibly add or remove) terms that are then rendered later in the pipeline.

dawehner’s picture

Additional using recursive methos to make ordering is afaik not the fastest way: http://www.php.net/manual/de/function.uasort.php should be the better way

esmerel’s picture

Status: Needs work » Closed (won't fix)

No updates to patch in more than 3 months.

nicktech’s picture

Thanks for the patch. I was looking for this exact feature in views. Would be great to see it committed to Views proper.

jrstmartin’s picture

Thanks so much for this patch! Looks like the maintainers don't like it, but at a high level, this patch makes a lot of sense. Taxonomy is all about hierarchical categorization and it would be great if the Taxonomy: All Terms field respected hierarchy by default.

I found hook_views_pre_render too difficult to use as a novice Drupal/PHP programmer. It was given as a potential solution in a similar issue.

This may appear as an edge-case to some, but hierarchy is very common in taxonomies. I bet you a lot of people just gave up looking for a solution to this :). This issue is something that's been bugging me for years and I've given-up looking for solutions a few times :). Thanks Shawn_S!

steve.hartley’s picture

I've made the code changes as suggested by merlinofchaos and fixed a warning - "Invalid argument supplied for foreach()" which occurred if no terms are attached to a node.

esmerel’s picture

Status: Closed (won't fix) » Needs review

Please don't suggest that a 'Won't fix due to uncorrected problems' as a "the maintainers don't like it". One of the primary maintainers asked for changes - that suggests that the maintainers, did, in fact, think it was worthwhile. We can't leave issues just sitting around forever though, and not much work is happening in 6.x at this point.

Since stevehart808 made the changes, I'll happily set it back to 'needs review' - thanks steve!

Status: Needs review » Needs work

The last submitted patch, views-hierarchical_ordering-660116-6.patch, failed testing.

steve.hartley’s picture

StatusFileSize
new3.36 KB

Sorry corrected the last patch so its against the latest dev version.

mustanggb’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)