Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Currently I'm uning Views PHP to reformat some fields in a view. in the documentation of views it says

While this module allows you to directly use PHP inside views which may be useful for quick and easy solutions, it is highly advisable to use regular handlers and plugins when available (or even to create one yourself).

In my particular use case, I'm trying to re-format the output of a field that is being loaded by relationship. How would I create a views handler, or a views plugin that does this?

share|improve this question
Have you considered theming the field? – David Thomas Feb 19 at 22:20
well thats the trick, I'm trying to output the field to services (in my case the field is a file). Unfortunately, the field is displayed with all of it html markup. Theming does not effect services output – user379468 Feb 19 at 22:35

1 Answer

You add your custom views handler using hook_views_data() like so:

function mymodule_views_data() {
  $data = array(
    ['my_table_name'] => array(
      'my_field_alias' => array(
        'real field' => 'my_field_id',
        'group' => t('Testing'),
        'title' => t('Testing'),
        'help' => t('Testing a thing.'),
        'field' => array(
          'handler' => 'my_custom_views_handler_test',
          'click sortable' => TRUE,
        ),
        'sort' => array(
          'handler' => 'views_handler_sort',
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_string',
        ),
        'argument' => array(
          'handler' => 'views_handler_argument_string',
        ),
      ),
    ),
  );
}

And then you declare your custom views handler:

class my_custom_views_handler_test extends views_handler_field {
  /**
   * Render some stuff.
   *
   * Data should be made XSS safe prior to calling this function.
   */
  function render($values) {
    $value = $this->get_value($values);
    return $this->sanitize_value($value);
  }
}

Almost all changes you'll be wanting to make should be made in an override of the render function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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