In Drupal 7, I have a custom content type containing distance (a decimal field) and time (a date field where only hours, minutes and seconds are entered).

In a view on that custom content type, I would like to show not only distance and time but also speed. (speed = distance / time)

I already tried to create a formula in views calc but:

  1. I didn't find a way to get the hour, minute and second values separately from the date field. When I tried to define DATE_FORMAT (%field, '%i'), I got an error mentioning that i is not an allowed value.
  2. I tried to work with the value of %field directly (because I found out that the field contains 101HHiiss as value) but I coulnd't define something like %field / 1000000 because then I would get an error mentioning that 00 is not an allowed value.

Edit: Sorry, I overread "Views Calc" before I wrote my answer. But maybe it helps you nevertheless.

Views maybe isn't the right place to add your custom code to. You might consider writing a very small custom module. Let Views then maybe display full nodes. And in your custom module you might hook into hook_node_view(), grab your node's fields, do the calculation and finally add the result as a new field to the the node's content, which then will get rendered automatically:

function MY_MODULE_node_view($node) {
    dpm($node); // start with that

    $time = $node->field_time[LANGUAGE_NONE][0]['value'];
    $distance = $node->field_distance[LANGUAGE_NONE][0]['value'];

    // do your calculations
    // ...

    $speed = $result;

    $node->content['speed'] = array(
        '#prefix' => '<div id="speed">',
        '#markup' => $speed,
        '#suffix' => '</div>'

When developing your custom module you might find the Devel module quite helpful. After activating it you can easily check out what's inside a variable and then double click the displayed results for a deeper insight. So you might start by calling dpm($node) right inside hook_node_view().

|improve this answer|||||

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.