I have a custom table defined in hook_views_data(). One of my fields is an integer, which maps to some defined constants. When they are queried and displayed by views, I'd like to change them to their string name that's represented by the integer.

So, my question is how can I manipulate values returned by views?


If you're using hook_views_data, you can also specify a custom field handler for the output, using a function in the field handler such as views_handler_field->render($values)

See also: views_handler_field for the base class that is extended by the field handler.

That way your field in your custom table can have its own field handler.

  • I think this is what I want. I'm trying to wrap my brain around it. Do you have a good tutorial you recommend? – vintorg Jan 18 '13 at 4:45
  • @vintorg Yes, the best place to start there is to look at another module, e.g Drupal Commerce and see how it adds its own field handlers for it's data. – David Thomas Jan 18 '13 at 23:30

You can theme (you dont want to change the value stored in the DB, just change its displayed value) the view row value by using a template suggestion: enter image description here

I prefer using a preprocessor function to do this on the field. This blog post covers how to do this for a specific view_name and field.

From the blogpost:

There is an issue to let Drupal 7 and 8 themes implement a preprocess/process function per view as it is possible in Drupal 6, but it is in development (see issue).

Until it is fixed there is a way to achieve that behaviour:

  * Generic preprocess that is still working on D7
function MYTHEME_preprocess_views_view_fields(&$vars) {
  if (isset($vars['view']->name)) {
    $function = __FUNCTION__ . '__' . $vars['view']->name . '__' . $vars['view']->current_display;

    if (function_exists($function)) {

  * Then the specific preprocess that worked without the above code for D6
function MYTHEME_preprocess_views_view_fields__VIEWNAME__DISPLAY(&$vars) {
  // my specific preprocess code

There's also like views_custom_styles (sandbox project) and views_php which will let you do the same thing -- but for what you're asking (to my knowledge) you're gonna have to write code.

The preprocessor example I give is a clean solution re-using the Theme API (suggestions) and hooks.

  • Would this not tie me to a theme? I want to control this from my module. – vintorg Jan 18 '13 at 4:40
  • you can implement preprocess functions in a module ... see comments here: api.drupal.org/api/drupal/modules!system!theme.api.php/function/… i've used it because i too prefer stuff like this to be in a module vs a theme (where I mainly do CSS stuff)... – tenken Jan 18 '13 at 6:11
  • Why do you prefer to handle this on the presentation side and not in the views code? – vintorg Jan 18 '13 at 6:45
  • Im confused by your question ... Views is just an SQL builder. If your SQL isnt storing the information you are going to display (in raw/native format) -- then ultimately you're changing just the presentation of the data. A views template will let you do this, so will a views_display_style function, so will a theme_preprocessor function -- these all alter the presentation of something. I say "let View get me the value from SQL. I want the theme layer to be responsible in changing how it looks to the end user" ... hook_preprocess is just a simple Drupal thing, Views is complex. – tenken Jan 19 '13 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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