0

I have a Multivalued field called temperature in that everyday temperature values are stored. In that i would like to identify the highest and lowest value from that field.I found computed field module will do that but i would like to know how i achieve same in computed field.please anyone guide me do this.

Thanks

1 Answer 1

1

Never put code in the database. It is impossible to maintain and debug, and you spend hours wasting your time.

The computed field gives you the option to add the code to a custom module.

My answer used these three fields added to the content type:

  • Temp (machine name field_temp, integer, multi-value)
  • Highest (machine name field_highest, computed, int)
  • Lowest (machine name field_lowest, computed, int)

Method: Create a custom module (just module and info files), then add this to the module file:

function computed_field_field_highest_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  $temps = field_get_items($entity_type, $entity, 'field_temp');
  if (count($temps)) {
    $max = $temps[0]['value'];
    foreach($temps as $temp) {
      $max = $temp['value'] > $max ? $temp['value'] : $max;
    }
    $entity_field[0]['value'] = $max;
  }
}

function computed_field_field_lowest_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  $temps = field_get_items($entity_type, $entity, 'field_temp');
  if (count($temps)) {
    $min = $temps[0]['value'];
    foreach($temps as $temp) {
      $min = $temp['value'] < $min ? $temp['value'] : $min;
    }
    $entity_field[0]['value'] = $min;
  }
}

Read the documentation page for the computed field module to see the naming convention for adding a custom function. The function name is also visible in the field settings.

In the two functions I refer to 'field_temp'. It is the machine name of the multi-value field holding the temperature values.

0

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.