1

I have content type A with a field that references nodes of type B. This is field with multiple values.

I need to show all the values of content type A, and include one of the fields of the referenced content type B (an image). So I created a view showing these fields.

The problem is that I just want to show the first result of content type B and eliminate the duplicates of content type A. So according to the post Only show first entity reference in views, I used the module Views Distinct. And it works, the problem is that I have more than 2000 nodes and the query is very very slow.

So the next thing I thought was to use the Infinite Scroll module to make it faster. And this works, but the problem now is that I need to group the results by a field, and with this module the header of each group is being repeated, every time results are loaded.

So I thought I could use the Delta for the reference field, and filter it to the nodes with delta = 0, but I don't know why the first value is not 0 is a different number. So this option is not valid.

Does anyone know how to show only the first reference with a good performance?

2 Answers 2

1

I think you were on the right track with the delta filter. I've had success with this method before. You should print the deltas as a part of your testing to try to determine why it isn't working properly. Also, make sure that you aren't using a relationship on the delta filter.

[Edit] If you continue to have issues with the deltas being inconsistent, then I would recommend the views field view option. It should be more performant than views distinct and, while not preferred, will get the job done.

Another option would be to implement your own reset delta logic on node A save (really not ideal though).

4
  • Yes I thought so, but the values are random, they start in a different number than zero. But never the same. The only reason I can think is that I had set those values programmatically and I've been deleting them and assigning them back. Could this be the reason? Any way to reassign the values starting from zero?
    – jmzea
    Jan 25, 2017 at 18:20
  • I would imagine that resaving the node A would reset the delta values, however I've never tested this.
    – lukedekker
    Jan 25, 2017 at 18:21
  • Apparently resaving doesn't reset the values, I'll investigate it.
    – jmzea
    Jan 25, 2017 at 18:23
  • I have unset programmatically all the values for the field with the following code: code $nodes = node_load_multiple(array(), array('type' => 'exposiciones')); foreach ($nodes as $node){ unset($node->field_obras['und']); node_save($node); }code and set them back programmatically too, and now the first value got a 0 delta value. So I can use it as a filter to only show the first referenced value. Before I was deleting the referenced nodes without unsetting them first, that's probably why delta weren't starting at 0. Thanks @lukedekker and @echo for your help.
    – jmzea
    Jan 26, 2017 at 9:55
3

I can suggest you one solution which will probably help you with performance.

You can make use of views field view module.

You can use the view which you have created to render the fields of content type A and now create a separate view (as block) of content type B with current node ID as contextual filter and required relationship for the reference field and set limit to 1 or use delta as filter and call this view as field in the view in which you have rendered content type A fields.

EDIT: Second view can be called conditionaly wherever required, so is more handy if you require it only once in the parent view (by result count or once in header) OR get rendered it with every result of parent view.

2
  • Thanks for your reply. But with the first view of content type A, I would still need to filter to remove the duplicates, and the problem is that apart from using the Views Distinct module I don't know how to do it.
    – jmzea
    Jan 25, 2017 at 18:05
  • If you are using views field view, then the relationship to content type B is taken care of by the second view. If you remove the relationship to content type B from the parent view, it should get rid of the duplicates.
    – lukedekker
    Jan 25, 2017 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.