drupal views dynamic filter

偶尔善良 提交于 2020-01-15 12:27:42

问题


I have a Drupal website where I post tournament results. I have created a content-type where i write the result for 1 person in the tournament. So the fields are like: Date of tournament, player name, final position. If 10 people played in the tournament, I create this content for each player.

Now, I would like to create a table with views, to list all the players in the tournament, and the various info, like player name/final position. I can do that pretty easy by adding the fields and sorting criteria in views. But my problem is, what I should do next time we have a tournament. I would like to just use the same view settings, but without having to clone the previous view, just to change the date filter-criteria. So, I need the user to be able to view tournament page, and then see the results for that specific tournament. Then if the user go to see a different page from another tournament, then only those results will show up. And I wish to do it in a clean way, where I don't have to clone the view and just change date.


回答1:


You can achieve this by making a view with a contextual filter. The contextual filter will handle a taxonomy or node reference so that you can use only 1 dynamic view. But in order to make this work, you might want to adjust your content type a little so that it's easier for your view to filter them:

Your current content type has all the information of a contestant/player (Date of tournament, player name, final position), so it would be better to name that content type Contestant. Then create a new taxonomy or content type: Tournament which will handle the information of a Tournament. Keeping these separated is a better approach.

So let's say you have a taxonomy: Tournament taxonomy with all your tournaments set up. Then you can add this as a term reference field in your content type: Contestant. (Add a new term reference field: "Tournament" with widget: select list.)


Create your view:

  • Filter criteria -> Content:Type (=Contestant)

  • Page settings -> Path: /tournaments/%

  • Contextual filter -> Content: Has taxonomy term ID

  • Override title -> %1


For each taxonomy term (Tournament) you have this view now has a page. So when for example you go to http://yourdrupal/tournaments/1 it will filter your view only for that tournament.

I attached an image so that you can see how I configurated my view



来源:https://stackoverflow.com/questions/25344730/drupal-views-dynamic-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!