Hướng dẫn sử dụng Drupal Context.module

Hướng dẫn sử dụng Drupal Context.module

"If ..., then ..." is one of the most commonly used phrases in the world of software development and, I suppose, in life:

  • If the sky is blue, then it probably won't rain.
  • If I learn Drupal, then I'll make lots of money.
  • If the webpage URL includes "services" in the path, then display the block called Services.

In this blog, we are going to introduce to you to the Context module, which puts the power of "If ..., then ..." into your hands.

Sample scenario

Here's the example we're going to use to introduce Context:

Imagine you want this block to show in the first sidebar region on the home page but in the footer area on all other pages.

However, your block is generated by a module. A good example is  the "Who's online" block. Our problem is that it's not easy to duplicate a block that is created by a module.

Here's how we'd use Context to solve this problem ...

Install the required modules

There are two modules required for Context to work.

media_1359387875369.png

Once the modules are on your server, you will be able to see them in your module list.

  • Click on Modules in the black admin menu bar.
  • Check the box next to Context and Context UI modules.
  • Click Save configuration.
  • Drupal is smart enough to know which modules are required and will now prompt you with a list of modules it will enable for you. They include the Ctools module.
  • Click continue to finish enabling the modules.

Create the homepage context

media_1359386656820.png

In our scenario, we want the "Who's online" block to appear in a sidebar on the home page but in the footer on all other pages. To accomplish this, we will create a context for the block on the home page.

  • Click on Structure and then on Context
  • Click on Add

Name your context

media_1359389148823.png

  • Name your context "homepage"

Configure the "if"

media_1359389238870.png

  • In the Conditions dropdown, select Path
  • Observe that "Path" now appears below the dropdown
  • Type <front> in the text box.

Note: this path condition is the same as the default Pages visibility setting on the block configuration.

Configure the "then"

media_1359387249340.png

  • In the Reactions dropdown, select Blocks and observe that "Blocks" appears below the dropdown.
  • Scroll down and check the box for the Who's online block
  • Click +Add next the Sidebar first region

Check the settings

media_1359387621961.png

  • Observe that the block in no longer in the list and is showing in the Sidebar first region.
  • Scroll down and save.

Observe the placement of the block

media_1359387956422.png

Notice that the block appears first, above the Search and Navigation blocks. This is important. In this scenarion, if you want the Search block to appear above the "Who's online" block, you will need to add the Search block to the homepage context and position it above the "Who's online" block ... just as you would do in the default block configuration interface. Then you will want to remove the Search block from the default block configuration.

Configure the block for the rest of the site

media_1359388226218.png

We could create another context that displays the "Who's online" block in the footer for all other pages or we can use the default block system. For demonstration purposes, let's use the default block system.

  • Click on Structure and then on Blocks
  • Click configure next to the "Who's online" block
  • Select the Footer first column region
  • Select "All pages except those listed"
  • Type <front>
  • Save

Observe the block in the Footer first column

Hướng dẫn sử dụng Drupal Context.module

  • Notice when you click on a page on your site, the block does not show in the first sidebar but now shows in the footer.

More on Context

We have another Context tutorial showing how to place a block on different content types.

As you can imagine, using the Context module will require some exploration on your part. Don't be afraid to play with the conditions and reactions to see what you can do and if the options are what you need.

Also, it is important to plan your block layout if you plan on using Context to manage your blocks in addition to or instead of the default block system. Why? Well ... as you saw, the "Who's online" block appeared above the Search block and there wasn't a way to change that except to add the Search to the homepage context OR switch and use the Context to create the footer blocks instead of the sidebar.

Another reason is, if you plan to use Context to manage your blocks, please note there isn't a way to order your contexts (like we can order our blocks) and if you have more than one context feeding one region on one page, you won't have an easy way to reorder the groups of blocks managed by the contexts.

Have fun with this module. It is very powerful and once you get the hang of it.

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

 
Siri: Chỉnh sửa hình ảnh bằng... giọng nói

Siri: Chỉnh sửa hình ảnh bằng... giọng nói

Văn phòng Bản quyền thương hiệu Mỹ (USPTO) vừa tiết lộ một bản quyền mà Apple đã đăng kí, cho thấy trong tương lai tính năng Siri (trợ lí ảo thông minh) sẽ có khả năng điều khiển các tấm hình trên máy bằng giọng nói.

 
Video giới thiệu Securing your Data trong Drupal

Video giới thiệu Securing your Data trong Drupal

As data breaches are on the rise, protecting sensitive data is more important than ever - for you and your customers

Theo dõi đơn hàng trong Drupal Commerce với Google Analytics

Theo dõi đơn hàng trong Drupal Commerce với Google Analytics

Knowing what your customers are doing before spending money in your Drupal Commerce store can be valuable information for your business.

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

 

Diet con trung