drupal视图创建_Drupal 7:视图中的关系,上下文过滤器和字段重写3

drupal视图创建

In this article we will continue exploring the powers of Views and focus on how to use relationships, contextual filters and rewrite field outputs. In a previous tutorial I showed you how to create a new View and perform basic customizations for it. We’ve seen how to select a display format, which fields to show and how to filter and sort the results.

在本文中,我们将继续探索View的功能,并将重点放在如何使用关系,上下文过滤器和重写字段输出上。 在上一教程中,我向您展示了如何创建一个新视图并对其执行基本自定义。 我们已经看到了如何选择显示格式,要显示哪些字段以及如何对结果进行过滤和排序。

In this article we will go a bit further and see what relationships and contextual filters are – the two most important options found under the Advanced fieldset at the right of the View edit page. Additionally, we’ll rewrite the output of our fields and combine their values into one.

在本文中,我们将进一步介绍一下关系和上下文过滤器-在“视图”编辑页面右侧的“ 高级”字段集下找到的两个最重要的选项。 此外,我们将重写字段的输出并将其值合并为一个。

To begin with, I have a simple article View that just shows the titles. Very easy to set up if you want to follow along. And there are three things I want to achieve going forward:

首先,我有一篇简单的文章View,仅显示标题。 如果要继续学习,则非常容易设置。 今后我要实现三件事:

  1. Make it so that the View shows also the username of the article author

    使其显示视图还显示文章作者的用户名
  2. Make is so that the View shows only articles authored by the logged in user

    设置为使“视图”仅显示由登录用户创作的文章
  3. Make it so that the author username shows up in parenthesis after the title

    使得作者的用户名显示在标题后的括号中

人际关系 (Relationships)

First, let’s have the View include the author of the articles. If the View is displaying fields (rather than view modes or anything else), all we have to do is find the field with the author username, right? Wrong. The problem is the following: the node table only contains a reference to the user entity that created the node (in the form of a user ID – uid). So that’s pretty much all we will find if we look for user related fields: Content: Author uid.

首先,让View包含文章的作者。 如果“视图”显示的是字段(而不是查看模式或其他任何方式),我们要做的就是找到带有作者用户名的字段,对吗? 错误。 问题如下:节点表仅包含对创建该节点的用户实体的引用(以用户ID – uid的形式)。 因此,如果我们寻找与用户相关的字段,便可以找到几乎所有Content: Author uid

What we need to do is use a relationship to the user entity found in the user table. Relationships are basically a fancy way of saying that table A (in our case node) will join with table B (in our case user) in order to retrieve data related to it from there (such as the name of the user and many others). And the join will happen in our case on the uid field which will match in both tables.

我们需要做的是使用与user表中找到的用户实体的关系。 关系基本上是一种奇特的说法,表A(在我们的案例中为node )将与表B(在我们的案例中为user )联接,以便从那里检索与其相关的数据(例如用户名和许多其他名称) 。 在本例中, uid将在两个表中都匹配的uid字段上发生。

So let’s go ahead and add a new relationship of the type Content: Author. Under Identifier, we can put a descriptive name for this relationship like Content Author. The rest we can leave as default.

因此,让我们继续添加Content: Author类型Content: Author的新关系。 在“ 标识符”下 ,我们可以为这种关系添加一个描述性名称,例如“ 内容作者” 。 其余的我们可以保留为默认值。

Now if you go and add a new field, you’ll notice many others that relate to the user who authored the content. Go ahead and add the User: Name field. In its settings, you’ll see a Relationship select list at the top where the relationship identifier we just specified is automatically selected. That means this field is being pulled in using that relationship (or table join). Saving the field will now add the username of the author, already visible in the View preview.

现在,如果您添加一个新字段,您会注意到许多其他与创作内容的用户有关的字段。 继续并添加User: Name字段。 在其设置中,您会在顶部看到一个“ 关系选择”列表,其中自动选择了我们刚刚指定的关系标识符。 这意味着使用该关系(或表联接)来拉入该字段。 现在,保存字段将添加作者的用户名,该用户名已在“视图”预览中可见。

relationships

You can also chain relationships. For instance, if the user entity has a reference to another table using a unique identifier, you can add a second relationship. It will use the first one and bring in fields from that table. So the end result will be that the View will show fields that relate to the node through the user who authored the node but not strictly from the user table but somewhere else connected to the author. And on and on you can join tables like this.

