You are here

public function WSClientRESTEndpoint::formAlter in Web service client 7

Adds options to the Web Service config form specific to the REST service.

Allows the user to choose which formatter (JSO, PHP, XML) to use for requesting or parsing

Overrides WSClientEndpoint::formAlter

See also

WSClientEndpoint::formAlter($form, $form_state)

File

wsclient_rest/wsclient_rest.inc, line 146
Web service client REST - include file.

Class

WSClientRESTEndpoint
A remote endpoint type for invoking REST services.

Code

public function formAlter(&$form, &$form_state) {
  $service = $form_state['service'];
  $options = wsclient_rest_formatters_as_options();
  switch ($form_state['form']) {
    case 'main':
      $default_formatter = NULL;
      foreach (array(
        'send_formatter',
        'receive_formatter',
      ) as $formatter_type) {
        if (wsclient_rest_has_custom_formatter($service->settings, $formatter_type)) {
          $default_formatter = 'custom';
        }
        else {
          $default_formatter = isset($service->settings[$formatter_type]['class']) ? $service->settings[$formatter_type]['class'] : 'WsclientRestJSONFormatter';
        }
        switch ($formatter_type) {
          case 'send_formatter':
            $title = t('Request formatter (send_formatter)');
            $description = t('Choose how to serialize the request.');
            $weight = 50;
            break;
          case 'receive_formatter':
            $title = t('Response formatter (receive_formatter)');
            $description = t('Choose how to parse the response.');
            $weight = 55;
            break;
        }
        $form['settings'][$formatter_type] = array(
          '#type' => 'fieldset',
          '#title' => $title,
          '#tree' => TRUE,
          '#weight' => $weight,
        );
        $form['settings'][$formatter_type]['class'] = array(
          '#type' => 'select',
          '#title' => t('Formatter'),
          '#default_value' => $default_formatter,
          '#description' => $description,
          '#options' => $options,
        );
      }
      break;
    case 'operation':
      $operation = $form_state['operation'];
      $form['type'] = array(
        '#type' => 'select',
        '#title' => t('HTTP Method'),
        '#default_value' => isset($operation['type']) ? $operation['type'] : 'GET',
        '#description' => t('Specify the HTTP request method used for this operation.'),
        '#options' => array(
          'GET' => 'GET',
          'POST' => 'POST',
          'PUT' => 'PUT',
          'DELETE' => 'DELETE',
        ),
        '#weight' => -5,
      );
      $form['url'] = array(
        '#type' => 'textfield',
        '#title' => t('Path'),
        '#default_value' => isset($operation['url']) ? $operation['url'] : '',
        '#description' => t('The path to append to the services base URL. In order to construct the path using parameter values make use of replacements in the form "@parameter-name". E.g. the path "node/@nid" together with a "nid" parameter could be used to construct the path to a Drupal node.'),
        '#weight' => -5,
      );
      break;
  }

  // Allow formatters to alter the form.
  foreach ($options as $options_groupped) {
    foreach (array_keys($options_groupped) as $formatter_class) {
      if ($formatter_class == 'custom') {

        // Skip custom.
        continue;
      }
      $formatter = new $formatter_class();
      if (method_exists($formatter, 'formAlter')) {
        $formatter
          ->formAlter($form, $form_state);
      }
    }
  }
  $form['#submit'][] = 'wsclient_rest_form_submit';
}