21

This seemed easy at first but I'm losing my hair on this.

I simply want to add a custom view handler that uses no table.

I tried to do as views php proposed, $data['views']['mycustomfield'] in hook_views_data, but still

mycustomfield colum not found.

2 Answers 2

26

Actually its easy. Just look at the implementation Views Global handlers which is available in views.views.inc.

Below is the example how you can add field handler without having a table,

  1. Specify your custom handlers in hook_views_data like below,

    /**
     * Implements hook_views_data().
     */
     function my_module_views_data() {
       $data['custom']['table']['group'] = t('Custom');
       $data['custom']['table']['join'] = array(
         // #global is a special flag which let's a table appear all the time.
         '#global' => array(),
       );
    
       $data['custom']['custom_handler'] = array(
         'title' => t('Custom Handler'),
         'help' => t('Custom Handler.'),
         'field' => array(
           'handler' => 'views_handler_custom_handler',
         ),
       );
    
       return $data;
    }
    
  2. Create a inc file for your handler an put your logic there. something like below Here the file name would be views_handler_custom_handler.inc

    /**
     * A handler to provide a field that is completely custom by the administrator.
     *
     * @ingroup views_field_handlers
     */
     class views_handler_custom_handler extends views_handler_field {
       function query() {
         // do nothing -- to override the parent query.
       }
    
       function option_definition() {
         $options = parent::option_definition();
    
         // Override the alter text option to always alter the text.
         $options['alter']['contains']['alter_text'] = array('default' => TRUE);
         return $options;
       }
    
       function options_form(&$form, &$form_state) {
         parent::options_form($form, $form_state);
    
         // Remove the checkbox
         unset($form['alter']['alter_text']);
         unset($form['alter']['text']['#dependency']);
         unset($form['alter']['text']['#process']);
       }
    
       function render($values) {
         // Render your content.
         return 'Sample';
       }
    }
    
  3. Specify this new handler file in your module's info file

  4. Clear the caches

Now the new field handler will be there in the field list.

3
  • 1
    Thx ! Exactly the 2 things I needed : override the query by specifying it empty, and join '#global'. Commented Jul 24, 2013 at 14:05
  • Is there a way to filter on these results? For instance, I've created a custom Field Handler for calculating a special "status" based on a user's relationship to an entity. I'd like to be able to filter via that status. Any ideas? Commented Jan 14, 2014 at 18:41
  • Thanks, this is what I needed! But it seems that I cannot enable views relationships for a field handler like this. drupal.stackexchange.com/questions/166734/… Any idea?
    – donquixote
    Commented Jul 26, 2015 at 18:06
1

To be complete: you would need to add hook_views_api too

function my_module_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'my_module'),
  );
}
1
  • 1
    To be complete: You have to include handler in .info file files[] = views_handler_custom_handler.inc
    – Fawwad
    Commented May 26, 2016 at 12:47

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.