How can I work out this view?

2019-02-25 19:41发布

问题:

  • List -> l_user I have a List node that has a user-reference field (l_user).
  • Story -> s-user I then have Story nodes that also have a user-reference field (s_user).
  • There is no direct connection between List and Story.

I want to add a view to List that lists all the stories that reference the same user that list references. Basically something like SELECT stories WHERE story.s_user = this list.l_user and l_user is the user referenced in the list this view is on.

So far the view is being filtered by node:type story but I'm not sure what to use (arguments/filter, etc) to link the story s_user to the list l_user.

Is this doable with Views?

回答1:

You can do this with a Views argument. What you're trying to do is filter that list by user, and you get the user to filter by from the node you're currently on (or more specifically, the node's user reference field). So what you'll need to do is supply Views with an argument that is equal to the node's user reference field.

To do that, set up your view as normal and as if you were showing every user's node. So you might have a view that's like:

  • Page 1 (by User 1)
  • Page 2 (by User 1)
  • Page 3 (by User 2)
  • Page 4 (by User 2)
  • Page 5 (by User 1)

Where the user is a user reference field on each page called Story user reference.

Now, under Arguments add an argument for Content: Story user reference. Now, the view will only show nodes that are posted by the user specified in the argument. The problem is, in a block, there is no argument to specify: you need to provide a default argument.

Still on the argument configuration pane for Content: Story user reference, select Provide default argument under Action to take if argument is not present. You'll get a variety of options, but none of them are what you're looking for: the current node's user reference field.

So you'll need to use the PHP code action and use the following code:

$node = node_load(arg(1));
return $node->field_list_user[0]['uid'];

This loads a node based on the node ID retrieved from the current page's path and returns the node's user reference field (change field_list_user to the name of the user reference field on the list nodes).

So if I'm on node 17 whose user reference field states user 4, the argument that'll be passed to the view is 4. The view will then only show nodes in the view who have user references that are also 4.

Save your view, create a block display, and place it wherever you want. When you visit a node page with a user reference field, the block will populate with the referenced user's nodes.



回答2:

I'd suggest getting all of the user references from the list node and passing those into a views argument. So your code would look something like this (untested):

$user_ids = array();
foreach ($list_node->l_user as $user_reference) {
  $user_ids[] = $user_reference['uid'];
}
$view = views_get_view('list_view');
return $view->preview('block_1', array(implode(',', $user_ids));

That assumes you have a view named 'list_view' with a display named 'block_1' (you can see the machine name when you hover over the display). That display needs to be a node view with a filter of node type 'story' and an argument of content user_l set to take multiple values. There's almost certainly a bug in that code, but I know the general concept works, as I've done it several times before.



回答3:

To solve this you need to download NodeReferrer. http://drupal.org/project/nodereferrer

It provides a counter part to CCK's node reference field.

Then you can call the field "Node referrer" in your View.