You are here

function recipe_get_units in Recipe 7

Same name and namespace in other branches
  1. 7.2 recipe.module \recipe_get_units()

Returns an array of units.

WARNING: Do not change keys once they are released without a migration path.

This allows for unit string translations and the possibility of allowing for unit level integration with other modules.

For new units, use the same unit keys as the unitsapi module keys found in units.xml. NOTE: This is not possible for all units.

Return value

An array of units. $units = array('unit key'=> array(name=>, plural=>, abbreviation=>, system=>, type=>, aliases=>))

7 calls to recipe_get_units()
format_plaintext_ingredients in includes/recipe_plaintext.module
merge_template in includes/recipe_mastercook4.module
RecipeWebTestBase::setUp in src/Tests/RecipeWebTestBase.php
recipe_recipeML_export_single in includes/recipe_recipeML.module
recipe_unit_fuzzymatch in ./recipe.module
Returns a best-guess matched unit key for a unit of measure.

... See full list

File

./recipe.module, line 1998
Contains functions for Recipe node CRUD and display.

Code

function recipe_get_units($limit_to_preferred = 0) {
  $us_units = array(
    'cup' => array(
      'name' => t('cup'),
      'plural' => t('cups'),
      'abbreviation' => t('c'),
      'system' => 'us customary',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'us liquid pint' => array(
      'name' => t('pint'),
      'plural' => t('pints'),
      'abbreviation' => t('pt'),
      'system' => 'us customary',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'us liquid quart' => array(
      'name' => t('quart'),
      'plural' => t('quarts'),
      'abbreviation' => t('q'),
      'system' => 'us customary',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'us gallon' => array(
      'name' => t('gallon'),
      'plural' => t('gallons'),
      'abbreviation' => t('gal'),
      'system' => 'us customary',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'pound' => array(
      'name' => t('pound'),
      'plural' => t('pounds'),
      'abbreviation' => t('lb'),
      'system' => 'us customary',
      'type' => t('weight'),
      'aliases' => array(),
    ),
    'ounce' => array(
      'name' => t('ounce'),
      'plural' => t('ounces'),
      'abbreviation' => t('oz'),
      'system' => 'us customary',
      'type' => t('weight'),
      'aliases' => array(),
    ),
    'us fluid ounce' => array(
      'name' => t('fluid ounce'),
      'plural' => t('fluid ounces'),
      'abbreviation' => t('fl oz'),
      'system' => 'us customary',
      'type' => t('volume'),
      'aliases' => array(),
    ),
  );
  $si_units = array(
    'milliliter' => array(
      'name' => t('milliliter'),
      'plural' => t('milliliters'),
      'abbreviation' => t('ml'),
      'system' => 'si',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'centiliter' => array(
      'name' => t('centiliter'),
      'plural' => t('centiliters'),
      'abbreviation' => t('cl'),
      'system' => 'si',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'liter' => array(
      'name' => t('liter'),
      'plural' => t('liters'),
      'abbreviation' => t('l'),
      'system' => 'si',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'deciliter' => array(
      'name' => t('deciliter'),
      'plural' => t('deciliters'),
      'abbreviation' => t('dl'),
      'system' => 'si',
      'type' => t('volume'),
      'aliases' => array(),
    ),
    'milligram' => array(
      'name' => t('milligram'),
      'plural' => t('milligrams'),
      'abbreviation' => t('mg'),
      'system' => 'si',
      'type' => t('weight'),
      'aliases' => array(),
    ),
    'gram' => array(
      'name' => t('gram'),
      'plural' => t('grams'),
      'abbreviation' => t('g'),
      'system' => 'si',
      'type' => t('weight'),
      'aliases' => array(),
    ),
    'centigram' => array(
      'name' => t('centigram'),
      'plural' => t('centigrams'),
      'abbreviation' => t('cg'),
      'system' => 'si',
      'type' => t('weight'),
      'aliases' => array(),
    ),
    'kilogram' => array(
      'name' => t('kilogram'),
      'plural' => t('kilograms'),
      'abbreviation' => t('kg'),
      'system' => 'si',
      'type' => t('weight'),
      'aliases' => array(),
    ),
  );
  $common_units = array(
    'tablespoon' => array(
      'name' => t('tablespoon'),
      'plural' => t('tablespoons'),
      'abbreviation' => t('T'),
      'system' => 'common',
      'type' => t('volume'),
      'aliases' => array(
        'tbsp',
        'tb',
      ),
    ),
    'teaspoon' => array(
      'name' => t('teaspoon'),
      'plural' => t('teaspoons'),
      'abbreviation' => t('t'),
      'system' => 'common',
      'type' => t('volume'),
      'aliases' => array(
        'tsp',
      ),
    ),
    'slice' => array(
      'name' => t('slice'),
      'plural' => t('slices'),
      'abbreviation' => t('sli'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'clove' => array(
      'name' => t('clove'),
      'plural' => t('cloves'),
      'abbreviation' => t('clv'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'load' => array(
      'name' => t('loaf'),
      'plural' => t('loaves'),
      'abbreviation' => t('lf'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'pinch' => array(
      'name' => t('pinch'),
      'plural' => t('pinches'),
      'abbreviation' => t('pn'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'package' => array(
      'name' => t('package'),
      'plural' => t('packages'),
      'abbreviation' => t('pk'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(
        'pack',
      ),
    ),
    'can' => array(
      'name' => t('can'),
      'plural' => t('cans'),
      'abbreviation' => t('cn'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(
        'jar',
      ),
    ),
    'drop' => array(
      'name' => t('drop'),
      'plural' => t('drops'),
      'abbreviation' => t('dr'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'bunch' => array(
      'name' => t('bunch'),
      'plural' => t('bunches'),
      'abbreviation' => t('bn'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'dash' => array(
      'name' => t('dash'),
      'plural' => t('dashes'),
      'abbreviation' => t('ds'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'carton' => array(
      'name' => t('carton'),
      'plural' => t('cartons'),
      'abbreviation' => t('ct'),
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
    'unit' => array(
      'name' => t('unit'),
      'plural' => t('units'),
      'abbreviation' => '',
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(
        'each',
        'ea',
        'whole',
      ),
    ),
    'unknown' => array(
      'name' => t('unknown'),
      'plural' => t('unknown'),
      'abbreviation' => '',
      'system' => 'common',
      'type' => t('indefinite'),
      'aliases' => array(),
    ),
  );
  if (variable_get('recipe_preferred_system_of_measure', 0) == 0) {

    // US system preferred.
    $units = array_merge($us_units, $common_units);
    if ($limit_to_preferred == 0) {
      $units = array_merge($units, $si_units);
    }
    uasort($units, 'unit_sort');
  }
  else {

    // SI system preferred.
    $units = array_merge($si_units, $common_units);
    if ($limit_to_preferred == 0) {
      $units = array_merge($units, $us_units);
    }
    uasort($units, 'unit_sort');
  }

  // Allow other modules to alter the units.
  drupal_alter('recipe_ing_units', $units);
  return $units;
}