1 list.module list_field_settings_form($field, $instance, $has_data)

Implements hook_field_settings_form().

File

core/modules/field/modules/list/list.module, line 50
Defines list field types that can be used with the Options module.

Code

function list_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];

  switch ($field['type']) {
    case 'list_integer':
    case 'list_float':
    case 'list_text':
      // Get default value.
      $options = $field['settings']['allowed_values'];
      // Toggled by default if options already set.
      $key_type_toggled = !empty($options);

      $form['allowed_values'] = array(
        '#type' => 'options',
        '#title' => t('Allowed values list'),
        '#description' => '',
        '#default_value' => '',
        '#default_value_allowed' => FALSE,
        '#options' => $options,
        '#element_validate' => array('list_allowed_values_setting_validate'),
        '#field_has_data' => $has_data,
        '#field' => $field,
        '#field_type' => $field['type'],
        '#key_type' => 'mixed', // Optimal for now.
        '#key_type_toggle' => t('Custom keys'),
        '#key_type_toggled' => $key_type_toggled,
        '#access' => empty($settings['allowed_values_function']),
        '#multiple' => !($field['cardinality'] == 1),
      );

      // Set proper allowed values in $form_state.
      $form['#validate'][] = 'options_field_settings_validate';
      break;

    case 'list_boolean':
      $values = $settings['allowed_values'];
      $off_value = array_shift($values);
      $on_value = array_shift($values);

      $form['allowed_values'] = array(
        '#type' => 'value',
        '#description' => '',
        '#value_callback' => 'list_boolean_allowed_values_callback',
        '#access' => empty($settings['allowed_values_function']),
      );
      $form['allowed_values']['on'] = array(
        '#type' => 'textfield',
        '#title' => t('On value'),
        '#default_value' => $on_value,
        '#required' => FALSE,
        '#description' => t('If left empty, "1" will be used.'),
        // Change #parents to make sure the element is not saved into field
        // settings.
        '#parents' => array('on'),
      );
      $form['allowed_values']['off'] = array(
        '#type' => 'textfield',
        '#title' => t('Off value'),
        '#default_value' => $off_value,
        '#required' => FALSE,
        '#description' => t('If left empty, "0" will be used.'),
        // Change #parents to make sure the element is not saved into field
        // settings.
        '#parents' => array('off'),
      );

      // Link the allowed value to the on / off elements to prepare for the rare
      // case of an alter changing #parents.
      $form['allowed_values']['#on_parents'] = &$form['allowed_values']['on']['#parents'];
      $form['allowed_values']['#off_parents'] = &$form['allowed_values']['off']['#parents'];

      break;
  }

  // Alter the description for allowed values depending on the widget type.
  if ($instance['widget']['type'] == 'options_onoff') {
    $form['allowed_values']['#description'] .= '<p>' . t("For a 'single on/off checkbox' widget, define the 'off' value first, then the 'on' value in the <strong>Allowed values</strong> section. Note that the checkbox will be labeled with the label of the 'on' value.") . '</p>';
  }
  elseif ($instance['widget']['type'] == 'options_buttons') {
    $form['allowed_values']['#description'] .= '<p>' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the <em>Number of values</em> option is greater than 1 for this field, otherwise radios will be displayed.") . '</p>';
  }

  $form['allowed_values_function'] = array(
    '#type' => 'value',
    '#value' => $settings['allowed_values_function'],
  );
  $form['allowed_values_function_display'] = array(
    '#type' => 'item',
    '#title' => t('Allowed values list'),
    '#markup' => t('The value of this field is being determined by the %function function and may not be changed.', array('%function' => $settings['allowed_values_function'])),
    '#access' => !empty($settings['allowed_values_function']),
  );

  return $form;
}