You are here

function recipe_view in Recipe 6

Same name and namespace in other branches
  1. 5 recipe.module \recipe_view()
  2. 7.2 recipe.module \recipe_view()
  3. 7 recipe.module \recipe_view()

Implementation of hook_view().

File

./recipe.module, line 648
recipe.module - share recipes

Code

function recipe_view($node, $teaser = FALSE, $page = FALSE) {
  drupal_add_css(drupal_get_path('module', 'recipe') . '/recipe.css');
  if ($page) {
    $breadcrumb = array();
    $breadcrumb[] = l(t('Home'), NULL);
    $breadcrumb[] = l(t('Recipes'), 'recipe');
    if ($vocabs = taxonomy_get_vocabularies('recipe')) {
      $vocab = array_shift($vocabs);
      if ($terms = taxonomy_node_get_terms_by_vocabulary($node, $vocab->vid)) {
        $term = array_shift($terms);
        if ($parents = taxonomy_get_parents_all($term->tid)) {
          $parents = array_reverse($parents);
          foreach ($parents as $p) {
            $breadcrumb[] = l($p->name, 'recipe/bycat/' . $p->tid);
          }
        }
      }
    }
    drupal_set_breadcrumb($breadcrumb);

    // Remove taxo links if you are showing the summary.
    if (variable_get('recipe_summary_location', 0) < 2) {

      // Remove each node term that is from a recipe vocab.
      $recipe_vocabs = taxonomy_get_vocabularies('recipe');
      foreach ($node->taxonomy as $tid => $term) {
        if (isset($recipe_vocabs[$term->vid])) {
          unset($node->taxonomy[$tid]);
        }
      }
    }
  }

  // Prepare and sanitize node fields.
  $node = recipe_node_prepare($node, $teaser);

  // If it is a teaser, you're done.
  if ($teaser) {
    return $node;
  }

  // Calculate yield and ingredient factor.
  // Get custom yield or default to a factor of 1.
  $node->yield = intval($node->yield);

  // Factor is calculated and added into the $node variable.
  $node->factor = 1;

  // check post variable to see if the yield form was posted.
  if ($node->yield != 0) {
    if ($_POST['op'] == t('Change')) {
      $node->factor = $_POST['custom_yield'] / $node->yield;
      $node->yield = $_POST['custom_yield'];
      $_POST = array();
    }
    elseif ($_POST['op'] == t('Halve')) {
      $node->factor = $_POST['custom_yield'] / 2 / $node->yield;
      $node->yield = $_POST['custom_yield'] / 2;
      $_POST = array();
    }
    elseif ($_POST['op'] == t('Double')) {
      $node->factor = $_POST['custom_yield'] * 2 / $node->yield;
      $node->yield = $_POST['custom_yield'] * 2;
      $_POST = array();
    }
    elseif ($_POST['op'] == t('Reset')) {
      $_POST = array();
    }
    elseif (isset($node->custom_yield)) {
      $node->factor = $node->custom_yield / $node->yield;
      $node->yield = $node->custom_yield;
    }
  }

  // Begin filling the node->content array with with recipe items.
  $node->content['recipe_description'] = array(
    '#value' => '<div class="recipe-description">' . theme('box', t('Description'), $node->body) . '</div>',
    '#weight' => recipe_content_extra_field_weight('body'),
  );

  // Clear the normal node body value.
  $node->content['body']['#value'] = '';
  $node->content['recipe_ingredients'] = array(
    '#value' => '<div class="recipe-ingredients">' . theme('box', t('Ingredients'), theme('recipe_ingredients', $node)) . '</div>',
    '#weight' => recipe_content_extra_field_weight('ingredients'),
  );
  $node->content['recipe_instructions'] = array(
    '#value' => '<div class="recipe-instructions">' . theme('box', t('Instructions'), $node->instructions) . '</div>',
    '#weight' => recipe_content_extra_field_weight('instructions'),
  );

  // Don't show the notes box at all it there are no notes.
  if ($node->notes) {
    $node->content['recipe_notes'] = array(
      '#value' => '<div class="recipe-notes">' . theme('box', t('Notes'), $node->notes) . '</div>',
      '#weight' => recipe_content_extra_field_weight('notes'),
    );
  }

  // If you are showing the summary in the node content.
  if (variable_get('recipe_summary_location', 0) == 0) {
    $node->content['recipe_summary_box'] = array(
      '#value' => '<div class="recipe-summary">' . theme('recipe_summary', $node) . '</div>',
      '#weight' => recipe_content_extra_field_weight('summary_box'),
    );
  }
  return $node;
}