Making a custom views field handler click sortable (getting an error when clicked) When I make my custom handler click sortable and subsequently click the label in the table heading of the view I get the following error:


  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'unknown' in
  'order clause'


My handler is defined as below. This is a render only handler that performs a calculation on some other field data and outputs. 

UPDATE:

Am I right in thinking that because this data is not stored in the database that I can't make it click sortable because there is no column to order by in the SQL statement that is generated? 

Is there a way around this? Any help appreciated. 

function rate_calc_views_data() {

$data = array();

$data['node']['rate_calc_handler_result'] = array(
    'title' =&gt; t('Standard Deviation'),
    'help' =&gt; t('Calculates the voting result standard devaition'),
    'field' =&gt; array(
      'handler' =&gt; 'rate_calc_handler_result',
      'click sortable' =&gt; TRUE,
    ),
    'sort' =&gt; array(
       'handler' =&gt; 'views_handler_sort',
     ),
     'filter' =&gt; array(
       'handler' =&gt; 'views_handler_filter',
     ),
);  return $data; }
Because this data is not stored in the database (it's just a dummy field that returns the result of some calculations) you can't make it click sortable as there is no database column to 'order by' in the SQL statement generated by the view. 

There must be a way around this but it's likely quite complicated and beyond me at the moment.

If you have a custom handler that does something to change the way you display existing field data you can make it click sortable by overriding the click_sort($order) function in your handler class. 

My mymodule.views.inc now looks like the below (where 'real field' is the name of the field included in the view, before the custom field created by my handler, but excluded from display)

&lt;?php 

function mymodule_views_data() {

    $data = array();

    $data['node']['mymodule_handler_myname'] = array(
        'title' =&gt; t('Score'),
        'help' =&gt; t('Makes score values human readable'),
        'real field' =&gt; 'value',
        'field' =&gt; array(
          'handler' =&gt; 'mymodule_handler_myname',
          'click sortable' =&gt; TRUE,
        ),
    ); 

    return $data;  

}

?&gt;


and my mymodule_handler_myname.inc looks like

&lt;?php

class mymodule_handler_myname extends views_handler_field {

    /**
    * Loads additional fields.
    */
    function query() {

    }  

    function click_sort($order) {

        $this-&gt;query-&gt;orderby[] = array(
            'field' =&gt; 'field_name', // here may be a field, or some SQL clause e.g. FIELD(str, str1, ...)
            'direction' =&gt; strtoupper($order),
        );

    }

    /**
    * Renders the field handler.
    */
    function render($values) {       

        if (isset($values-&gt;field_name)){

            switch ($values-&gt;field_name){
                case 2:
                    $vote = 'Yes';
                    break;
                case 0:
                    $vote = 'No';
                    break;
                default:
                    $vote = 'Maybe';
                    break;
            }

            return t($vote);

        }
    }
}

?&gt;


This wrecked my head for a full day so hopefully it's helpful to someone in the future.