您还可以链接关系。 例如,如果user实体使用唯一标识符引用另一个表,则可以添加第二个关系。 它将使用第一个,并从该表中引入字段。 因此,最终结果将是,视图将显示通过创建节点的用户与节点相关的字段,但并非严格地来自user表,而是连接到该作者的其他位置。 而且,您可以不断地像这样联接表。

上下文过滤器 (Contextual filters)

Contextual filters are similar to regular filters in that you can use mainly the same fields to filter the records on. Where contextual filters differ greatly is that you do not set the filtering value when you create the View, but it is taken from context.

上下文过滤器与常规过滤器相似,因为您可以主要使用相同的字段来过滤记录。 上下文过滤器有很大不同的地方是,创建视图时无需设置过滤值,而是从上下文中获取的。

There are many different contexts a filter value can come from, but mainly it comes from the URL. However, you can instruct Views to look elsewhere for contexts as well – such as the ID of the logged in user.

过滤器值可以来自许多不同的上下文,但主要来自URL。 但是,您也可以指示视图在其他位置查找上下文,例如已登录用户的ID。

What we’ll do now is add a contextual filter so that the View shows only the articles authored by the logged in user. So go ahead and add a new contextual filter of the type Content: Author uid. Next, under the WHEN THE FILTER VALUE IS NOT IN THE URL fieldset, select the Provide default value radio. Our goal here is to have Views look elsewhere if it does not find the user ID in the URL.

现在,我们要做的是添加一个上下文过滤器,以便“视图”仅显示由登录用户创作的文章。 因此,继续添加类型为Content: Author uid的新上下文过滤器。 接下来,在“ 在URL字段中没有过滤器值时”下 ,选择“ Provide default value单选。 我们的目标是,如果在URL中找不到用户ID,则视图将在其他地方显示。

contextual filters

You then have some options under the Type select list, where you should choose User ID from logged in user. This will make Views take the ID of the user that is logged in and pass it to the View as a filter. The rest you can leave as is and save the filter. You’ll immediately notice in your preview that only articles authored by you show up. The filtering is taking place dynamically. If you log in with another user account, you should see only the articles authored by that user account.

然后,在“ 类型选择”列表下有一些选项,您应该User ID from logged in user选择“ User ID from logged in user 。 这将使View使用已登录用户的ID,并将其作为过滤器传递给View。 其余的您可以保留,并保存过滤器。 您会立即在预览中注意到只有您撰写的文章会出现。 过滤是动态进行的。 如果使用另一个用户帐户登录,则应该只看到该用户帐户撰写的文章。

A great thing about contextual filters is that if you are displaying a View programatically in a custom module, you can pass the filtering value in code, which opens the door to many possibilities.

上下文过滤器的一大优点是,如果要在自定义模块中以编程方式显示View,则可以在代码中传递过滤值,这为许多可能性打开了大门。

改写字段 (Rewriting fields)

The last thing we will do in this tutorial is look at rewriting fields in order to concatenate their values. We will illustrate this technique by changing the title field to include the author username in parenthesis.

我们在本教程中要做的最后一件事是查看重写字段以连接它们的值。 我们将通过更改标题字段以在括号中包括作者用户名来说明此技术。

We’ll start by rearranging the order of the fields and move the title to be the last one showing. The reason we want to do this is that when you rewrite fields, you can use tokens that get values only from fields that are added before the one being rewritten. And since we want to rewrite the title field, we want the token for the username value to be present so we need to move it before the title field.

我们将从重新排列字段的顺序开始,并将标题移动到最后一个显示的位置。 我们要这样做的原因是,重写字段时,可以使用仅从在重写字段之前添加的字段获取值的令牌。 并且由于我们要重写标题字段,因此我们希望用户名值存在令牌,因此我们需要将其移动到标题字段之前。

Now that the title field is last, edit the author username field and uncheck the box Create a label and then check the box Exclude from display. You can now save the field. The reason we are excluding this field from being displayed in our View is so that we don’t duplicate it once we concatenate it to the title field.

现在标题字段是最后一个字段,编辑作者用户名字段,然后取消选中Create a label框,然后选中Exclude from displayExclude from display框。 您现在可以保存该字段。 我们排除此字段以使其不显示在视图中的原因是,一旦将其连接到标题字段,我们就不会重复该字段。

rewriting fields

Next, edit the title field and under REWRITE RESULTS, check the box Rewrite the output of this field. A new textarea should appear below where we will write the new contents of this field. If you write some gibberish in there and save the field, you’ll notice the title gets replaced by that gibberish.

