You are here

function clients_connection_form in Web Service Clients 7.3

Same name and namespace in other branches
  1. 6.2 clients.connection.admin.inc \clients_connection_form()
  2. 7.2 clients.connection.admin.inc \clients_connection_form()

Form builder for editing a connection.

Gets the class of the connection from the data in the form and calls the connectionSettingsFormAlter() method on the class to build the form. This allows different elements for different connection types.

Parameters

$connection: A connection object.

See also

clients_connection_form_submit()

1 string reference to 'clients_connection_form'
clients_entity_info in ./clients.module
Implements hook_entity_info().

File

includes/clients.connection.admin.inc, line 78
clients.connection.admin.inc Page callbacks relating to client connection admin.

Code

function clients_connection_form($form, &$form_state, $connection, $op = 'edit') {

  // Notify the user if this connection is either going to be substituted with
  // an environment-specific one, or is one that substitutes for another.
  $environment_name = variable_get('environment_name', NULL);
  if (isset($environment_name)) {

    // Create a replacement name by appending the environment name.
    $replacement_connection_name = $connection->name . '_' . $environment_name;

    // Use entity_load_single() so we bypass connection replacement.
    if ($replacement_connection = entity_load_single('clients_connection', $replacement_connection_name)) {

      // Is this going to get overlooked for another connection?
      // ie. is NAME + ENV = another connection?
      $message = t("This connection is inactive due to the site's environment setting. The <a href=\"!url\">%connection connection</a> will be substituted for it.", array(
        '!url' => url('admin/structure/clients/connections/manage/' . $replacement_connection_name),
        '%connection' => $replacement_connection->label,
      ));
    }
    elseif (substr($connection->name, -1 * strlen($environment_name)) == $environment_name) {
      $original_connection_name = substr($connection->name, 0, -1 * (strlen($environment_name) + 1));
      if ($original_connection = entity_load_single('clients_connection', $original_connection_name)) {
        $message = t("Due to the site's environment setting, this connection will substitute for the <a href=\"!url\">%connection connection</a>.", array(
          '!url' => url('admin/structure/clients/connections/manage/' . $original_connection_name),
          '%connection' => $original_connection->label,
        ));
      }
    }
    if (isset($message)) {
      drupal_set_message($message);
    }
  }
  $form = array();
  $type = $connection->type;
  $class = 'clients_connection_' . $type;
  if ($op == 'clone') {
    $connection->label .= ' (cloned)';
    $connection->name = '';
  }
  $form['#connection'] = clone $connection;
  $form['fake_type'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection type'),
    '#description' => t('The type of this connection. May not be changed.'),
    '#value' => $connection->type,
    '#size' => 50,
    '#disabled' => TRUE,
  );
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection label'),
    '#default_value' => isset($connection->label) ? $connection->label : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('The human-readable name for the connection.'),
    '#required' => TRUE,
  );

  // Machine-readable type name.
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => isset($connection->name) ? $connection->name : '',
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'clients_connection_load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this connection. It must only contain lowercase letters, numbers, and underscores.'),
  );

  /*
  if ($connection->name) {
    $form['name']['#description'] .= '<br /><strong>' . t('Warning: Changing the name of an existing connection may affect any data you have stored based on that connection.') . '</strong>';
  }
  */
  $form['endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection endpoint'),
    '#default_value' => isset($connection->endpoint) ? $connection->endpoint : '',
    '#size' => 100,
    '#maxlength' => 100,
    '#description' => t('Remote service URL e.g. http://mysite.com/services/endpoint'),
    '#required' => TRUE,
  );

  // Container for all form elements whose values should be serialized to the
  // configuration array.
  // Not a fieldset by default, but connection classes may choose to do this.
  $form['configuration'] = array(
    '#tree' => TRUE,
  );
  $form['configuration']['debug'] = array(
    '#type' => 'checkbox',
    '#title' => t('Debugging mode'),
    '#description' => t('Outputs debug data for this connection. (Not all connection types support this.)'),
    '#weight' => 100,
  );

  // Container for credentials form elements.
  $form['credentials'] = array(
    '#type' => 'fieldset',
    '#title' => t('Credentials'),
    '#tree' => TRUE,
  );
  $form['#connection_credentials_storage'] = isset($connection->configuration['credentials_storage']) ? $connection->configuration['credentials_storage'] : NULL;
  $form['credentials']['credentials_storage'] = array(
    '#type' => 'radios',
    '#title' => t('Credentials storage'),
    '#required' => TRUE,
    '#default_value' => $form['#connection_credentials_storage'],
  );

  // Get an array of the available label plugins.
  ctools_include('plugins');
  $credentials_plugins = ctools_get_plugins('clients', 'clients_credentials_storage');
  $options = array();
  foreach ($credentials_plugins as $credentials_plugin => $credentials_plugin_info) {
    $options[$credentials_plugin] = $credentials_plugin_info['label'];
    $form['credentials']['credentials_storage'][$credentials_plugin]['#description'] = $credentials_plugin_info['description'];
  }
  $form['credentials']['credentials_storage']['#options'] = $options;

  // Get the current (or default) storage plugin, and get the credentials from
  // it.
  $credentials_storage_plugin = $connection
    ->get_credentials_storage_plugin();
  $credentials_storage_plugin
    ->credentialsLoad($connection);
  $form['#connection_type'] = $type;
  $form['#connection_class'] = $class;

  // Allow the connection class to make additions to the form.
  $connection
    ->connectionSettingsFormAlter($form, $form_state);

  // Fill in the default values from the connection configuration; save the
  // handler class all the legwork.
  foreach (element_children($form['configuration']) as $configuration_key) {
    if (isset($connection->configuration[$configuration_key])) {
      $form['configuration'][$configuration_key]['#default_value'] = $connection->configuration[$configuration_key];
    }
  }

  // Fill in the default values for the credentials.
  $credentials_properties = $connection
    ->credentialsProperties();
  foreach ($credentials_properties as $property_name) {
    if (isset($connection->credentials[$property_name])) {
      $form['credentials'][$property_name]['#default_value'] = $connection->credentials[$property_name];
    }
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save connection'),
  );
  $form['actions']['delete'] = array(
    '#name' => 'delete',
    '#type' => 'submit',
    '#value' => t('Delete connection'),
  );
  return $form;
}