Hello,

I have two separate fields in a View:

1. Miles on Engine at Time of Purchase/Delivery (field_engine_hours_at_delivery)
2. Miles added to Engine After Purchase/Delivery (field_util_agg_engine_hours)

(1) is a standard integer field. (2) is fed by an aggregation of many other content items. My view is set to display these two values in a table, side by side.

I would like to now add those two fields to get a "Total Miles on Engine" value for the view. I tried adding a Views PHP field with the following PHP code, but the result that is returned is blank.

Value Code
$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_engine_hours_at_delivery'))) + array_pop(array_pop(field_get_items($entity_type, $entity, 'field_util_agg_engine_hours')));

Output Code

<?php 
$display_output = $entity_field_item['value'];
?>

I'm not very well-versed in PHP, so I'm sure I've got something amiss in that code. Can anyone clarify whether this can be done... and if it can, what I need to change in my code to make it work?

P.S., I also tried this version of the Value Code, but still did not get any result:

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, '$row->field_engine_hours_at_delivery'))) + array_pop(array_pop(field_get_items($entity_type, $entity, '$row->field_util_agg_engine_hours')));

Thanks!

Comments

chi’s picture

I think it could be done with global math expression field which is provided by Views module.

rashmit sidhu’s picture

You need to return value from "Value code" and print in "Output code".

Value code

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_engine_hours_at_delivery'))) + array_pop(array_pop(field_get_items($entity_type, $entity, 'field_util_agg_engine_hours')));
return $entity_field[0]['value'];

Output Code

<?php
        echo $value; // Which is return from Value code (For more details review Replacement pattern of output code).
?>

Hope this helps.

Thanks!

liam morland’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.