You are here

function units_field_devel_generate in Units of Measurement 7.2

Generate dummy values for Units field.

File

units_field/units_field.devel_generate.inc, line 11
Units field integration with Devel Generate module.

Code

function units_field_devel_generate($object, $field, $instance, $bundle) {

  // Picking random unit from $measure, in which we will generate result.
  $units = units_unit_by_measure_load_multiple('length');
  $unit = array_rand($units);
  $unit1 = $units[$unit];
  $value1 = rand(0, 999);
  $unit = array_rand($units);
  $unit2 = $units[$unit];
  $value2 = rand(0, 999);
  $expression1 = new UnitsMathematicalOperatorLinear(units_get_operator('multiply'), $unit1, new UnitsConstantMathematicalExpression($value1));
  $expression2 = new UnitsMathematicalOperatorLinear(units_get_operator('multiply'), new UnitsConstantMathematicalExpression($value2), $unit2);
  $expression = new UnitsMathematicalOperatorLinear(units_get_operator('add'), $expression1, $expression2);
  $wrapper = new UnitsMathematicalExpressionWrapper();
  $wrapper
    ->setExpression($expression);
  $wrapper
    ->save();
  return array(
    'mathematical_expression_id' => $wrapper
      ->getMathematicalExpressionId(),
  );
}