我怎样才能得出这个观点?

2021-12-29 00:00:00 php drupal drupal-views
  • 列表 ->l_user 我有一个 List 节点,它有一个用户引用字段 (l_user).
  • 故事 ->s-user 然后我有 Story 节点,这些节点也有一个用户引用字段 (s_user).
  • ListStory 之间没有直接联系.
  • 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.

我想向 List 添加一个视图,该视图列出了引用 list 引用的同一用户的所有故事.基本上类似于 SELECT stories WHERE story.s_user = this list.l_userl_user 是此视图所在列表中引用的用户.

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.

到目前为止,视图由 node:type story 过滤,但我不确定使用什么(参数/过滤器等)来链接 story s_userlist l_user.

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.

这对视图可行吗?

推荐答案

您可以使用 Views 参数来做到这一点.您要做的是按用户过滤该列表,并让用户从您当前所在的节点(或更具体地说,该节点的用户引用字段)进行过滤.所以你需要做的是为 Views 提供一个等于节点的用户引用字段的参数.

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:

  • 第 1 页(按用户 1)
  • 第 2 页(按用户 1)
  • 第 3 页(按用户 2)
  • 第 4 页(按用户 2)
  • 第 5 页(按用户 1)

其中用户是每个页面上的用户参考字段,称为故事用户参考.

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

现在,在 Arguments 下为 Content: 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.

因此您需要使用 PHP 代码 操作并使用以下代码:

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'];

这会根据从当前页面路径检索到的节点 ID 加载节点,并返回节点的用户引用字段(将 field_list_user 更改为列表节点上的用户引用字段的名称).

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).

因此,如果我在节点 17 上,其用户引用字段指出用户 4,则将传递给视图的参数为 4.然后视图将仅显示视图中具有用户引用也是 4 的节点.

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.

相关文章