You are here

function values_form in Values 7

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

Form for adding a new value set.

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

File

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

Code

function values_form($form, &$form_state, $action = 'edit', $value_set = NULL) {
  if (!empty($form_state['values']['data'])) {
    $value_set->data = $form_state['values']['data'];
    usort($value_set->data, 'values_sort_by_weight');
  }
  else {

    // If the argument is a string, aka a value set name, we load it
    // otherwise we assume it's a value set object
    if (is_string($value_set)) {
      $value_set = values_load($value_set, TRUE);
    }
  }
  $form = array();
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $action,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Value set name'),
    '#description' => t('The human readable name of this value set.'),
    '#default_value' => isset($value_set->title) ? $value_set->title : '',
    '#required' => TRUE,
    '#weight' => -10,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => isset($value_set->name) ? $value_set->name : '',
    '#required' => TRUE,
    '#weight' => -9,
    '#disabled' => isset($value_set->name),
    '#machine_name' => array(
      'source' => array(
        'title',
      ),
      'exists' => '_values_name_exists',
    ),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#description' => t('A short description of the value set'),
    '#default_value' => isset($value_set->description) ? $value_set->description : '',
    '#weight' => -8.5,
  );

  // 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,
  );

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

  // Add the current values to the form.
  for ($delta = 0; $delta < $form_state['values_count']; $delta++) {
    $weight = isset($value_set->data[$delta]['weight']) ? intval($value_set->data[$delta]['weight']) : $delta;
    $form['values_wrapper']['data'][$delta] = array(
      // Old way
      // 'value' => array(
      //   '#type' => 'textfield',
      //   '#title' => t('Value @n', array('@n' => ($delta + 1))),
      //   '#default_value' => isset($value_set->data[$delta]['value']) ? $value_set->data[$delta]['value'] : '',
      //   '#access' => user_access('administer values'),
      // ),
      'value' => array(
        '#type' => 'textfield',
        '#title' => t('Value @n', array(
          '@n' => $delta + 1,
        )),
        '#default_value' => isset($value_set->data[$delta]['value']) ? $value_set->data[$delta]['value'] : '',
        '#access' => user_access('administer values'),
      ),
      'key' => array(
        '#type' => 'machine_name',
        '#title' => t('Key for value @n', array(
          '@n' => $delta + 1,
        )),
        '#default_value' => isset($value_set->data[$delta]['key']) ? $value_set->data[$delta]['key'] : '',
        '#description' => t('A key for this value. Strips out HTML and other unsafe characters.'),
        '#after_build' => array(
          'values_form_value_after_build',
        ),
        '#machine_name' => array(
          'source' => array(
            'values_wrapper',
            'data',
            $delta,
            'value',
          ),
          'exists' => '_values_value_key_exists',
          // This is a dummy function
          'label' => 'Key',
        ),
      ),
      'remove' => array(
        '#type' => 'checkbox',
        '#title' => t(''),
        '#default_value' => isset($value_set->data[$delta]['remove']) ? $value_set->data[$delta]['remove'] : 0,
      ),
      'weight' => array(
        '#type' => 'weight',
        '#delta' => $form_state['values_count'],
        '#default_value' => $weight,
      ),
    );
  }

  // AJAX-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.
    '#ajax' => array(
      'callback' => 'values_form_js',
      'wrapper' => 'values-values',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 10,
  );
  return $form;
}