0

How do I alter the output of an existing views handler?

I want to alter views_handler_field_user_picture() to get the link to the user profile, not the themed picture.

See alter existing handlers (handler overrides). I expect a link like this http://cdn.sstatic.net/drupal/img/logo.png.

function mymodule_views_data_alter(&$data) {
  $data['users']['picture']['field']['handler'] = 'my_custom_handler';
}

module .info

files[] = user_picture_formatter.inc

and user_picture_formatter.inc is like this

<?php
class my_custom_handler extends views_handler_field_user_picture {
  function render($values) {
    if ($picture_fid = $this->get_value($values)) {
      $picture = file_load($picture_fid);
      $picture_filepath = $picture->uri;
    }
    else {
      $picture_filepath = variable_get('user_picture_default', '');
    }
    return $picture_filepath;
  }
}
?>
7
  • 1
    quick suggestion: you need to override this handler views_handler_field_user_picture in order to get user profile image link
    – Aboodred1
    Commented Feb 18, 2013 at 7:31
  • @Aboodred1, is there any hook available ? I dont prefer hacking the core anyway.
    – niksmac
    Commented Feb 18, 2013 at 7:35
  • You don't need to hack the core, you need to create a new handler and extend this class views_handler_field_user_picture and then call the new handler instead
    – Aboodred1
    Commented Feb 18, 2013 at 7:54
  • @Aboodred1 How to do that?Do you have any tutorials ? or guidelines ?
    – niksmac
    Commented Feb 18, 2013 at 7:55
  • 1
    I'll try to write the answer for you, give me few minutes.
    – Aboodred1
    Commented Feb 18, 2013 at 8:00

1 Answer 1

4

The short answer is that you can't alter the output of a views handler. But what you can do is:

  • Create your own handler that is a subclass the handler you want to overwrite.
  • Add a render method - which will generate the output (this is where your alter code is)
  • Use hook_views_data_alter to make views use your handler instead where needed.

This will get the wanted effect, where you can alter the output of the original views handler, but in reality to make views use your custom handler.

In code it could look something like this.

class my_custom_handler extends views_handler_field_user_picture {
  function render($values) {
    ...
  }
}

function my_module_views_data_alter(&$data) {
  $data[...]['handler'] = 'my_custom_handler';
}

This code is for Drupal 7, but the method can be used in both Drupal 6 and 7. For Drupal 7 you should create the class in it's own file and add the file to the module's .info file, to make Drupal autoload the file with the class when needed.

2
  • See my edit. i cant get it working.
    – niksmac
    Commented Feb 18, 2013 at 10:44
  • Hi i've managed to fix it. I accidentally missed an object.
    – niksmac
    Commented Feb 18, 2013 at 10:51

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.