Hướng dẫn tạo page Title từ the Views Exposed Filter Term

Hướng dẫn tạo page Title từ the Views Exposed Filter Term

Suppose you're using taxomomy terms in exposed filters, and you want the resulting page to display one of the the terms as a title. Taxonomy term pages will give this to you very easily, but sometimes the taxonomy term page is not the right solution.  Here's how you can display the taxonomy term as the title of your views page.

So, for example, the user selects Comparative Literature from the exposed filter. Along with displaying all nodes tagged with Comparative literature, you also want the page title to be Comparative Literature. To make this happen, you'll need to dig out your PHP skills and add a snippet of code to your view.

Hướng dẫn tạo page Title từ the Views Exposed Filter Term

Some functionality you’ll need

Get the view object:

$view = views_get_current_view();

Get the value from the exposed filter:

$tid = $view->exposed_input['field_my_tax_tid']; 

In this case, ‘field_my_tax_tid’ is the machine name for the exposed filter field. You can find this field name in the URL after executing the exposed filter.

Get the term object and the human-readable name:

$term_obj = taxonomy_term_load($tid);
$title = $term_obj->name; 

Check that the term ID is set and valid

if (isset ($view->exposed_input['field_my_tax_tid'])) {
 $tid = $view->exposed_input['field_my_tax_id'];
 if (is_numeric($tid))

Set up the values to return

     $term = $tid;
     $term_obj = taxonomy_term_load($tid);
     $title = $term_obj->name;

Set the view title object

$argument->validated_title = $title;

Return the orginal term

return($term); 

If this term had not been set by the exposed filter the term should return all.

Code snippet example

$view = views_get_current_view();
$term = 'all';
$title = 'Default Title';
if (isset ($view->exposed_input['field_my_tax_tid'])) {
 $tid = $view->exposed_input['field_my_tax_tid'];
 if (is_numeric($tid)){
     $term = $tid;
     $term_obj = taxonomy_term_load($tid);
     $title = $term_obj->name;
 }
}
$argument->validated_title = $title;
return($term); 

Putting your code into the view

For your view, you'll need to adjust this code. Start by replacing Default Title with the title from your view, and field_my_tax_tid with the exposed filter field for your view.

  1. From with the Views UI under Conditional Filters add Content: Has taxonomy term ID.

  2. In the form under the section WHEN THE FILTER VALUE IS NOT IN THE URL select Provide default value.

  3. In the dropdown, select PHP Code. (Note: you must be logged in as user 1 for this option to be available.)

  4. Copy and paste your code snippet into the text block.

  5. Select Override title and place a %1 in the text field.

Save your view, or use Update preview to check out your result.

Troubleshooting

If you have the Devel module enabled, you can use the dpm() function to get other information such as field and variable names.

If "PHP Code" is not appearing as a selection on the view Contextual Filter Configuration form, enable the "PHP Filter" module.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ả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

 
Hướng dẫn 3 cách prompt for user input in Drush

Hướng dẫn 3 cách prompt for user input in Drush

Drush is awesome. It makes Drupal development much easier. Not only that it comes already packed with a bunch of useful commands, but you can declare your own with great ease.

5,3 triệu máy Mac được bán ra vào Quý IV

5,3 triệu máy Mac được bán ra vào Quý IV

Doanh số của Apple có vẻ bị giảm, tuy nhiên trong quý này nó bắt đầu tăng trở lại. Theo báo cáo phân tích của Gene Munster thì đến cuối tháng 12 năm nay, doanh số bán ra của Apple có thể đạt tới 5.3 triệu máy.

4 Hình thức "đội lốt" của Spam trên Facebook

4 Hình thức "đội lốt" của Spam trên Facebook

Bạn nên tránh xa những thứ tương tự như thế này để mang lại một môi trường mạng xã hội "lành" cho bạn bè và người thân.

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

 

Diet con trung