You are here

function key_config_form in Key 7.3

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

Form constructor for the key configuration form.

Parameters

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

Return value

array The key configuration form.

1 string reference to 'key_config_form'
key_menu in ./key.module
Implements hook_menu().

File

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

Code

function key_config_form($form, &$form_state, $config = array()) {

  // Add the defaults to the configuration, if they are needed.
  $config += _key_config_defaults();

  // If the form is rebuilding.
  if ($form_state['rebuild']) {
    $fields = array(
      'key_type',
      'key_type_settings',
      'key_provider',
      'key_provider_settings',
    );

    // Get current values from the form state.
    foreach ($fields as $field) {
      if (isset($form_state['values'][$field])) {
        $config[$field] = $form_state['values'][$field];
      }
    }

    // If a key type change triggered the rebuild.
    if ($form_state['triggering_element']['#name'] == 'key_type') {

      // Update the type and input plugins.
      _key_update_key_type($form_state, $config);
      _key_update_key_input($form_state, $config);
    }

    // If a key provider change triggered the rebuild.
    if ($form_state['triggering_element']['#name'] == 'key_provider') {

      // Update the provider and input plugins.
      _key_update_key_provider($form_state, $config);
      _key_update_key_input($form_state, $config);
    }
  }
  else {

    // Clear the plugin cache.
    _key_clear_plugin_cache();

    // Store the original key configuration.
    $form_state['storage']['original_key'] = empty($config['id']) ? NULL : $config;

    // Store the key value data in form state for use by plugins.
    $form_state['storage']['key_value'] = _key_set_key_value_data($config);

    // Update the input plugin.
    _key_update_key_input($form_state, $config);
  }

  // Store the current key configuration.
  $form_state['storage']['key_config'] = $config;

  // Load the plugins.
  $key_type = key_get_plugin('key_type', $config['key_type']);
  $key_provider = key_get_plugin('key_provider', $config['key_provider']);
  $form['label'] = array(
    '#title' => t('Key name'),
    '#type' => 'textfield',
    '#default_value' => $config['label'],
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $config['id'],
    '#maxlength' => 32,
    '#disabled' => !empty($config['id']),
    '#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.'),
  );

  // This is the element that contains all of the dynamic parts of the form.
  $form['settings'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="key-settings">',
    '#suffix' => '</div>',
  );

  // Key type section.
  $form['settings']['type_section'] = array(
    '#type' => 'fieldset',
    '#title' => t('Type settings'),
    '#collapsible' => TRUE,
  );
  $form['settings']['type_section']['key_type'] = array(
    '#type' => 'select',
    '#title' => t('Key type'),
    '#options' => key_get_plugins_as_options('key_type', FALSE),
    '#required' => TRUE,
    '#default_value' => $config['key_type'],
    '#ajax' => array(
      'callback' => '_key_config_form_ajax_update_settings',
      'event' => 'change',
      'wrapper' => 'key-settings',
    ),
  );
  $form['settings']['type_section']['key_type_description'] = array(
    '#markup' => $key_type['description'],
  );
  $form['settings']['type_section']['key_type_settings'] = array(
    '#type' => 'container',
    '#title' => t('Key type settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  );
  if ($key_type_settings_form = ctools_plugin_get_function(key_get_plugin('key_type', $config['key_type']), 'build configuration form')) {
    $plugin_form_state = _key_create_plugin_form_state('key_type', $form_state);
    $form['settings']['type_section']['key_type_settings'] += $key_type_settings_form(array(), $plugin_form_state);
    $form_state['values']['key_type_settings'] = $plugin_form_state['values'];
  }

  // Key provider section.
  $form['settings']['provider_section'] = array(
    '#type' => 'fieldset',
    '#title' => t('Provider settings'),
    '#collapsible' => TRUE,
  );
  $form['settings']['provider_section']['key_provider'] = array(
    '#type' => 'select',
    '#title' => t('Key provider'),
    '#options' => key_get_plugins_as_options('key_provider', FALSE),
    '#required' => TRUE,
    '#default_value' => $config['key_provider'],
    '#ajax' => array(
      'callback' => '_key_config_form_ajax_update_settings',
      'event' => 'change',
      'wrapper' => 'key-settings',
    ),
  );
  $form['settings']['provider_section']['key_provider_description'] = array(
    '#markup' => $key_provider['description'],
  );
  $form['settings']['provider_section']['key_provider_settings'] = array(
    '#type' => 'container',
    '#title' => t('Key provider settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  );
  if ($key_provider_settings_form = ctools_plugin_get_function(key_get_plugin('key_provider', $config['key_provider']), 'build configuration form')) {
    $plugin_form_state = _key_create_plugin_form_state('key_provider', $form_state);
    $form['settings']['provider_section']['key_provider_settings'] += $key_provider_settings_form(array(), $plugin_form_state);
    $form_state['values']['key_provider_settings'] = $plugin_form_state['values'];
  }

  // Key input section.
  $form['settings']['input_section'] = array(
    '#type' => 'fieldset',
    '#title' => t('Value'),
    '#collapsible' => TRUE,
  );
  $form['settings']['input_section']['key_input'] = array(
    '#type' => 'value',
    '#value' => $config['key_input'],
  );
  $form['settings']['input_section']['key_input_settings'] = array(
    '#type' => 'container',
    '#title' => t('Key input settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  );
  if ($key_input_settings_form = ctools_plugin_get_function(key_get_plugin('key_input', $config['key_input']), 'build configuration form')) {
    $plugin_form_state = _key_create_plugin_form_state('key_input', $form_state);
    $form['settings']['input_section']['key_input_settings'] += $key_input_settings_form(array(), $plugin_form_state);
    $form_state['values']['key_input_settings'] = $plugin_form_state['values'];
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save key'),
    '#submit' => array(
      'key_config_form_submit',
    ),
    '#weight' => 40,
  );
  if (!empty($config['id'])) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete key'),
      '#submit' => array(
        'key_config_form_delete_submit',
      ),
      '#limit_validation_errors' => array(),
      '#weight' => 45,
    );
  }
  return $form;
}