I was able to accomplish this functionality through the use of two separate views combined into one. I could not find a way for Drupal to handle this out of the box. Essentially I am using one view to show the nodes related to the child terms, and using another view to bring in the View displaying child related nodes and sort it under the parent term.
The modules I used were: Views PHP, entity reference
Create your taxonomy vocabulary:
* Parent term
* Child term1
* Child term2
Create a content type
Once created add a Entity Reference field of type Term Reference referencing your taxonomy vocabulary's children terms you have created. Create a handful of nodes.
Create a View Block displaying content (related Nodes)
After creating the View Block of content (the content type with the reference, show fields, HTML list), include the fields you want to show for the related node and give that view a contextual filter of the Entity reference field you used for the content type. Simply set the settings of the contextual filter under When the filter value is NOT available to Hide View. You can optionally set the no results behavior as well.
Create a view displaying taxonomy terms (Master view)
Create a view of taxonomy (HTML List, Show fields), add a relationship of Taxonomy term: Parent term and require this relationship. Add field Taxonomy term: Term ID
and hide from display. Add field taxonomy term: Name
and under the field setttings, set the relationship to Parent, then hide from display. Add field Taxonomy term: Name
to display the name of the term. Finally add a Global: PHP
field and under the Output code provide the following snippet:
<?php
$viewNodeDetail = views_get_view('YOUR_VIEW_BLOCK_MACHINE_NAME');
$viewNodeDetail->set_arguments(array($row->tid));
print $viewNodeDetail->preview('BLOCK_MACHINE_NAME');
?>
Under the view format settings set the Grouping field Nr.1 to the (Parent) Taxonomy term: Name
.
This will treat the view block as a field (the view block is the child term + node and the master view sorts by the parent term). This will give you an end result of:
- Parent term
- Child term
- Related node
- Related node
- Related node
- Parent term