You are here

function key_ui_key_config_form in Key 7

Form constructor for the key configuration edit form.

Parameters

array $config: (optional) An array representing the configuration, when editing an existing configuration.

1 string reference to 'key_ui_key_config_form'
key_ui_menu in modules/key_ui/key_ui.module
Implements hook_menu().

File

modules/key_ui/includes/key_ui.admin.inc, line 87
Administrative functionality for managing key configurations.

Code

function key_ui_key_config_form($form, &$form_state, $config = NULL) {

  // Clear the plugin cache on the first page load, but not on AJAX refreshes.
  if (!isset($form_state['values'])) {
    _key_clear_plugin_cache();
  }

  // Get all valid key providers.
  $providers = key_get_providers(FALSE);

  // Get all valid key providers as options.
  $provider_options = key_get_providers_as_options(FALSE);

  // Determine the key provider.
  if (isset($form_state['values']['provider'])) {
    $provider = $form_state['values']['provider'];
  }
  elseif (isset($config['provider'])) {
    $provider = $config['provider'];
  }
  else {
    $provider = NULL;
  }

  // Get all valid types as options.
  $type_options = key_get_types_as_options(FALSE);
  $form['label'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $config['label'],
    '#description' => t('The human-readable name of the key.'),
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $config['name'],
    '#maxlength' => 32,
    '#disabled' => isset($config['name']),
    '#machine_name' => array(
      'exists' => 'key_ui_config_load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for the key. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $config['description'],
    '#description' => t('A short description of the key.'),
  );
  $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#description' => t('The type of key.'),
    '#required' => TRUE,
    '#options' => $type_options,
    '#default_value' => $config['type'],
  );
  $form['provider'] = array(
    '#type' => 'select',
    '#title' => t('Key provider'),
    '#description' => t('The key provider to use.'),
    '#required' => TRUE,
    '#options' => $provider_options,
    '#default_value' => $config['provider'],
    '#ajax' => array(
      'method' => 'replace',
      'callback' => 'key_ui_provider_extras_ajax',
      'wrapper' => 'key-provider-extras-wrapper',
    ),
  );
  $form['provider_extras'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="key-provider-extras-wrapper">',
    '#suffix' => '</div>',
  );
  $form['provider_extras']['provider_settings'] = array(
    '#type' => 'container',
    '#title' => t('Key provider settings'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  if ($provider && ($provider_settings_form = ctools_plugin_get_function($providers[$provider], 'settings form'))) {
    $form['provider_extras']['provider_settings']['#type'] = 'fieldset';
    $form['provider_extras']['provider_settings'] += call_user_func($provider_settings_form, $config['provider_settings']);
  }
  $form['provider_extras']['provider_key'] = array(
    '#type' => 'container',
  );
  if ($provider && ($provider_key_form = ctools_plugin_get_function($providers[$provider], 'key form'))) {
    $key = !empty($config['name']) ? key_get_key($config['name']) : '';
    $form['provider_extras']['provider_key'] += call_user_func($provider_key_form, array(
      'key' => $key,
    ));
  }
  $form['provider_extras']['provider_instructions'] = array(
    '#type' => 'container',
    '#title' => t('Instructions'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  if ($provider && ($provider_instructions = ctools_plugin_get_function($providers[$provider], 'instructions'))) {
    $form['provider_extras']['provider_instructions']['#type'] = 'fieldset';
    $form['provider_extras']['provider_instructions'] += call_user_func($provider_instructions);
  }
  $form['created'] = array(
    '#type' => 'value',
    '#value' => $config['created'],
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save key'),
    '#submit' => array(
      'key_ui_key_config_form_submit',
    ),
    '#weight' => 40,
  );
  if (isset($config['name'])) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete key'),
      '#submit' => array(
        'key_ui_config_form_delete_submit',
      ),
      '#limit_validation_errors' => array(),
      '#weight' => 45,
    );
  }
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => KEY_MENU_PATH,
    '#weight' => 50,
  );
  return $form;
}