Views : custom table with relationship is not queried I use a custom table to store data on my nodes, and hook on _views_data to display or sort from this table.

Everything's going fine :

/**
* Implements hook_views_data()
*/
function MYMODULE_views_data() {

  $data['MYTABLE']['table']['join'] = array(
    'node' =&gt; array(
      'left_field' =&gt; 'nid',
      'field' =&gt; 'nid',
    )
  );
  $data['MYTABLE']['MYFIELD'] = array(
    'field' =&gt; array(
      'handler' =&gt; 'MYHANDLER',
    )
  );
}

class MYCLASS extends views_handler_field {
  function query() {
    // Add 2 columns I need to the query
    $this-&gt;view-&gt;query-&gt;add_field('MYTABLE', 'COLUMN1', 'ALIAS1');
    $this-&gt;view-&gt;query-&gt;add_field('MYTABLE', 'COLUMN2', 'ALIAS2');
  }

function render($values) {
    // WORKS FINE WITH NO RELATIONSHIP
    $dates = array($values-&gt;ALIAS1, $values-&gt;ALIAS2);
    ...
    return ..
  }
}


My problem is : I can't get values when my table is called on a node-relationship relationship.

I tried

function query() {
        if(isset($this-&gt;relationship) &amp;&amp; !empty($this-&gt;relationship)) {
            $table = $this-&gt;view-&gt;relationship[$this-&gt;options['relationship']]-&gt;table_alias;
            $field = $this-&gt;view-&gt;relationship[$this-&gt;options['relationship']]-&gt;real_field;
            $this-&gt;view-&gt;query-&gt;add_field($table . '.' . $field, 'COLUMN1', 'ALIAS1');
            $this-&gt;view-&gt;query-&gt;add_field($table . '.' . $field, 'COLUMN2', 'ALIAS2');
        }
}


and 

function query() {
if (!empty($this-&gt;relationship)) {
            $this-&gt;table = $this-&gt;query-&gt;ensure_table($this-&gt;table, $this-&gt;relationship);
        }


but in render, my $values is always empty when the relationship is set.
Instead of adding the fields manually, you should populate the instance variable additional_fields. From the views documentation:


  additional fields: An array of fields that should be added to the query for some purpose. The array is in the form of: array('identifier' =&gt; array('table' =&gt; tablename, 'field' =&gt; fieldname);


Looking through some of the implementations out there indicates that your code should look something like this:

class MYCLASS extends views_handler_field {
  function construct() {
    parent::construct();

    $this-&gt;additional_fields['ALIAS1'] = array(
      'field' =&gt; 'COLUMN1',
      'table' =&gt; 'MYTABLE',
    );
    $this-&gt;additional_fields['ALIAS2'] = array(
      'field' =&gt; 'COLUMN2',
      'table' =&gt; 'MYTABLE',
    );
  }

  function query() {
    $this-&gt;ensure_my_table();
    $this-&gt;add_additional_fields();
  }

  function render($values) {
    $alias1 = $this-&gt;get_value($values, 'ALIAS1');
    $alias2 = $this-&gt;get_value($values, 'ALIAS2');
    ...
  }
}