Hướng dẫn control Context sử dụng Context Entity Field trong Drupal 7

Hướng dẫn control Context sử dụng Context Entity Field trong Drupal 7

When you need to dynamically display a block within a region, nothing can really beat the Context module. It allows you to define a set of conditions, that when met, executes a set of reactions. For example, you could create a context rule which adds a block to the sidebar second region (reaction) when a user is viewing an Article content type (condition).

A lot of what Context does can also be achieved using Panels. But if you're new to Drupal, and never used Panels than Context will be easier to use.

If you're new to Context then I would highly recommend you watch the two introductory videos below:

Recently I've discovered a powerful module called Context entity field. It allows you to define a condition that checks the value of a field on an entity.

Let's say you have a use-case where a block should only appear if an editor selects a tag like "Article". Using Context entity field you can create a condition that checks the Tags field if a specific term has been selected.

Fig 1.0

In this tutorial, we'll use Context entity field to display a debug message only if a specific term is selected from the Tags field.

Getting Started

Head over to drupal.org and download Context, Context entity field and Ctools. Then install Context UI and Context entity field.

For this tutorial I'll use the 7.x-1.x-devbranch.

If you use Drush, run the following two commands:

drush dl context context_entity_field-7.x-1.x-dev ctools

drush en context_ui context_entity_field

Create Context Rule

Let's first create a context rule that will be used to display a debug message.

1. Go to Structure, Context and click on Add.

2. Enter in article_blockinto the Name field.

Hướng dẫn control Context sử dụng Context Entity Field trong Drupal 7

3. In the Conditions section, click on the <Add a condition>drop-down box and select Entity field.

Fig 1.1

4. From within the Entity field condition form select Node from Entity type, field_tags from Field name and Match from Field status.

Fig 1.2

5. Then in Field value enter in the term ID which you want to match on.

Fig 1.3

Field Status

Now let's take a quick detour and talk about the "Field status" option. A field's status can be evaluated in three ways: "All value", "Empty value" or Match.

Fig 1.8

The "All value" option is useful for when you want to check if a field has been filled out. Then the "Empty value" is useful for when you want to check if a field is empty. And finally, use the Match option for when you want to match on a specific value.

This means that if a term with an ID of 1 is selected from the Tags field then the context rule will execute. Also understand, that the Match option may not work with every field out there so make sure you test it out. If you're having trouble look at the get_field_value()method.

6. From the Reactions section, select Debug from the <Add a reaction>and then click on Save.

Fig 1.4

The debug reaction will simply display a message saying that the reaction has been executed.

Test Context Rule

Now that we've created a context rule using Entity field condition let's test it out.

1. Go to Content, "Add content" and Article.

2. Enter "Test article" in the title field and select a term in the Tags field that has an ID of 1.

In my Tags vocabulary I have two terms: Article (term ID 1) and News (term ID 2).

Fig 1.5

3. If I select Article from the Tags field, the context rule will execute and I should see the "Active context: article_block" message.

Fig 1.6

Summary

The example I've shown in this tutorial is pretty basic but you get the idea behind the module. Now, before we finish up, the module comes with a second context condition called "Entity view mode".

Fig 1.9

This condition is useful for when you want to run a reaction on a specific view mode like teaser.

Bạn thấy bài viết này như thế nào?: 
Average: 9 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

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

 

Sử dụng drush để import/export DB

If the sql dump file is on a remote server, you can use this terminal command to download a copy of the sql file:

Những người làm kỹ xảo VFX - các nghệ sĩ làm kỹ xảo VFX (Visual Effect)

Những người làm kỹ xảo VFX - các nghệ sĩ làm kỹ xảo VFX (Visual Effect)

VFX dĩ nhiên góp một vai trò rất lớn trong sự thành công của các bộ phim bom tấn. Ví dụ như trong tập Avenger: Infinity War, chỉ có khoảng 80 shots phim  không qua xử lý kỹ xảo hiệu ứng trong tổng số hơn 2500 shots.

Tự xây dựng Wordpress - Theme: Building Your Own Theme

Tự xây dựng Wordpress - Theme: Building Your Own Theme

Wordpress theme step by step tutorial - part 1: Although thousands themes available, You may want to create your own theme. Make something different from others. You can customize from exist theme or build from zero

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

 

Diet con trung