Hướng dẫn tạo Contextual Filters và Arguments trong Drupal 7

Hướng dẫn tạo Contextual Filters và Arguments trong Drupal 7

Sometimes the name you give something is really important. 

In Drupal 6, a key feature in Views was called "arguments". This term made perfect sense to developers, but caused plenty of raised eyebrows amongst news users.

Both the name and location of this feature has changed in Drupal 7. Arguments are now called "Contextual filters".

Here's an introduction to arguments and contextual filters. We'll show you why they are so useful, whatever they may be called.

Where are Arguments or Contextual Filters?

In the Drupal 6 views screen, Arguments were right in the center of the page:

Hướng dẫn tạo Contextual Filters và Arguments trong Drupal 7

With Drupal 7, the Arguments area was moved and re-named. There's now a whole, hidden area of the Views screen that is simply called "Advanced":

media_1400259431382.png

Under that Advanced tab is the "Contextual Filters" area. That is where Arguments have moved to.

media_1400259618101.png

Examples of Contextual Filters: Default Views

Several of the default Views contain contextual filters and would make good starting points to learn more.

Go to Structure > Views and enable both the Archive and Glossary views to see them in action.

The Archive view dynamically organizes content into the month it was published. A normal filter would only allow us to show content from certain months. A contextual filter changes which months it shows, based on the page you're viewing.

media_1401280802586.png

Here's the contextual filter that makes that possible:

media_1401280670506.png

The Glossary view is similar to the Archive, but it dynamically sorts content by the first letter of the title.

An ordinary filter could show all content that starts with A or B, but a contextual filter is flexible enough to show any of those, depending on the context

media_1401280748143.png

Here's the contextual filter for the Glossary:

media_1401280708880.png

Examples of Contextual Filters: Fields

One of the most common examples of a contextual filter is a calendar created with Views:

media_1401279238647.png

In order to functional correctly, the calendar needs a Contextual Filter which pulls from a Date field.

A simple filter would not suffice here because it's not flexible enough. The user can change between June, July, August and other months. They could even change to view weeks, days or years. Only a contextual filter can adapt automatically to those different displays.

media_1401278541602.png

Examples of Contextual Filters: Users

A second example of contextual filters involves creating a personalized newsfeed for users. In the View below, the users only see movies that are in genres they have selected:

media_1401279615605.png

This is possible because we've added a contextual filter for the user. The view is

An ordinary filter would allow us to choose "Action", "Comedy" or "Romantic" movies, which are set up as taxonomy terms.

However, a contextual filter is flexible enough so that we can dynamically change those taxonomy terms based on the user.

media_1401279683537.png

Examples of Contextual Filters #4: Nodes

This third and more complex example is part of our video class on Views.

In this part of the class, Rod is building a clone of IMDB.com. He wants to automatically show a list of actors who have appeared in the movie that's showing on the page.

Again, a simple filter would suffice to show a list actors, but not one that can dynamically change based on the main node being displayed. Watch the video to see how it's done:

Drupal 6 Users

If you are still on Drupal 6, Mustard Seed Media have a very useful video for you:

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Facebook quyết định về những nội dung họ muốn người dùng xem

Facebook quyết định về những nội dung họ muốn người dùng xem

Cựu quản lý Facebook tố thuật toán của công ty xếp nội dung dựa trên sự tương tác, khiến bài đăng gây sốc có thể được ưu tiên lên đầu News Feed.

Lucidly Drupal: Setting up Ubuntu 10.4 Lucid LAMP stack for your Drupal site

Lucidly Drupal: Setting up Ubuntu 10.4 Lucid LAMP stack for your Drupal site

We're frequently setting up servers for development, for staging, for production. I've lately preferred the Debian flavor of Linux, but up until now that had been something of a problem because Debian and Ubuntu did not include the higher-quality php5-gd library, which meant that you either had to compile your own PHP, pull from an alternative source host, or cope with substandard image resizing with limited processing features.

Hướng dẫn Creating a Custom Filter trong Drupal 8

Hướng dẫn Creating a Custom Filter trong Drupal 8

In this article, we will create a custom filter for Drupal 8, one that replaces a pattern and adds the required CSS to the page

Công ty diệt chuột T&C

 

Diet con trung