3

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' => 3,
    'path' => 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' => array(
      'title' => t('Custom link'),
      'help' => t('Provide a custom content link.'),
      'handler' => '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->additional_fields['type'] = 'type';
    $this->additional_fields['nid'] = 'nid'; // this had to be added !!!
  }
  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();
  }
  function render($values) {
    $type = $this->get_value($values, 'type');
    $nid = $this->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?

3
  • For what it's worth, there's modulate which attempts to automate this process. Not sure how well it will help, and it doesn't appear to be maintained, but it might still contain something useful for you.
    – Letharion
    Commented Oct 28, 2014 at 11:22
  • CCK fields? So this is 6 question?
    – Mołot
    Commented Oct 28, 2014 at 13:10
  • @molot This is a Drupal 7 question, I used term CCK to describe fields that are attached to entities and not in base table. Commented Oct 28, 2014 at 15:17

1 Answer 1

1

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->additional_fields['test'] = array(
      'table' => 'field_data_field_test',
      'field' => 'field_test_value',
    );
  }

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.