Units API

raspberryman - January 5, 2009 - 21:07

Units API converts between various weights and measurements.

The central API call, unitsapi_convert($value, $from, $to, $details = FALSE), uses the International System of Units (SI) conversion factors to convert measurement units.

This module is only an API and does not have an user interface.

SUPPORTED UNITS

A list of supported units, and the keys you need to pass to the unitsapi_convert() function, are available on the module's help page.

EXAMPLES

<?php
// Convert kilometer to feet
$result = unitsapi_convert(1.5, 'kilometer', 'foot');
// $result == 4921.259843

// Convert Fahrenheit to Kelvin
$result = unitsapi_convert(55, 'fahrenheit', 'kelvin');
// $result == 285.927778

// Convert US liquid ounces to Imperial pints with a detailed array of the conversion
$result = unitsapi_convert(50, 'us fluid ounce', 'imperial pint', TRUE);
// $result == Array ([result] => 2.602107, [from] => US fluid ounces, [to] => Imperial pints)
?>

HOOK_UNITSAPI_ALTER()

Use hook_unitsapi_alter(&$units) to make changes to the units array. For example:

<?php
/**
* Implementation of hook_unitsapi_alter().
*/
function mymodule_unitsapi_alter(&$units) {
 
$units['meter']['singular'] = 'metre';
}
?>

HOW TO ADD NEW UNITS

  • Option #1: Implement hook_unitsapi_alter() from your own module to add units.
  • Option #2: Create an issue with your requested units. The issue requires at least the unit name, plural name, symbol, conversion factor, and an URL to a credible source to verify the conversion factor. For the conversion factor, check the units.xml file for the common factor. For example, all Area conversions use square meters as their conversion factor.

INTEGRATED MODULES

  1. Units of Measurement
  2. Measured Value Field

FUTURE PLANS

  1. Add additional measurement units.
  2. Add additional automated tests.

SPONSORED BY

This module is sponsored by Raspberry Man LLC.

Downloads

Recommended releases

Version Downloads Date Links
6.x-1.0-beta1 Download (14.69 KB) 2010-Jan-31 Notes

Development releases

Version Downloads Date Links
6.x-1.x-dev Download (14.69 KB) 2010-Feb-01 Notes


 
 

Drupal is a registered trademark of Dries Buytaert.