How can I work out this view?

前端 未结 3 614
终归单人心
终归单人心 2021-01-22 11:26
  • List -> l_user I have a List node that has a user-reference field (l_user).
  • Story -> s-user I then have
3条回答
  •  感动是毒
    2021-01-22 12:14

    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.

提交回复
热议问题