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

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.


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.


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.

Tags:

Views

7