You are here

function bat_options_field_widget_form in Booking and Availability Management Tools for Drupal 7

Implements hook_field_widget_form().

File

modules/bat_options/bat_options.module, line 229

Code

function bat_options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'bat_options_combined') {
    $field_parents = $element['#field_parents'];
    $field_name = $element['#field_name'];
    $language = $element['#language'];
    $parents = array_merge($field_parents, array(
      $field_name,
      $language,
      $delta,
    ));
    $element['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => isset($items[$delta]['name']) ? $items[$delta]['name'] : NULL,
      '#attributes' => array(
        'class' => array(
          'bat_options-option--name',
        ),
      ),
    );
    $element['quantity'] = array(
      '#type' => 'select',
      '#title' => t('Quantity'),
      '#options' => drupal_map_assoc(range(1, 10, 1)),
      '#default_value' => isset($items[$delta]['quantity']) ? $items[$delta]['quantity'] : NULL,
      '#description' => t('How many of this add-on should be available'),
      '#attributes' => array(
        'class' => array(
          'bat_options-option--quantity',
        ),
      ),
    );
    $per_person = isset($field['settings']['per_person']) ? $field['settings']['per_person'] : FALSE;
    $price_options = bat_options_price_options($per_person);
    $element['operation'] = array(
      '#type' => 'select',
      '#title' => t('Operation'),
      '#options' => $price_options,
      '#default_value' => isset($items[$delta]['operation']) ? $items[$delta]['operation'] : NULL,
      '#attributes' => array(
        'class' => array(
          'bat_options-option--operation',
        ),
      ),
    );
    $element['value'] = array(
      '#type' => 'textfield',
      '#title' => t('Value'),
      '#size' => 10,
      '#default_value' => isset($items[$delta]['value']) && $items[$delta]['value'] != 0 ? $items[$delta]['value'] : NULL,
      '#element_validate' => array(
        'bat_options_element_value_validate',
      ),
      '#attributes' => array(
        'class' => array(
          'bat_options-option--value',
        ),
      ),
      '#states' => array(
        'disabled' => array(
          ':input[name="field_addons[en][' . $delta . '][operation]"]' => array(
            'value' => 'no_charge',
          ),
        ),
      ),
    );
    $type_options = array(
      BAT_OPTIONS_OPTIONAL => t('Optional'),
      BAT_OPTIONS_MANDATORY => t('Mandatory'),
      BAT_OPTIONS_ONREQUEST => t('On Request'),
    );
    $element['type'] = array(
      '#type' => 'select',
      '#title' => t('Type'),
      '#options' => $type_options,
      '#default_value' => isset($items[$delta]['type']) ? $items[$delta]['type'] : 'optional',
      '#attributes' => array(
        'class' => array(
          'bat_options-option--type',
        ),
      ),
    );
    $element['remove'] = array(
      '#delta' => $delta,
      '#name' => implode('_', $parents) . '_remove_button',
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#validate' => array(),
      '#submit' => array(
        'bat_options_remove_submit',
      ),
      '#limit_validation_errors' => array(),
      '#ajax' => array(
        'path' => 'bat_options/ajax',
        'effect' => 'fade',
      ),
      '#attributes' => array(
        'class' => array(
          'bat_options-option--remove-button',
        ),
      ),
    );
    $element['#attached']['css'] = array(
      drupal_get_path('module', 'bat_options') . '/css/bat_options_widget.css',
    );
    return $element;
  }
}