Sử dụng Search API Sorts trong View Drupal 7

Graph Query Language do Facebook tạo ra từ năm 2012

Average: 9.5 (13 votes)

Làm thế nào để tương tác với hệ sinh thái của React JS hiệu quả

Average: 9.4 (13 votes)

Sử dụng Search API Sorts trong View Drupal 7

It's been a while since I've written a post here (especially, Drupal-related). But today I have something interesting to share.

There's a module called Search API sorts (https://drupal.org/project/search_api_sorts) that provides custom sorts and a global sort block for Search API. The module itself is ok, but has one big flaw: all settings are global and will get applied to every view you want to have the sort on. But what if you want to have different default sorts for different views? For example, View 1 to be sorted by created date and View 2 by radioactivity field value? Well, as you may have guessed, regular sitebuilder won't do it via UI, you need to dig deep into code so I'll guide you through then.

If we'll look at the code of search_api_sorts.module we'll see a function that's responsible for applying sorts on the current query, it's called search_api_sorts_search_api_query_alter. The function itself, implements hook_search_api_query_alter() hook, which means, we can easily re-implement that function in our custom module with a few tweaks to reach our goal.

So what we actually need is to create a custom module that'll have less weight than search_api_module (-1) or have same weight but it's name should start on a letter that comes before S. We need this to make sure, that the hook we defined in our custom module kicks in earlier than the hook defined in search_api_sorts.

So we just take search_api_sorts_search_api_query_alter, copy it and put into our custom example_module renaming it accordingly: 

<?php

/**

* Implements hook_search_api_query_alter().

*/

function example_module_search_api_query_alter(SearchApiQueryInterface $query) {

if (!user_access('use search_api_sorts')) {

return;

}


// There's already an existing sort, so abort!

$existing = $query->getSort();

if (!empty($existing)) {

return;

}


$search_sorts = search_api_sorts_search_sorts($query->getIndex()->machine_name);

if (empty($search_sorts)) {

return;

}

$default_sort = _search_api_sorts_get_default_sort($search_sorts, $query->getKeys());


// alter sort field and sort order

$sort = $default_sort->field;

$params = drupal_get_query_parameters($_GET, array(

'q',

'page'

));

if (isset($params['sort']) && !empty($params['sort'])) {

$sort = $params['sort'];

}


$order = $default_sort->default_order;

if (isset($params['order']) && !empty($params['order'])) {

$order = $params['order'];

}


if (!empty($order) && !empty($sort)) {

$query->sort($sort, $order);

}


// Static save current search query

$_query = &drupal_static(__FUNCTION__, array());

$_query = $query;

}

Now, we need to modify it a bit to suit our needs:

1) We need to check if the current view is the view we want to put custom sort on, like this:

<?php

/**

* Implements hook_search_api_query_alter().

*/

function example_module_search_api_query_alter(SearchApiQueryInterface $query) {


if (!user_access('use search_api_sorts')) {

return;

}


$view = views_get_current_view();

if (empty($view) || $view->name != 'articles_list') {

return;

}


...


}

2) We need to tweak $search_sorts variables and set our field (field_radioactivity) as a default sort, like this:

foreach ($search_sorts as $key => $sort) {

// We set our desired field as a default sort

if ($sort->field == 'field_shared_radioactivity') {

$search_sorts[$key]->default_sort = 1;

$search_sorts[$key]->default_sort_no_terms = 1;

$search_sorts[$key]->active = TRUE;

$search_sorts[$key]->default_order = 'DESC';

}

// We reset default sorts for other fields

else {

$search_sorts[$key]->default_sort = 0;

$search_sorts[$key]->default_sort_no_terms = 0;

$search_sorts[$key]->active = FALSE;

}

}

So in the end, our modified example_module_search_api_query_alter would look like this:

<?php

/**

* Implements hook_search_api_query_alter().

*/

function example_module_search_api_query_alter(SearchApiQueryInterface $query) {


if (!user_access('use search_api_sorts')) {

return;

}


$view = views_get_current_view();

if (empty($view) || $view->name != 'articles_list') {

return;

}


if (!empty($_GET['sort'])) {

return;

}


// There's already an existing sort, so abort!

$existing = $query->getSort();

if (!empty($existing)) {

return;

}


$search_sorts = search_api_sorts_search_sorts($query->getIndex()->machine_name);


foreach ($search_sorts as $key => $sort) {

if ($sort->field == 'field_shared_radioactivity') {

$search_sorts[$key]->default_sort = 1;

$search_sorts[$key]->default_sort_no_terms = 1;

$search_sorts[$key]->active = TRUE;

$search_sorts[$key]->default_order = 'DESC';

}

else {

$search_sorts[$key]->default_sort = 0;

$search_sorts[$key]->default_sort_no_terms = 0;

$search_sorts[$key]->active = FALSE;

}

}


if (empty($search_sorts)) {

return;

}

$default_sort = _search_api_sorts_get_default_sort($search_sorts, $query->getKeys());


// alter sort field and sort order

$sort = $default_sort->field;

$params = drupal_get_query_parameters($_GET, array(

'q',

'page'

));

if (isset($params['sort']) && !empty($params['sort'])) {

$sort = $params['sort'];

}


$order = $default_sort->default_order;

if (isset($params['order']) && !empty($params['order'])) {

$order = $params['order'];

}


if (!empty($order) && !empty($sort)) {

$query->sort($sort, $order);

}


// Static save current search query

$_query = &drupal_static('search_api_sorts_search_api_query_alter', array());

$_query = $query;

}

3) This function will only get applied on our desired view, if the view is different example_module_search_api_query_alter exists and the default search_api_sorts_search_api_query_alter kicks in, so your customisation breaks nothing if the view is not the one you need.

Hope this one is helpful, let me know in comments and enjoy!

Bạn thấy bài viết này như thế nào?: 
No votes yet

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

 
Mansour bin Zayed Al Nahyan - chủ tịch tập đoàn Abu Dhabi United Group, sở hữu Man City
Man City khát khao vô địch UEFA Champions League 2020

Man City cùng các nhà tài trợ không ngại thao túng hợp đồng. Khi Chumillas hỏi Pearce rằng liệu họ có thể thay đổi thời gian ký hợp đồng tài trợ,

Đuôi tên miền độc quyền được kỳ vọng sẽ giúp các công ty dịch vụ tài chính đối phó tốt hơn với bọn tội phạm trên mạng.
Chống phishing bằng đuôi tên miền độc quyền

Các công ty dịch vụ tài chính đang tìm cách thâu gom những đuôi tên miền độc quyền mới trong nỗ lực đối phó với bọn tội phạm trên mạng đang gây thiệt hại không ít cho ngành công nghiệp này.

30 phút để cài đặt website Drupal 8 trên Simplytest
30 phút để cài đặt website Drupal 8 trên Simplytest

Simplytest.me is a web site created and maintained by Patrick Drotleff.