I need to save term lineage in Drupal 8, not only the deepest term. I know there are modules like Hierarchical select, Simple hierarchical select or Client-side hierarchical select but these are not for Drupal 8, or not solves "save term lineage" functionality.

I need to get my "nodes" visible on both child term and parent term pages too. I know, I can use the "Content: Has taxonomy term ID (with depth)" (under contextual filters) functionality in "views", but it slows the database queries very much. Or maybe I'm doing something wrong.

Is there some reasonable solution?

Thank you!

EDIT: The hierarchy is very simple. Only two-tiered: Country / City.

Can someone help me to re-save my nodes with "lineage" structure? For example, using the TermStorage functions listed below?

1 Answer 1


Depends on your understanding of reasonable. Until hierarchical select or similar modules become available you could save the parent terms programmatically on node save - the code probably wouldn't exceed 30 lines or so, especially when you don't have many taxonomy fields where you want to do this.

You can access a term's parents with the following code utilizing TermStorage:

$parents = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($termId);

Then you only need to go recursively through the tree and grab all parents (if your terms can have more than one), get their ids and set them in the node you want to save.

  • Thanks for the reply. I have many (almost one million) "nodes" already created. I need to apply any solution to the finished database.
    – Hefi
    Aug 1, 2016 at 13:46
  • Would batch updating your existing nodes be an option? The modules you named wouldn't update nodes already in the db either from what I recall.
    – Zuzuesque
    Aug 1, 2016 at 13:53
  • Batch updating is ok, but....I need some "final solutions". I am not a programmer.
    – Hefi
    Aug 1, 2016 at 16:58
  • Not sure I can help you in that case. Even if I wrote some generic code, you would need to updated it to your needs with the right node/field/taxonomy names + duplicate pieces to cover all fields you need changed. At this point there are no generic 'final solutions' available I am afraid.
    – Zuzuesque
    Aug 1, 2016 at 18:12
  • Hierarchical select is "just" a widget for the user, it doesn't change how those terms are stored. Either you have to live with the slow queries or you have to normalize it as suggested here. The only other alternative I can think of is doing it through a search API index, with custom indexing. But that will require a bit of custom code too. Not everything can be done (in a performant way) without programming, you can always try to hire one for this task.
    – Berdir
    Aug 1, 2016 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.