You are here

function units_units_unit_presave in Units of Measurement 7.2

Implements hook_ENTITY_TYPE_presave().

File

./units.module, line 194
Provide API for managing and converting units of measurement.

Code

function units_units_unit_presave($entity) {
  if (isset($entity->original->decomposition_mathematical_expression_id)) {
    units_mathematical_expression_delete($entity->original->decomposition_mathematical_expression_id);
  }
  if (is_object($entity->decomposition)) {
    $entity->decomposition
      ->save();
    $entity->decomposition_mathematical_expression_id = $entity->decomposition
      ->getMathematicalExpressionId();
  }
  else {
    $entity->decomposition_mathematical_expression_id = 0;
  }

  // We must make sure that $entity dimension is the same as all other units
  // within its measure. It just does not make sense to have units of different
  // dimension in the same measure.
  $dimension = $entity
    ->dimension();
  foreach (units_unit_by_measure_load_multiple($entity->measure) as $unit) {
    if ($entity
      ->identifier() != $unit
      ->identifier() && !units_dimension_equal($dimension, $unit
      ->dimension())) {

      // TODO: what am I supposed to do? throw an exception? show an error message? Log it to watchdog?
    }
  }
}