Wednesday 13 July 2016

How to sort Views results based on Content Type display name instead of machine name in Drupal 7?

We can join 'node' table with 'node_type' table in views_query_alter.  And, add the Node Type Display Name to the fields list so that it can be used to sort.
$join = new views_join;
        $join->construct('node_type',
                    'node',  // left table
                    'type',   // left field
                    'type'   // field
            );
        // Add join to query; 'node' is the left table name
        $view->query->add_relationship('node_type', $join, 'node');
        // Add fields from table (or where clause, or whatever)
        $view->query->add_field('node_type', 'name', 'type_name');
        $orderby = $view->query->orderby;
        if(isset($orderby[0]['field']) && $orderby[0]['field'] == 'node_type'){
        $view->query->orderby[0]['field'] = 'type_name';
        }

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.