Helllooo!

I'm having trouble creating a view that sorts by content type! I can only get views to order by the content type's machine name instead of the human readable name. Is there something I'm missing?

Maybe I make a custom handler?

k' thanks bye!

Comments

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Views is only able to sort by data that's actually in the database, which is the machine name. The human readable name is stored elsewhere and translated only upon display. It is not possible to sort by the human readable name with Views at this time.

terbs’s picture

I was able to accomplish this using a custom module and views handler, for anyone that's curious. Node type human names are stored in the database in the node_type table.

Below is the query portion of the module.


function views_nodehuman_views_data() {
 
  $data['node_type']['table']['group']  = t('Node: Type Human Name');
  $data['node_type']['table']['join'] = array(
    'node' => array(
      'left_field' => 'type',
      'field' => 'type',
    ),
  );
  

  $data['node_type']['name'] = array(
    'title' => t('Node: Type Human Name'),
    'group' => t('Node'),
    'help' => t('Node type human'),
    'field'  => array('handler' => 'views_nodehuman_field_nodetypename'),
    'sort'  => array('handler' => 'views_nodehuman_sort_nodetypename'),
  );
  
  return $data;
}

fourmi4x’s picture

I also stumbled upon this problem.

For those interested in an alternative approach, I used a custom module as simple as:

c_views.module file:

function c_views_views_api() {
  return array(
    'api' => 3.0,
  );
}

c_views.views.inc file:

function c_views_views_query_alter(&$view, &$query) {
    // What view is it for?
	if ($view->name=='selec_tbi_item') {
        // Precise the order using the machine names
	    $query->orderby[0] = "CASE node.type WHEN 'type1' THEN 1 WHEN 'type2' THEN 2 WHEN 'type3' THEN 3 ELSE 4 END";
	}
}