You are here

function recipe_form in Recipe 6

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

Implementation of hook_form().

File

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

Code

function recipe_form(&$node, $form_state) {
  $type = node_get_types('type', $node);

  // Title.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#default_value' => $node->title,
    '#required' => TRUE,
    '#weight' => -6,
  );

  // To allow for wysiwyg, we need a parent key.
  $form['body']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body,
    '#cols' => 60,
    '#rows' => 1,
    '#description' => t('A short description or "teaser" for the recipe.'),
    '#required' => TRUE,
  );

  // Render a filter_form to allow for wysiwyg javascript hookup.
  $form['body']['format'] = filter_form($node->format, NULL, array(
    'body_format',
  ));

  // Hide the filter form so the users won't see one of these for each textarea.
  $form['body']['format']['#prefix'] = '<div style="display:none;">';
  $form['body']['format']['#suffix'] = '</div>';

  // Weight goes on parent, not value.
  $form['body']['#weight'] = -5;
  $form['yield'] = array(
    '#type' => 'textfield',
    '#title' => t('Yield'),
    '#default_value' => $node->yield,
    '#size' => 4,
    '#maxlength' => 4,
    '#description' => t('The number of servings the recipe will make (whole number integer, ie 5 or 6).'),
    '#required' => TRUE,
    '#weight' => -4,
  );
  $form['yield_unit'] = array(
    '#type' => 'textfield',
    '#title' => t('Yield Units'),
    '#default_value' => $node->yield_unit == '' ? t('Servings') : $node->yield_unit,
    '#size' => 16,
    '#maxlength' => 64,
    '#description' => t('The units for the yield field(ie servings, people, cans, cookies, etc).'),
    '#required' => FALSE,
    '#weight' => -4,
  );
  $form['ingredients'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Ingredients'),
    '#tree' => TRUE,
    '#theme' => 'ingredients_form',
    '#weight' => -3,
  );

  // This is the autocomplete callback url.
  $callback = 'recipe/ingredient/autocomplete';
  if (!is_array($node->ingredients)) {
    $node->ingredients = array();
  }
  if (isset($form_state['add_ingredients']) || count($node->ingredients) == 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->ingredients, array(
        'ri_id' => NULL,
        'quantity' => '',
        'unit_id' => '',
        'name' => '',
        'note' => '',
        'weight' => 0,
      ));
    }
  }

  // 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->ingredients);
  foreach ($node->ingredients as $id => $ingredient) {
    $form['ingredients'][$id]['ri_id'] = array(
      '#type' => 'hidden',
      '#value' => $ingredient['ri_id'],
    );

    // Strange, but html_entity_decode() doesn't handle &frasl;
    $form['ingredients'][$id]['quantity'] = array(
      '#type' => 'textfield',
      '#title' => '',
      '#default_value' => preg_replace('/\\&frasl;/', '/', recipe_ingredient_quantity_from_decimal($ingredient['quantity'], TRUE)),
      '#size' => 8,
      '#maxlength' => 8,
    );
    $form['ingredients'][$id]['unit_id'] = array(
      '#type' => 'select',
      '#title' => '',
      '#default_value' => $ingredient['unit_id'],
      '#options' => recipe_unit_options(),
    );
    $form['ingredients'][$id]['name'] = array(
      '#type' => 'textfield',
      '#title' => '',
      '#default_value' => $ingredient['name'],
      '#size' => 25,
      '#maxlength' => 128,
      '#autocomplete_path' => $callback,
    );
    $form['ingredients'][$id]['note'] = array(
      '#type' => 'textfield',
      '#title' => '',
      '#default_value' => $ingredient['note'],
      '#size' => 40,
      '#maxlength' => 255,
    );
    $form['ingredients'][$id]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $ingredient['weight'],
      '#delta' => $weight_delta,
    );
  }
  $form['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',
    ),
  );

  // To allow for wysiwyg, we need a parent key.
  $form['instructions']['instructions'] = array(
    '#type' => 'textarea',
    '#title' => t('Instructions'),
    '#default_value' => $node->instructions,
    '#cols' => 60,
    '#rows' => 10,
    '#description' => t('Step by step instructions on how to prepare and cook the recipe.'),
  );

  // Render a filter_form to allow for wysiwyg javascript hookup.
  $form['instructions']['format'] = filter_form($node->format, NULL, array(
    'instructions_format',
  ));

  // Hide the filter form so the users won't see one of these for each textarea.
  $form['instructions']['format']['#prefix'] = '<div style="display:none;">';
  $form['instructions']['format']['#suffix'] = '</div>';

  // Weight goes on parent, not value.
  $form['instructions']['#weight'] = -2;
  $form["source"] = array(
    '#type' => 'textfield',
    '#title' => t('Source'),
    '#default_value' => $node->source,
    '#size' => 60,
    '#maxlength' => 127,
    '#description' => t('Optional. Does anyone else deserve credit for this recipe?'),
    '#weight' => -2,
  );

  // To allow for wysiwyg, we need a parent key.
  $form['notes']['notes'] = array(
    '#type' => 'textarea',
    '#title' => t('Additional notes'),
    '#default_value' => $node->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.'),
  );

  // Render a filter_form to allow for wysiwyg javascript hookup.
  $form['notes']['format'] = filter_form($node->format, NULL, array(
    'notes_format',
  ));

  // Hide the filter form so the users won't see one of these for each textarea.
  $form['notes']['format']['#prefix'] = '<div style="display:none;">';
  $form['notes']['format']['#suffix'] = '</div>';

  // Weight goes on parent, not value.
  $form['notes']['#weight'] = -2;
  $form['preptime'] = array(
    '#type' => 'select',
    '#title' => t('Preparation time'),
    '#default_value' => $node->preptime,
    '#options' => array(
      5 => t('5 minutes'),
      10 => t('10 minutes'),
      15 => t('15 minutes'),
      20 => t('20 minutes'),
      30 => t('30 minutes'),
      45 => t('45 minutes'),
      60 => t('1 hour'),
      90 => t('1 1/2 hours'),
      120 => t('2 hours'),
      150 => t('2 1/2 hours'),
      180 => t('3 hours'),
      210 => t('3 1/2 hours'),
      240 => t('4 hours'),
      300 => t('5 hours'),
      360 => t('6 hours'),
    ),
    '#description' => t('How long does this recipe take to prepare (i.e. elapsed time)'),
    '#weight' => -1,
  );

  //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;
}