Move views global text field into code (attached fields and fields via relationships) I have recently done away with using views php as it is unsafe. I have read a comment by @letharion whereby he mentions that adding a php directive disable_functions = eval is a good practice to tighten up security.

I have a problem with the views global text field:


One cannot run php functions on fields
It is a bit messy to have a lot of the html markup in the database
It runs the markup through filter_xss (which is good) without giving
one the flexibility of expanding on the allowed tags.


I have read this question: Move php in views to module and would like to broaden it a bit.

I have implemented the accepted answer and it works with a slight modification:

custom.module:

function custom_views_api() {
  return array(
    'api' =&gt; 3,
    'path' =&gt; drupal_get_path('module', 'custom') . '/views',
  );
}

function custom_print_my_link($nid, $type){
  return $nid . '-' . $type;
}


custom.views.inc:

function custom_views_data() {
  $data['node']['custom_stuff'] = array(
    'field' =&gt; array(
      'title' =&gt; t('Custom link'),
      'help' =&gt; t('Provide a custom content link.'),
      'handler' =&gt; 'views_handler_field_node_custom_stuff',
    ),
  );
  return $data;
}


views_handler_field_node_link_custom_stuff:

class views_handler_field_node_custom_stuff extends views_handler_field_node {
  function construct() {
    parent::construct();
    $this-&gt;additional_fields['type'] = 'type';
    $this-&gt;additional_fields['nid'] = 'nid'; // this had to be added !!!
  }
  function query() {
    $this-&gt;ensure_my_table();
    $this-&gt;add_additional_fields();
  }
  function render($values) {
    $type = $this-&gt;get_value($values, 'type');
    $nid = $this-&gt;get_value($values, 'nid');
    return custom_print_my_link($nid, $type);
  }
};


I have set up the view with two hidden fields (nid and type) that appear before this custom field.

This works perfectly for fields attached to the node table.

My question is: How do I implement it for attached fields, and access the fields across relationships as well?
So I figured out how to accomplish a small part of what I need done.

To add an attached field (a simple text field called 'test') I did this:

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

    $this-&gt;additional_fields['test'] = array(
      'table' =&gt; 'field_data_field_test',
      'field' =&gt; 'field_test_value',
    );
  }

  /**
   * {@inheritdoc}
   *
   * Perform any database or cache data retrieval here. In this example there is
   * none.
   */
  function query() {
    $this-&gt;ensure_my_table();
    $this-&gt;add_additional_fields();
  }

  /**
   * Render callback handler.
   *
   * Return the markup that will appear in the rendered field.
   */
  function render($values) {
    $test = $this-&gt;get_value($values, 'test');
    return $test;
  }
}