You are here

function values_form in Values 6

Same name and namespace in other branches
  1. 7 values.module \values_form()

Form for adding a new value set.

1 string reference to 'values_form'
values_menu in ./values.module
Implementation of hook_menu().

File

./values.module, line 134
API for managing reusable value sets.

Code

function values_form(&$form_state, $action = 'edit', $values = NULL) {
  if ($form_state['values_count']) {
    $values->data = $form_state['values']['data'];
  }
  else {
    $values = values_load($values, TRUE);
  }
  $form = array();
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $action,
  );

  // Don't change machine names if we're editing a set
  if (isset($values->name)) {
    $form['name'] = array(
      '#type' => 'hidden',
      '#value' => $values->name,
    );
  }
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Value set description'),
    '#description' => t('This description will appear on the administrative table to tell you what the values are about.'),
    '#default_value' => $values ? $values->description : '',
    '#required' => TRUE,
    '#weight' => -9,
  );

  // Count values
  if (isset($form_state['values_count'])) {
    $values_count = $form_state['values_count'];
  }
  else {
    $values_count = max(2, empty($values->data) ? 2 : count($values->data));
  }

  // Wrapper for values
  $form['values_wrapper'] = array(
    '#tree' => FALSE,
    '#title' => t('Values'),
    '#description' => t('These are the actual values associated with this value set.'),
    '#prefix' => '<div class="clear-block" id="values-value-wrapper">',
    '#suffix' => '</div>',
    '#weight' => -8,
  );

  // Container for value fields
  $form['values_wrapper']['data'] = array(
    '#tree' => TRUE,
    '#prefix' => '<div id="values-values">',
    '#suffix' => '</div>',
    '#theme' => 'values_value_fields',
    '#cache' => TRUE,
  );

  // Add the current values to the form.
  for ($delta = 0; $delta < $values_count; $delta++) {
    $form['values_wrapper']['data'][$delta] = array(
      'value' => array(
        '#type' => 'textfield',
        '#title' => t('Value @n', array(
          '@n' => $delta + 1,
        )),
        '#default_value' => isset($values->data[$delta]['value']) ? $values->data[$delta]['value'] : '',
        '#size' => 4,
        '#maxlength' => 32,
      ),
      'label' => array(
        '#type' => 'textfield',
        '#title' => t('Label for value @n', array(
          '@n' => $delta + 1,
        )),
        '#default_value' => isset($values->data[$delta]['label']) ? $values->data[$delta]['label'] : '',
        '#access' => user_access('administer values'),
      ),
      'weight' => array(
        '#type' => 'weight',
        '#delta' => $values_count,
        '#default_value' => isset($values->data[$delta]['weight']) ? intval($values->data[$delta]['weight']) : $delta,
      ),
    );
  }

  // AHAH-enabled "Add more" button
  $form['values_wrapper']['values_add_more'] = array(
    '#type' => 'submit',
    '#value' => t('Add more'),
    '#description' => t("If the amount of options above isn't enough, click here to add more."),
    '#weight' => 1,
    '#submit' => array(
      'values_add_more_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => 'values/js',
      'wrapper' => 'values-values',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 10,
  );
  return $form;
}