Quantcast
Channel: Question and Answer » hook-views-query-alter
Viewing all articles
Browse latest Browse all 26

hook_views_query_alter() order by taxonomy term name

$
0
0

I am using Drupal 7 with dataTables and everything works great except that the sort by taxonomy term is not working properly. The data is sorted by tid instead of term name so I was wondering if there is a way to achieve this programatically by using hook_views_query_alter and alter the $query.

function datatables_views_query_alter(&$view, &$query) {
    $field_to_avoid = array('edit_node', 'delete_node');
    if (($view->style_plugin->definition['name'] == 'datatables') && (in_array('sSearch', array_keys($_GET)))) {
        $string = $_GET['sSearch'];
        $group = count($query->where) + 1;

        if (!empty($string)) {
            foreach ($view->field as $field_name => $field) {
                if (!in_array($field_name, $field_to_avoid)) {
                    if (($field_name != 'title')) {
                        $join = new views_join();
                        $join->table = $field->table;
                        $join->field = 'entity_id';
                        $join->left_table = 'node';
                        $join->left_field = 'nid';
                        $join->type = 'LEFT';
                        $query->add_relationship($field->table, $join, 'node', null);
                        $query->add_where($group, $field->table . '.' . $field->real_field, '%' . db_like($string) . '%', 'LIKE');
                    }else {
                        $query->add_where($group, $field->table . '.' . $field->real_field, '%' . db_like($string) . '%', 'LIKE');
                    }
                }
            }
        }

        $query->where[$group]['type'] = "OR";
        $query->distinct = TRUE;


    }

    $order_by = (array_key_exists('order', $_GET)) ? $_GET['order'] : '';
    $order = (array_key_exists('sort', $_GET)) ? $_GET['sort'] : '';

    foreach ($view->field as $field_name => $field) {
        if (!in_array($field_name, $field_to_avoid)) {
            if ($field_name == $order_by) {
                $query->add_orderby($field->table, $field->real_field, strtoupper($order));
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 26

Trending Articles