接下来,编辑标题字段,然后在“ REWRITE RESULTS”下 ,选中“ Rewrite the output of this field ”框。 一个新的textarea应该出现在下面,我们将在其中编写该字段的新内容。 如果在其中写入一些乱码并保存该字段,您会发现标题已被该乱码代替。

Below this textarea, you’ll notice also some REPLACEMENT PATTERNS. These represent tokens of all the fields in the View loaded before this one (and including this one as well). So if you followed along, you’ll see there [name] and [title], among others.

在此文本区域下方,您还会注意到一些替换样式 。 这些代表在此之前(并且也包括此一)加载的View中所有字段的标记。 因此,如果您继续进行下去,将会看到[name][title]等。

What we need to do now is put these tokens in this box, wrapped with the text or markup we want. Having said that we want the username to be in parenthesis after the node title, we can add the following to the text box to achieve this:

现在我们需要做的是将这些标记放入此框中,并用所需的文本或标记包装。 前面已经说过,我们希望用户名位于节点标题后的括号内,我们可以在文本框中添加以下内容以实现此目的:

[title] ([name])

Save the field and check out the result. Now you should have the author user in parenthesis. However, it’s still not perfect. We left the title field’s Link this field to the original piece of content box checked and this is breaking the output for us a bit due to also the username having a link to the user profile page. What we want is a clean link to the node title and in parenthesis (which themselves do not link to anything), the username linking to the user profile page.

保存字段并检查结果。 现在您应该在括号中有作者用户。 但是,它仍然不是完美的。 我们保留了标题字段的Link this field to the original piece of content框” Link this field to the original piece of content选中状态,由于用户名也具有指向用户个人资料页面的链接,这使我们的输出有些中断。 我们想要的是到节点标题的清晰链接,并在括号内(它们本身不链接任何内容),用户名链接到用户配置文件页面。

So first up, add a new field called Content: Path (the path to the node). Make sure you exclude it from display, remove its label and move it before the title field. Then, edit the title field, uncheck the Link this field to the original piece of content box and replace the REWRITE RESULTS text with this:

因此,首先,添加一个名为Content: Path (节点的路径)的新字段。 确保将其从显示中排除,删除其标签并将其移到标题字段之前。 然后,编辑标题字段,取消选中“ Link this field to the original piece of content框”,然后将“ 重写结果”文本替换为:

<a href="[path]">[title]</a> ([name])

The [path] token is available from the new field we just added. And after you save, you should see already in the preview a much cleaner display of title nodes and usernames in parenthesis.

[path]令牌可从我们刚刚添加的新字段中获得。 保存后,您应该已经在预览中看到了括号中标题节点和用户名的清晰显示。

结论 (Conclusion)

In this tutorial we’ve looked at three main aspects of building Views in Drupal 7: relationships, contextual filters and rewriting fields. We’ve seen how with the use of relationships we can use information also from related entities, not just those on the base table a View is built on. Contextual filters are great for when the View needs to display content dynamically depending on various contextual conditions (such as a URL or logged-in user). Lastly, we’ve learned how to rewrite fields and build more complex ones with values taken from multiple fields. As you can see, this technique is very powerful for theming Views as it allows us to output complex markup.

在本教程中,我们研究了在Drupal 7中构建视图的三个主要方面:关系,上下文过滤器和重写字段。 我们已经了解了如何使用关系来使用相关实体中的信息,而不仅仅是使用View建立在基表上的信息。 当视图需要根据各种上下文条件(例如URL或登录用户)动态显示内容时,上下文过滤器非常有用。 最后,我们学习了如何重写字段并使用来自多个字段的值构建更复杂的字段。 如您所见,此技术对于主题化视图非常有用,因为它允许我们输出复杂的标记。

Views is pretty much the most popular Drupal module and it is highly complex. Despite its complexity, building views as a site administrator is very easy. All you need to understand is a few basic concepts and you are good to go. Developing for Views to extend its functionality or expose data to it is also an enjoyable experience. If you’d like to know more about that, you can read my tutorial on exposing your own custom module table to Views right here on Sitepoint.com.

视图几乎是最受欢迎的Drupal模块,并且非常复杂。 尽管非常复杂,但是以站点管理员的身份建立视图非常容易。 您只需要了解一些基本概念,就可以了。 开发Views以扩展其功能或向其中公开数据也是一种愉快的体验。 如果您想了解更多信息,可以在Sitepoint.com上阅读我的教程,以将自己的自定义模块表暴露给Views。

翻译自: https://www.sitepoint.com/relationships-contextual-filters-field-rewriting-views-3-drupal-7/

drupal视图创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值