You are here

API.txt in Units of Measurement 6

Units module provides following API functions:
 
  Hooks:
  ======
  
  hook_define_units()
  
    Main hook of Units module API.
    Returns array of arrays of unit definitions, with unit id's as keys.
    
    ID of unit is it's machine-readable name (i.e. alphanumeric characters 
    and underscores). It's database stored value, and must be unique within
    all Units implementations. For the sake of uniqueness, it's better to 
    prefix unit id with it's category id.
    
    For simplicity, unit category descriptions are stored in units themselves. 
    As there are probably not that many units needed for single site, it won't
    be big penalty.
    
    All unit properties should be safe for output (i.e. all special 
    characters should be encoded). Units module does not filter them and
    integration modules such as MVF will assume they are safe.
    Translation of unit properties also should be done in module implementing
    hook_define_units(). 

    Structure of unit definition:
    'id' => array('property1' => 'value1', 'property2' => 'value2', ...)
    
    Example definition for USD currency:
    
    'currency_usd' => array(
      // Short name ( ~ 1 word if possible).  
      'shortname' => 'USD',
       
      // Full name, e.g. several words describing unit.
      'fullname' => t('U.S. Dollar'),

      // Symbol of unit, or abbreviation.
      'symbol' => '$',
      
      // Machine-readable name of the category of unit. Serves for storing 
      // category internally.
      'category_id' => 'currency',
      
      // Name of the category of unit. Serves for grouping up similar unit 
      // types e.g. in UI.
      'category' => t('World Currencies'),
      
      // Decimals. The number of digits to the right of the decimal point.
      // Note: this is just recommended value. It's not enforced in Units 
      // module.
      'decimals' => '2'
    );
  
  
  hook_units_alter(&$units)
  
    Allows modules to alter units collection after it was built. It allows one 
    module to alter definition of other module's unit
    
 
  Some useful functions:
  ====================== 
  
  units_get_units()
    
    Main workhorse function.
    Returns array of unit definitions, keyed by unit id.
  
    
  units_get_enabled_units()
  
    Returns array of globally enabled units id-s. By convention, empty array 
    means "all units are enabled". Units get enabled or disabled in UI at 
    "admin/content/units".
  
    
  units_get_unit_names($full = NULL)
  
    Returns array of unit names, keyed by unit id-s.
    If $full is not null, names are full names, otherwise short names.
  
  
  units_get_symbol($id)
  
    Returns symbol for unit, if one exists, or empty string otherwise.

File

API.txt
View source
  1. Units module provides following API functions:
  2. Hooks:
  3. ======
  4. hook_define_units()
  5. Main hook of Units module API.
  6. Returns array of arrays of unit definitions, with unit id's as keys.
  7. ID of unit is it's machine-readable name (i.e. alphanumeric characters
  8. and underscores). It's database stored value, and must be unique within
  9. all Units implementations. For the sake of uniqueness, it's better to
  10. prefix unit id with it's category id.
  11. For simplicity, unit category descriptions are stored in units themselves.
  12. As there are probably not that many units needed for single site, it won't
  13. be big penalty.
  14. All unit properties should be safe for output (i.e. all special
  15. characters should be encoded). Units module does not filter them and
  16. integration modules such as MVF will assume they are safe.
  17. Translation of unit properties also should be done in module implementing
  18. hook_define_units().
  19. Structure of unit definition:
  20. 'id' => array('property1' => 'value1', 'property2' => 'value2', ...)
  21. Example definition for USD currency:
  22. 'currency_usd' => array(
  23. // Short name ( ~ 1 word if possible).
  24. 'shortname' => 'USD',
  25. // Full name, e.g. several words describing unit.
  26. 'fullname' => t('U.S. Dollar'),
  27. // Symbol of unit, or abbreviation.
  28. 'symbol' => '$',
  29. // Machine-readable name of the category of unit. Serves for storing
  30. // category internally.
  31. 'category_id' => 'currency',
  32. // Name of the category of unit. Serves for grouping up similar unit
  33. // types e.g. in UI.
  34. 'category' => t('World Currencies'),
  35. // Decimals. The number of digits to the right of the decimal point.
  36. // Note: this is just recommended value. It's not enforced in Units
  37. // module.
  38. 'decimals' => '2'
  39. );
  40. hook_units_alter(&$units)
  41. Allows modules to alter units collection after it was built. It allows one
  42. module to alter definition of other module's unit
  43. Some useful functions:
  44. ======================
  45. units_get_units()
  46. Main workhorse function.
  47. Returns array of unit definitions, keyed by unit id.
  48. units_get_enabled_units()
  49. Returns array of globally enabled units id-s. By convention, empty array
  50. means "all units are enabled". Units get enabled or disabled in UI at
  51. "admin/content/units".
  52. units_get_unit_names($full = NULL)
  53. Returns array of unit names, keyed by unit id-s.
  54. If $full is not null, names are full names, otherwise short names.
  55. units_get_symbol($id)
  56. Returns symbol for unit, if one exists, or empty string otherwise.