You are here

function recipe_plaintext_format_ingredients in Recipe 7.2

Formats an ingredient for display as a MasterCook4 recipe.

Parameters

stdClass $node: The node being displayed.

Return value

string The formatted ingredients.

1 call to recipe_plaintext_format_ingredients()
recipe_plaintext_export in modules/recipe_plaintext.module

File

modules/recipe_plaintext.module, line 208

Code

function recipe_plaintext_format_ingredients($node = NULL) {
  $col_widths = array(
    'quantity' => 0,
    'unit' => 0,
  );

  //prepare ingredients factor
  $factor = 1;
  if (isset($node->recipe_custom_yield)) {
    $factor = $node->recipe_custom_yield / $node->recipe_yield;
    $node->recipe_yield = $node->recipe_custom_yield;
  }
  $unit_list = recipe_get_units();
  $ingredient_items = field_get_items('node', $node, 'recipe_ingredient');
  $ingredient_instance = field_read_instance('node', 'recipe_ingredient', 'recipe');
  $quantity_format = $ingredient_instance['display']['default']['settings']['fraction_format'];
  $abbreviation_setting = $ingredient_instance['display']['default']['settings']['unit_abbreviation'];
  $recipe_ingredients = array();
  foreach ($ingredient_items as $item) {

    // Load the ingredient so we can print its name.
    $ingredient = recipe_load_ingredient($item['iid']);
    $item['name'] = $ingredient->name;

    // Sanitize the name and note.
    $name = filter_xss($item['name'], array());
    $note = filter_xss($item['note'], array());

    // Format the ingredient quantity.
    if ($item['quantity'] > 0) {
      $quantity = recipe_ingredient_quantity_from_decimal($item['quantity'] * $factor, $quantity_format, TRUE);
      $quantity = str_replace('⁄', '/', $quantity);
    }
    else {
      $quantity = ' ';
    }

    // Collect column widths.
    if (strlen($quantity) > $col_widths['quantity']) {
      $col_widths['quantity'] = strlen($quantity);
    }

    // Concatenate the ingredient note to the name, if set.
    if (!empty($note)) {
      $name .= ' (' . $note . ')';
    }

    // Print the unit unless it has no abbreviation. Those units do not get
    // printed in any case.
    $unit_display = '';
    $item['unit'] = isset($unit_list[$item['unit_key']]) ? $unit_list[$item['unit_key']] : array();
    if (!empty($item['unit']['abbreviation'])) {

      // Print the abbreviation if recipe_unit_display == 0.
      if ($abbreviation_setting == 0) {
        $unit_display = $item['unit']['abbreviation'];
      }
      else {
        $unit_display = $item['quantity'] > 1 ? $item['unit']['plural'] : $item['unit']['name'];
      }
      if (strlen($unit_display) > $col_widths['unit']) {
        $col_widths['unit'] = strlen($unit_display);
      }
    }
    $recipe_ingredients[] = array(
      'quantity' => $quantity,
      'unit' => $unit_display,
      'name' => $name,
    );
  }

  // Render output with the correct column padding.
  $output = '';
  $wrap_pad = str_repeat(" ", $col_widths['unit'] + $col_widths['quantity'] + 2);
  foreach ($recipe_ingredients as $ingredient) {

    // Format the ingredient display.
    $ingredient_display = sprintf("%" . $col_widths['quantity'] . "s %-" . $col_widths['unit'] . "s %s\n", $ingredient['quantity'], $ingredient['unit'], $ingredient['name']);
    $output .= wordwrap($ingredient_display, 75, "\n{$wrap_pad}");
  }
  return $output;
}