8

How can I add a new field to a view that is calculated using other fields?

Is there a way native to Views to accomplish this?


If the original view returns the row: X | Y | Z

And I have a function calculating new data: $new = do_something(X, Y, Z);

How can I include $new as a fourth column to resulting in: X | Y | Z | $new


I have looked into using a views template to accomplish this, but I'm curious as to whether there is a better way to do this using Views hooks or another module.

What templates, hooks or modules would be best suited to accomplish this?

4 Answers 4

3

I think Computed Field module is a good fit for this: http://drupal.org/project/computed_field

With this, you will be able to add a computed type field to your content type. You can do calculations based on other field's values.

If you set up the field, you just simply have to add that field into your view, like you did with x, y and z.

I used it once for a price field that was given by the user in USD and than I had to make another field with the price converted into EUR, it looked something like this, used from a custom module:

function computed_field_field_euro_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  $value = $entity->field_price;
  // do some calculations with the value of field_price
  $entity_field[0]['value'] = $value; //return your modified value to your computed field
}

You can input your code in the Drupal admin in your field's settings, or you can put it in a custom module like I did above.

1
  • 1
    Computed Field comes close, but before Views can access the data it looks like it must first be saved to the database. drupal.org/node/126522 The values calculated in the do_something() function will never be the same twice which makes pre-saving a non-option. I can see several other places to use this module though, thank you for the code snippit!
    – Citricguy
    Commented Aug 22, 2012 at 23:29
7

You could create your own views field handler in a custom module. Implement hook_views_api and hook_views_data_alter(&$data) than you can write your own field handler.

1
  • 2
    As the highest voted answer an example would be great
    – frazras
    Commented May 29, 2017 at 20:18
4

You can use hook_views_data in a custom module. It takes a little longer to implement but makes for a very modular solution because you get a VIRTUAL views field.

declare views api version in your MODULE_NAME.module file

function MODULE_NAME_views_api($module = NULL, $api = NULL) {
  return array('api' => '3.0');
}

add file MODULE_NAME.views.inc with contents

function MODULE_NAME_views_data() {
  $data = array();

  $data['node']['virtual_views_field'] = array(
    'title' => t('Virtual views field from X Y Z'),
    'help' => t('Virtual views field from X Y Z fields.'),
    'field' => array(
      'handler' => 'MODULE_NAME_virtual_views_field',
    ),
  );    
  return $data;
}

add file MODULE_NAME_handlers.inc with contents

class MODULE_NAME_virtual_views_field extends views_handler_field {

  /**
   * Overrides views_handler_field::render().
   */
  function render($values) {
    //uncomment following to see the actual values available
    //need DEVEL module for this
    //dpm($values);

    $X = $values->field_field_X[0]['raw']['value'];
    $Y = $values->field_field_Y[0]['raw']['value'];
    $Z = $values->field_field_Z[0]['raw']['value'];

    //implement your logic here and get result
    $result = $X . " " . $Y . " " . $Z;
    return $result;
  }

  function query() {
    // do not need to override the parent query.
  }
}

Your MODULE_NAME.info file should look similar to this

name = Custom Virtual Views Field 
description = takes X Y Z fields to create virtual field
core = 7.x
package = custom
dependencies[] = views
files[] = MODULE_NAME_handlers.inc
core = "7.x"

Enable your module (or clean your class registry if modifying existing/enabled module) and you should be able to see/add your virtual field called Virtual views field from X Y Z

This can be used and has the advantages of any other regular views field.

2

I think your best option will be to use the views_customfield module. This lets you define custom fields (including PHP) for a view, without having to have that field elsewhere (e.g. content type).

If you are using Drupal 7 this is now called Views PHP as some of the functionality is in the more recent Views module

3
  • 1
    Views PHP looks like it could work. I would prefer to avoid the PHP input filter though if at all possible. Thank you for the modules you listed. I'll look through them to see how they have hooked into views.
    – Citricguy
    Commented Aug 22, 2012 at 23:46
  • 2
    I would avoid it, based on the "fine print" on the Views PHP page. "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)."
    – sea26.2
    Commented Aug 22, 2012 at 23:54
  • 1
    @user908998 - I agree that generally PHP in the database is not best practice (I have been bitten by that once). But this can be useful for mockup purposes or rapid deployment. It is a lot of work to create a new views handler just to test out an idea.
    – Rider_X
    Commented Aug 23, 2012 at 4:04

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.