You are here

function values_import_form in Values 7

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

Import a value list

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

File

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

Code

function values_import_form($form, &$form_state) {
  drupal_set_message(t('Importing a value list with a conflicting name will override it.'), 'warning');

  // If the user has the ctools import permission, we allow them to a ctools
  // export (raw php code that will be eval'd)
  if (module_exists('ctools') && user_access('use ctools import')) {
    $form['import_type'] = array(
      '#type' => 'radios',
      '#title' => t('Import Type'),
      '#description' => t('Choose an import type. Import a previously exported value set, or import a flat list of values in a textarea.'),
      '#options' => array(
        'ctools' => 'Ctools Export',
        'values' => 'Flat List',
      ),
      '#default_value' => 'ctools',
    );
    $form['import'] = array(
      '#type' => 'textarea',
      '#title' => t('Import data'),
      '#description' => t('Copy the text from an previously exported value list and paste it here.'),
      '#default_value' => isset($form_state['values']['import']) ? $form_state['values']['import'] : '',
      '#rows' => 10,
      '#states' => array(
        'visible' => array(
          ':input[name="import_type"]' => array(
            'value' => 'ctools',
          ),
        ),
      ),
    );
  }
  $form['import_values'] = array(
    '#type' => 'fieldset',
    '#title' => t('Import Values'),
    '#description' => 'Import a new value set as a flat set of key|value pairs. Useful for migrating from allowed values lists to value sets.',
    '#collapsible' => FALSE,
  );

  // Add a state for import type toggle if ctools import is also an option
  if (module_exists('ctools') && user_access('use ctools import')) {
    $form['import_values']['#states'] = array(
      'visible' => array(
        ':input[name="import_type"]' => array(
          'value' => 'values',
        ),
      ),
    );
  }
  $form['import_values']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Value set name'),
    '#description' => t('The human readable name of this value set.'),
    '#default_value' => '',
    '#weight' => -10,
  );
  $form['import_values']['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => '',
    '#weight' => -9,
    '#access' => !isset($value_set->name),
    '#after_build' => array(
      'values_import_form_after_build',
    ),
    '#machine_name' => array(
      'source' => array(
        'import_values',
        'title',
      ),
      'exists' => '_values_name_exists',
    ),
  );
  $form['import_values']['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['import_values']['values'] = array(
    '#type' => 'textarea',
    '#title' => t('Import data'),
    '#description' => t('Enter one value per line. Keys will be generated automatically. If you wish to specify the key, use the format key|value.'),
    '#default_value' => '',
    '#rows' => 10,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );
  return $form;
}