3

I know that this has been asked several times with different variations, but I cannot seem to find an optimal solution, so here we go again:

I have a content type 'Parent' and a content type 'Child'. The relation between the two is one-to-many i.e. there is a field within the 'Parent' CT called 'children' which is of type 'Entity Reference' and allows for multiple values. Now, I want to create a view that will fetch 'Parent' nodes and for each resulting row I want to display the respective children nodes. Something like this:

    -----------
    Parent 1
    - Child 1
    - Child 2
    -----------
    Parent 2
    - Child 3
    - Child 4
    ----------

Solutions I have found and explored so far:

1. Use a Relationship for the 'Child' nodes within the 'Parent' view

OK, I can now use the 'Child' node fields, but instead of having them grouped within the 'Parent' node row, I end up with one row per relationship and duplicate content

    -----------
    Parent 1
    - Child 1
    -----------
    Parent 1
    - Child 2
    ----------

How should I remove the duplicate content?

1.1 Use a 'distinct' in query options. Naturally, it does not work, because distinct is applied to the whole row and since parent-child field tables are joined, each row IS actually distinct

1.2 Use aggregation. Again the desired result cannot be achieved, since by grouping by Parent nid I will only get the first Child node reference

1.3 Use module views_distinct. OK, this seems to do the job, but it breaks the pager and the results counter as processing is done after the respective view queries have been executed

2. Use a views_field_view, so that respective 'Child' nodes are fetched by a separate view (via Contexual filter) and embedded gracefully within the 'Parent' node row.

OK, this definitely does the job, and I get no duplicates since the child field tables no longer participate in the Parent view query, BUT:

  • it is not performance optimal
  • performance gets worse considering that my project has 30 different views (for different content types) that need to function similar to the 'Parent' view in this example
  • since I already have 30 "main" views, I would hate to create another 30 ones only to be used as field views for the "main" ones

3. Do the job manually: use a 'Global: Custom text' field, use the template "views-view-field--parents--nothing-1.tpl.php" and implement the whole logic there i.e. re-load the Parent node, get the Child node ids, load the Child nodes and print their desired fields. OK, this does the job too, BUT:

  • it is not exactly the "Drupal way" of coding against views
  • once again, there are performance issues since I bypass Drupal and load stuff on my own
  • once again, I will have to implement 30 template files named like "*nothing_1" for each of my views

So, my question is the following:

Is there a solution for this issue other than the ones that I mention here? If not, then which one should I choose and how to overcome its disadvantages?

1

You can fetch the 'Parent' nodes with their 'Children' nodes with the default functionality of views and without the use of Relationships, Aggregation or other modules.

The Steps Are:

  • Create a view of 'Parent' content type.
  • In the 'Fields' section add the field 'Content: Title' and the field 'Content: Children(Appears in: node:parent)'.
  • In the 'settings' of the 'Format' section select as 'Grouping field Nr.1' the 'Content: Title' field.
  • In the field 'Content: Children' select the display format that you want.
  • Finally 'Exclude' from display the 'Title' field.

Enjoy

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.