Group nodes by child & parent taxonothe with Views – Drupal 7

drupal-7drupal-taxonomydrupal-views

I have one content type with one taxonomy field with parent->child terms. In one view I need to group nodes by the child selected but also by the parent.

Expected output

  • parent 1 <- taxonomy parent
    • term 1 <- taxonomy child
      • title 1 <- node
      • title 2
      • title 3
    • term 2
      • title 4
      • title 5
  • parent 2
    • term 3
      • title 6
      • title 7
    • term 4
      • title 8
      • title 9

Current output

  • parent 1
    • term 1 & title 1
    • term 1 & title 2
    • term 1 & title 3
    • term 2 & title 4
    • term 2 & title 5
  • parent 2
    • term 3 & title 6
    • term 3 & title 7
    • term 4 & title 8
    • term 4 & title 9

Current view config

  • Relationships
    • Taxonomy terms on node <- child
    • Term taxonomy <- parent
  • Format
    • Unformatted list
    • Grouping field: parent
  • Fields
    • Taxonomy term <- parent
    • Exclude from display
    • Taxonomy term <- child
    • Content title <- node
  • Filter
    • Content type (= My content Type)

Thanks!

Best Answer

I got it! Solution

Node view

  • View machine name: node_view_machine_name
  • Type: Block
  • Fields
    • Title
    • Field 1
    • Field 2
  • Filter
    • Content type (= My content Type)
  • Contextual filters
    • Child taxonomy field
    • Hide view when filter is not available

Taxonomy view

  • Type: page
  • Filter
    • Taxonomy term: Vocabulary (= My Vocabulary)
  • Relationship
    • Taxonomy term: Parent term
  • Fields
    • Taxonomy term: Name
      • Relationship: parent
      • Exclude from display
    • Taxonomy term: ID <- We use this id to pass as argument to the node view
      • Relationship: none
      • Exclude from display
    • Taxonomy term: Name
      • Relationship: none
    • Global PHP <- Print node detail related for each child term
      • Output code:
$viewNodeDetail = views_get_view('node_view_machine_name');
$viewNodeDetail ->set_arguments(array($row->tid));
print $viewNodeDetail ->render('block');
  • Format
    • Group by
    • (Parent) Taxonomy term: Name

I hope can be useful for someone else!

Related Topic