Using entity reference to filter results and display node title in Views

Events happening in the community are now at Drupal community events on www.drupal.org.
wonder95's picture

I seem to have stumped everyone in the Views issue queue as well as Stackexchnage, so I'm hoping someone here might be able to answer my thorny views question.

This seems like it should be pretty simple, but for some reason I can't get this to work, and I'm not sure why. I have a node type called Project. Some projects can be a subproject of other projects. However, all projects have the same information, so there is no need to have separate entity types. To create this relationship, I have an entity reference field called Parent Project (field_parent_project) that relates sub-projects to parent Project nodes; in other words, only subprojects will have a value in this field.

I have a view that list Project nodes, and I want it to display only parent projects, with subprojects listed as a field at the bottom of each Project node display. I have defined a relationship for field_parent_project, and I have added a filter where field_parent_project is NULL, with the idea that it would not list sub projects, since they have a value for field_parent_project. However, what I get is very different than what I would expect:

  1. My sub-projects are still being listed in the display, even though the filter should be keeping them from being displayed
  2. The parent project node that is referenced by the other subprojects is not displayed at all

I have tried using both the referencing and referenced versions of the relationship, but neither seems to work. I would like to just be able to use this field and not have to add a separate field just to indicate that this is a subproject. What do I need to change to get my view to work as I want?

Comments

If all you want is a view of

ongdesign's picture

If all you want is a view of all nodes that are referenced by another node, you should be able to create a node view, add a reference to "Referenced entity," make the relationship required, then add a title field using the relationship. I just did a quick test of that and it's working. (Let me know if you want an export of the view.)

Note that this is a little deceptive, since your view is filtered to only show child nodes (those containing a reference). However, the only field you're displaying is the title/link to the related node, so you effectively have a view of all related nodes. You could add other fields using the relationship, of course, like published date.

If you need the pure back-reference, you might check this out: https://drupal.org/sandbox/jsacksick/1301456 -- haven't ever used it, but it seems to meet the use case.

Did you try without a relationship?

dnotes's picture

I don't know why I'm about to comment on this, since I have never used entity reference before, but it seems to me that you shouldn't need a relationship for that. At least with the references module and node reference, if you use a relationship with the filter, then you'd actually get all the nodes that reference a node whose reference field is null, which is pretty close to what you're describing so maybe that's why I'm commenting. Or maybe it's because now is a bad time of day for judging whether I'm going to be helpful or just reveal the extent of my ignorance. Anyhow, good luck.

Views is kinda goofy with

jyee's picture

Views is kinda goofy with joins, especially when joining using entity reference and joining to the same table. I suspect that if you were to output the query, you'd see some really screwy stuff with the table being aliased and those aliases being mixed up.

If your query is messed up, you can use the Query Settings under the Advanced section of the Views UI to set a query tag, then fix the query via a hook_query_alter()

To be honest, adding a bit field to indicate if a project is a sub-project would keep things easier and could be easily set via hook_node_update/insert so that it's completely invisible to end users. You could probably also use https://drupal.org/project/computed_field for the bit field.

You could also avoid the join and do a simple view of projects with a null parent field, then use https://drupal.org/project/views_field_view to include a sub view of any child projects.

Portland (Oregon)

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: