You are here

function recipe_form in Recipe 7

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

Implements hook_form().

File

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

Code

function recipe_form($node, &$form_state) {

  // Title.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#maxlength' => 255,
  );

  // Special WYSIWYG Section for description, instructions, and notes.
  $form['recipe_description'] = array(
    '#title' => t('Description'),
    '#type' => 'text_format',
    '#format' => isset($node->format) ? $node->format : NULL,
    '#default_value' => !empty($node->recipe_description) ? $node->recipe_description : '',
    '#cols' => 60,
    '#rows' => 1,
    '#description' => t('A short description or "teaser" for the recipe.'),
    '#required' => TRUE,
  );
  $form['recipe_instructions'] = array(
    '#title' => t('Instructions'),
    '#type' => 'text_format',
    '#format' => isset($node->format) ? $node->format : NULL,
    '#default_value' => !empty($node->recipe_instructions) ? $node->recipe_instructions : '',
    '#cols' => 60,
    '#rows' => 10,
    '#description' => t('Step by step instructions on how to prepare and cook the recipe.'),
  );
  $form['recipe_notes'] = array(
    '#title' => t('Additional notes'),
    '#type' => 'text_format',
    '#format' => isset($node->format) ? $node->format : NULL,
    '#default_value' => !empty($node->recipe_notes) ? $node->recipe_notes : '',
    '#cols' => 60,
    '#rows' => 5,
    '#description' => t('Optional. Describe a great dining experience relating to this recipe, or note which wine or other dishes complement this recipe.'),
  );
  $form['recipe_yield'] = array(
    '#type' => 'textfield',
    '#title' => t('Yield'),
    '#default_value' => !empty($node->recipe_yield) ? $node->recipe_yield : '',
    '#size' => 4,
    '#maxlength' => 4,
    '#description' => t('The number of servings the recipe will make (whole number integer, ie 5 or 6).'),
    '#required' => TRUE,
  );
  $form['recipe_yield_unit'] = array(
    '#type' => 'textfield',
    '#title' => t('Yield Units'),
    '#default_value' => empty($node->recipe_yield_unit) ? t('Servings') : $node->recipe_yield_unit,
    '#size' => 16,
    '#maxlength' => 64,
    '#description' => t('The units for the yield field(ie servings, people, cans, cookies, etc).'),
    '#required' => FALSE,
  );
  $form['recipe_ingredients'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Ingredients'),
    '#tree' => TRUE,
  );

  // This is the autocomplete callback url.
  $callback = 'recipe/ingredient/autocomplete';
  if (empty($node->recipe_ingredients['ing']) || !is_array($node->recipe_ingredients['ing'])) {
    $node->recipe_ingredients['ing'] = array();
  }

  # Figure out max weight so new ingredients will sort at the bottom by default.
  $max_weight = 0;
  foreach ($node->recipe_ingredients['ing'] as $id => $ingredient) {
    if ($max_weight < $ingredient['weight']) {
      $max_weight = $ingredient['weight'];
    }
  }
  if (isset($form_state['add_ingredients']) || count($node->recipe_ingredients['ing']) == 0) {
    unset($form_state['add_ingredients']);
    $add_count = variable_get('recipe_add_more_count', 5);
    for ($delta = 0; $delta < $add_count; $delta++) {
      array_push($node->recipe_ingredients['ing'], array(
        'ri_id' => NULL,
        'quantity' => '',
        'unit_key' => '',
        'name' => '',
        'note' => '',
        'weight' => $max_weight + $delta,
      ));
    }
  }

  // Weights range from -delta to +delta, so delta should be at least half
  // of the amount of blocks present. This makes sure all blocks in the same
  // region get an unique weight.
  $weight_delta = count($node->recipe_ingredients['ing']);

  // Container for just the ingredients.
  $form['recipe_ingredients']['ing'] = array(
    '#prefix' => '<div id="ingredients-wrapper">',
    '#suffix' => '</div>',
    '#theme' => 'ingredients_form',
  );
  foreach ($node->recipe_ingredients['ing'] as $id => $ingredient) {
    $form['recipe_ingredients']['ing'][$id]['ri_id'] = array(
      '#type' => 'hidden',
      '#value' => $ingredient['ri_id'],
    );

    // Strange, but html_entity_decode() doesn't handle &frasl;
    $form['recipe_ingredients']['ing'][$id]['quantity'] = array(
      '#type' => 'textfield',
      '#title' => t('Quantity'),
      '#default_value' => preg_replace('/\\&frasl;/', '/', recipe_ingredient_quantity_from_decimal($ingredient['quantity'], TRUE)),
      '#size' => 8,
      '#maxlength' => 8,
      '#attributes' => array(
        'class' => array(
          'form-item-recipe-ingredients-quantity',
        ),
      ),
    );
    $form['recipe_ingredients']['ing'][$id]['unit_key'] = array(
      '#type' => 'select',
      '#title' => t('Unit'),
      '#default_value' => $ingredient['unit_key'] != '' ? $ingredient['unit_key'] : variable_get('recipe_default_unit', ''),
      '#options' => recipe_unit_options(),
      '#attributes' => array(
        'class' => array(
          'form-item-recipe-ingredients-unit-key',
        ),
      ),
    );
    $form['recipe_ingredients']['ing'][$id]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $ingredient['name'],
      '#size' => 25,
      '#maxlength' => 128,
      '#autocomplete_path' => $callback,
      '#attributes' => array(
        'class' => array(
          'form-item-recipe-ingredients-name',
        ),
      ),
    );
    $form['recipe_ingredients']['ing'][$id]['note'] = array(
      '#type' => 'textfield',
      '#title' => t('Note'),
      '#default_value' => $ingredient['note'],
      '#size' => 40,
      '#maxlength' => 255,
      '#attributes' => array(
        'class' => array(
          'form-item-recipe-ingredients-note',
        ),
      ),
    );
    $form['recipe_ingredients']['ing'][$id]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => $ingredient['weight'],
      '#delta' => $weight_delta,
    );
  }

  // We name our button 'recipe_more_ingredients' to avoid conflicts with other modules using
  // Ajax-enabled buttons with the id 'more'.
  $form['recipe_ingredients']['recipe_more_ingredients'] = array(
    '#type' => 'submit',
    '#value' => t('More ingredients'),
    '#description' => t("If the amount of boxes above isn't enough, click here to add more ingredients."),
    '#weight' => 1,
    '#submit' => array(
      'recipe_more_ingredients_submit',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'recipe_more_ingredients_js',
      'wrapper' => 'ingredients-wrapper',
      'effect' => 'fade',
    ),
  );
  $form['recipe_source'] = array(
    '#type' => 'textfield',
    '#title' => t('Source'),
    '#default_value' => !empty($node->recipe_source) ? $node->recipe_source : '',
    '#size' => 60,
    '#maxlength' => 127,
    '#description' => t('Optional. Does anyone else deserve credit for this recipe?'),
  );
  $form['recipe_preptime'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#maxlength' => 10,
    '#title' => t('Preparation time'),
    '#default_value' => !empty($node->recipe_preptime) ? $node->recipe_preptime : 0,
    '#description' => t('How long does this recipe take to prepare, in <strong>minutes</strong>.  Utilize 0 for N/A.'),
  );
  $form['recipe_cooktime'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#maxlength' => 10,
    '#title' => t('Cooking time'),
    '#default_value' => !empty($node->recipe_cooktime) ? $node->recipe_cooktime : 0,
    '#description' => t('How long does this recipe take to cook, in <strong>minutes</strong>. Utilize 0 for N/A.'),
  );

  //We still need the parent input format filter set.

  //$form['filter'] = filter_form($node->format);

  // Move the filter form down a bit.

  //$form['filter']['#weight'] = 5;
  return $form;
}