You are here

function recipe_field_widget_form in Recipe 7.2

Implements hook_field_widget_form().

File

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

Code

function recipe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Add recipe.css to style the form elements on a single line.
  drupal_add_css(drupal_get_path('module', 'recipe') . '/recipe.css');
  switch ($instance['widget']['type']) {
    case 'recipe_ingredient_autocomplete':

      // Strange, but html_entity_decode() doesn't handle ⁄
      $quantity = isset($items[$delta]['quantity']) ? preg_replace('/\\⁄/', '/', recipe_ingredient_quantity_from_decimal($items[$delta]['quantity'], '{%d} %d⁄%d', TRUE)) : '';
      $element['quantity'] = array(
        '#type' => 'textfield',
        '#title' => t('Quantity'),
        '#default_value' => $quantity,
        '#size' => 8,
        '#maxlength' => 8,
        '#attributes' => array(
          'class' => array(
            'recipe-ingredient-quantity',
          ),
        ),
      );
      $element['unit_key'] = array(
        '#type' => 'select',
        '#title' => t('Unit'),
        '#default_value' => isset($items[$delta]['unit_key']) ? $items[$delta]['unit_key'] : $instance['widget']['settings']['default_unit'],
        '#options' => recipe_unit_options(),
        '#attributes' => array(
          'class' => array(
            'recipe-ingredient-unit-key',
          ),
        ),
      );
      $ingredient = isset($items[$delta]['iid']) ? recipe_load_ingredient($items[$delta]['iid']) : array();
      $element['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#default_value' => isset($ingredient->name) ? $ingredient->name : '',
        '#size' => 25,
        '#maxlength' => 128,
        '#autocomplete_path' => 'recipe/ingredient/autocomplete',
        '#attributes' => array(
          'class' => array(
            'recipe-ingredient-name',
          ),
        ),
      );
      $element['note'] = array(
        '#type' => 'textfield',
        '#title' => t('Note'),
        '#default_value' => isset($items[$delta]['note']) ? $items[$delta]['note'] : '',
        '#size' => 40,
        '#maxlength' => 255,
        '#attributes' => array(
          'class' => array(
            'recipe-ingredient-note',
          ),
        ),
      );
      $element['#element_validate'] = array(
        'recipe_ingredient_autocomplete_validate',
      );
      break;
  }
  return $element;
}