4

I have a view that displays all 'parent' nodes referenced from 'child' nodes. I just want to list the titles of the referenced nodes, but because there can be multiple 'child' nodes referencing the parent nodes, my view displays the 'parent' node titles multiple times (depending on how many child nodes refer to the parent).

I was trying to use Views 3.x's new 'Grouping' feature to group all nodes by nid, but that didn't seem to make a difference. I tried using an nid that was grouped in the filters section, and an nid that was grouped in the fields section, but it didn't matter.

I could do a views_pre_render and just remove the duplicates manually, but that's messy, and would also screw up the pager. (See here for something along those lines: http://capellic.com/blog/cure-duplicate-nodes-in-a-view).

Is there a better way to do this, or something I'm overlooking?

I also tried using the following hook_views_query_alter() hook:

/**
 * Implements hook_views_query_alter().
 */
function custom_views_query_alter(&$view, &$query) {
  if ($view->name == 'my_subscriptions' && $view->current_display == 'my_networks') {
    $query->groupby[] = 'node_field_data_field_list_network_nid';
  }
}

But still no dice...

Here's the query being generated by views:

SELECT DISTINCT node_field_data_field_list_network.nid AS node_field_data_field_list_network_nid, node_field_data_field_list_network.title AS node_field_data_field_list_network_title, node.title AS node_title, node.nid AS nid
FROM 
{node} node
INNER JOIN {flag_content} flag_content_node ON node.nid = flag_content_node.content_id AND (flag_content_node.fid = :views_join_condition_0 AND flag_content_node.uid = :views_join_condition_1)
LEFT JOIN {field_data_field_list_network} field_data_field_list_network ON node.nid = field_data_field_list_network.entity_id AND (field_data_field_list_network.entity_type = :views_join_condition_2 AND field_data_field_list_network.deleted = :views_join_condition_3)
INNER JOIN {node} node_field_data_field_list_network ON field_data_field_list_network.field_list_network_nid = node_field_data_field_list_network.nid
INNER JOIN {users} users ON node.uid = users.uid
WHERE (( (node.status = '1') AND (node_field_data_field_list_network.type IN  ('network')) AND (users.uid = '1' ) ))
ORDER BY node_title DESC
LIMIT 3 OFFSET 0
2
  • 1
    What are the fields you including in your views. (i) Node (ii) Node Ref ? Commented Jun 17, 2011 at 15:49
  • I'm just including the node title in the display (that's all I want—a list of node titles). However, I tried also including the node nid of the 'parent' content type, and grouping on that, but that didn't help :( Commented Jun 17, 2011 at 15:57

2 Answers 2

1

Well, as it turns out, I avoided this issue by adding another filter, on the 'list' nodes (the children), and I'm lucky that I have one child 'list' node per network that has a unique flag (a 'master' checkbox field that can only be checked on one child per parent 'network' node).

I filtered to only child 'list' nodes that contain that flag, and this means that my parent 'network' nodes only show once.

I still wish I didn't have to do it this way, though. If anyone else can think of a way to actually get a distinct set of nids/titles without my having to use the 'master' flag on the child nodes, I'll definitely mark that answer correct.

0

Try the Views Field View module.

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.

It can fix issues with stubborn duplicates that result from the use of Node References by splitting the work of a single Views' query into two separate queries performed by two separate Views. The results of one view can then be embedded as a field in another. This simplifies a view that may otherwise need to use complex Relationships and Contextual Filters.

In this case, you'd do something like this:

  1. Install and enable Views Field View.
  2. Create a new View:
    • Add a Contextual Filter for the node reference field that references the parent node.
    • Add a Field to display the title of the node.
  3. Edit your original View:
    • Add a field: "Content: Nid" (if it isn't there already)
    • Check the box to exclude it from display if desired and apply the settings.
    • Add another field: "Global: View." and edit its settings.
    • Under 'View settings->View,' select the view you just created.
    • Under 'View settings->Contextual filters,' add a token: "[nid]".
    • Apply the settings.
    • Ensure that Content: Nid appears before Global: View in the 'Fields' list.
  4. Save the view.

Now on your original view you should see a list of referenced parent nodes without duplicates.

2
  • 1
    How can that module help? Commented Jun 25, 2012 at 16:06
  • Updated my answer to improve the explanation and added configuration steps.
    – dags
    Commented Sep 18, 2013 at 18:51

Your Answer

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

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