You are here

function key_config_form in Key 7.2

Same name and namespace in other branches
  1. 7.3 includes/key.admin.inc \key_config_form()

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_config_form'
key_menu in ./key.module
Implements hook_menu().

File

includes/key.admin.inc, line 69
Administrative functionality for managing key configurations.

Code

function 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);

  // Store the original key in the form.
  if (!empty($config['name'])) {
    $key_value = key_get_key($config['name']);
    $form['#original_key'] = array(
      'config' => $config,
      'key_value' => $key_value,
    );
    if (!empty($key_value) && ($key_value_obscure = ctools_plugin_get_function($providers[$config['provider']], 'key value obscure'))) {
      $form['#original_key']['key_value_obscured'] = call_user_func($key_value_obscure, $key_value, $config);
    }
  }

  // Determine the current key provider.
  if (isset($form_state['values']['provider'])) {
    $provider = $form_state['values']['provider'];
  }
  elseif (isset($config['provider'])) {
    $provider = $config['provider'];
  }
  else {
    $provider = NULL;
  }
  $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_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' => 'textfield',
    '#default_value' => $config['description'],
    '#description' => t('A short description of the key.'),
  );
  $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_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,
  );
  $form['provider_extras']['key_value'] = array(
    '#type' => 'container',
    '#title' => t('Key value'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );

  // Unset the input values for provider settings and key value,
  // so they can be updated via AJAX.
  unset($form_state['input']['provider_settings']);
  unset($form_state['input']['key_value']);
  if ($provider) {

    // Add the Base64 checkbox to the settings, if allowed.
    if ($providers[$provider]['allow base64 encoding']) {
      $form['provider_extras']['provider_settings']['base64_encoded'] = array(
        '#type' => 'checkbox',
        '#title' => t('Base64-encoded'),
        '#description' => t('Check this if the key has been Base64-encoded. If the key should be used as-is, without Base64-decoding, leave this unchecked.'),
        '#default_value' => isset($config['provider_settings']['base64_encoded']) ? $config['provider_settings']['base64_encoded'] : 0,
      );
    }

    // Add any additional provider settings.
    if ($provider_settings_form = ctools_plugin_get_function($providers[$provider], 'provider settings form')) {
      $form['provider_extras']['provider_settings'] += call_user_func($provider_settings_form, $config['provider_settings']);
    }

    // If there are any provider settings, make the container a fieldset.
    if (element_children($form['provider_extras']['provider_settings'])) {
      $form['provider_extras']['provider_settings']['#type'] = 'fieldset';
    }

    // Add the value form, if defined.
    if ($provider && ($key_value_form = ctools_plugin_get_function($providers[$provider], 'key value form'))) {
      $form['provider_extras']['key_value']['#type'] = 'fieldset';

      // Determine what to display for the key value, if anything.
      if (isset($form['#original_key']['key_value_obscured'])) {
        $key_value = $provider == $config['provider'] ? $form['#original_key']['key_value_obscured'] : '';
      }
      else {
        $key_value = isset($form['#original_key']['key_value']) ? $form['#original_key']['key_value'] : '';
      }
      $form['provider_extras']['key_value'] += call_user_func($key_value_form, array(
        'key_value' => $key_value,
      ));
    }
  }
  $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_config_form_submit',
    ),
    '#weight' => 40,
  );
  if (isset($config['name'])) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete key'),
      '#submit' => array(
        'key_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;
}