I created a patch for hooking on the conversion method.

If the factor field of to or from is an array then use an other method for the unit conversion (like the temperature fields).
This way modules can extend the units api with their own units and conversion methods.

CommentFileSizeAuthor
#2 unitsapi.patch1.22 KBbugster
unitsapi.patch2.74 KBbugster

Comments

raspberryman’s picture

Assigned: Unassigned » raspberryman
Status: Active » Fixed

Heya bugster - Thanks for the idea! I very much agree.

I implemented it in a slightly different way: hook_unitsapi_result_alter(&$result) will give you an array with the original conversion value, the conversion result, and the full array of data for the relevant units from the XML, and let you alter any of these fields before the conversion is over.

Commit in http://drupal.org/cvs?commit=489122

bugster’s picture

Status: Fixed » Needs work
StatusFileSize
new1.22 KB

Nice work, cleaner implementation!

I added a patch, so you could add more arguments to the unitsapi_convert function, those arguments go with the drupal_alter function. (func_get_args()) This way you are extremely flexible.

and if the factor is an array then output them like you do with the temperatures.

bugster’s picture

I also think you need to test if factor of from and to is numeric, it is possible that someone puts something different in there, like temperature.

else you can get an illegal operand error.

switch ($units[$from]['kind']) {
    case 'temperature':
        $result = _unitsapi_convert_temperature($value, $units[$to]['factor'][$from]);
      break;
    default:
    	if(is_numeric($units[$from]['factor']) && is_numeric($units[$to]['factor'])) {
    		$from_si = $units[$from]['factor'];
		$to_si = $units[$to]['factor'];
		$from_convert = $value * $from_si;
		$result = $from_convert / $to_si;
    	}
    	else {
    		$result = NULL;
    	}
  }

If those factors are not numeric you may assume they are custom, and correctly implemented in alter result;

You can test this if you remove the switch statement and implement the temperature conversion using the unitsapi_unitsapi_result_alter() function :)

raspberryman’s picture

Status: Needs work » Closed (fixed)

Thanks for your great ideas, bugster!

The following two commits add these features:

http://drupalcode.org/project/unitsapi.git/commitdiff/0256ffb
http://drupalcode.org/project/unitsapi.git/commitdiff/48335a6