You are here

units_field.devel_generate.inc in Units of Measurement 7.2

Units field integration with Devel Generate module.

File

units_field/units_field.devel_generate.inc
View source
<?php

/**
 * @file
 * Units field integration with Devel Generate module.
 */

/**
 * Generate dummy values for Units field.
 */
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(),
  );
}

Functions

Namesort descending Description
units_field_devel_generate Generate dummy values for Units field.