0

I have a content type Article with an Entity Reference to himself.

Demo content:

Article 1 -> No ref
Article 2 -> No ref
Article 3 -> Ref to 1
Article 4 -> Ref to 1
Article 5 -> Ref to 2
Article 6 -> Ref to 1, Ref to 2

Step 1: I want a View list to display every node that doesn't referring to anybody. So: Filter criteria: RefField = Is empty works nice and the results:

View list results:

Article 1
Article 2

Step 2: Display the title of every node that is referring to the ‘parent’

View list results:

Article 1
   Article 3
   Article 4
   Article 6
Article 2
   Article 5
   Article 6

I'll break my head! The same architecture works fine if I have 2 content types, but I can't figure out with a single content type.

Edit: When I create a relationship: Entity Reference: Content referencing Content from field_Article_Ref and then add a Field and use the above relationship: (Content referencing Content from field_Article_Ref) Content: Title (Title). It displays the childs but the results are not grouped (even I use query settings->Distinct) and looks like:

Article 1
   Article 3
Article 1
   Article 4
Article 1
   Article 6
Article 2
   Article 5
Article 2
   Article 6

How I can group the results?

After that point go to Format->Settings->Grouping field Nr.1 and group by the desired field.

  • What's not working here? what is your problem? it sounds like you have a working solution ... – tenken Oct 20 '14 at 17:37
  • @tenken thanks for asking, I edit the question and add the next step. But the results are not grouped. – tvl Oct 21 '14 at 8:56
0

I find it's easiest to do this with Views Field View module. And then enable views caching for the sub-views related queries on the page.

This module allows you to embed a view as a field in a view. A new field handler is made available, so this can also be used in area (header/footer/empty) handlers as well as rows.

This view handler can accept arguments from fields of the parent view using tokens and pass them into the child view for each row. Raw or rendered token values can be used, as well as static values.

  • WOW! I love it! I didn't know that module. Opens new horizons on how Views works. But it worries me the fact of speed and the need of extra caching. I'd like to avoid the cache on this level. I'll do some testing and I'll report back. – tvl Oct 22 '14 at 10:39
  • Quick and dirty testing. Solution1: Single view with Relationships. Solution2: Two views with View field View Without cashing, Sol1: Query build time 4.15 ms, Query execute time 1.85 ms, View render time 24.19 ms Without cashing, Sol2: Query build time 3.88 ms, Query execute time 1.69 ms, View render time 97.17 ms With content cache, Sol1: Query build time 4.19 ms, Query execute time 1.9 ms, View render time 21.48 ms With content cache, Sol2: Query build time 3.6 ms, Query execute time 1.53 ms, View render time 81.76 ms – tvl Oct 22 '14 at 11:16
  • Maybe it’s not a scientific test on the production server but gives me an idea. So probably I go with the Single view solution but I'll use the Views Field View For other cases. Many thanks for the advice! – tvl Oct 22 '14 at 11:22
0

Recapitulation

  1. Relationships->Add->Entity Reference: Referencing entity of the Field that reference to himself. Also check Require this relationship.
  2. Fields->Add->Content: Title.
  3. Fields->Add->Content: Title and use the relationship.
  4. Format->HTML list (makes a wrapper element).
  5. Format->Settings->Grouping field Nr.1->Content: Title.
  6. Fields->Edit Content: Title->Exclude from display (it will be displayed by the grouping).

PS Also check the Views Field View module mentioned by @tenken here

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.