You are here

function wsconfig_edit_form in Web Service Data 7

Form callback: create or edit a wsconfig.

Parameters

$wsconfig: The wsconfig object to edit or for a create form an empty wsconfig object with only a wsconfig type defined.

1 call to wsconfig_edit_form()
wsconfig_form in modules/wsconfig/wsconfig.admin.inc
Entity UI Callback
1 string reference to 'wsconfig_edit_form'
wsconfig_form_wrapper in modules/wsconfig/wsconfig.admin.inc
Form callback wrapper: create or edit a wsconfig.

File

modules/wsconfig/wsconfig.admin.inc, line 62
Admin forms for wsconfig

Code

function wsconfig_edit_form($form, &$form_state, $wsconfig, $op = 'edit') {
  if (isset($_GET['remove']) and isset($wsconfig->data[$_GET['remove']])) {
    unset($wsconfig->data[$_GET['remove']]);
    $wsconfig
      ->save();
    drupal_set_message(t('Method !METHOD removed', array(
      '!METHOD' => $_GET['remove'],
    )));
  }
  $form['endpoint'] = array(
    '#type' => 'item',
    '#title' => t('Endpoint'),
    '#markup' => $wsconfig->type . ' (' . $wsconfig
      ->getEndpoint() . ')',
    '#weight' => -5,
  );

  // Add the default field elements.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Web Service Configuration Title'),
    '#default_value' => isset($wsconfig->title) ? $wsconfig->title : '',
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Web Service Configuration Machine Name'),
    '#default_value' => isset($wsconfig->name) ? $wsconfig->name : '',
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -4,
    '#machine_name' => array(
      'exists' => 'wsconfig_exists_by_name',
      'source' => array(
        'title',
      ),
    ),
  );
  foreach ($wsconfig
    ->getOperations() as $operation) {
    $op = $wsconfig
      ->getMethodKey($operation);
    $op_name = $wsconfig
      ->getMethodName($operation);
    $remove_link = l('Remove', current_path(), array(
      'query' => array(
        'remove' => $op,
      ),
    ));
    $form['data'][$op] = array(
      '#type' => 'textfield',
      '#title' => t('!NAME Data Method (!OP) - !REMOVE', array(
        '!NAME' => $op_name,
        '!OP' => $op,
        '!REMOVE' => $remove_link,
      )),
      '#description' => t('The method call for "!OP". If keys are included in the method name, include them as tokens starting with a "%".  Example: user/%id/course', array(
        '!OP' => $operation,
      )),
      '#size' => 80,
      '#maxlength' => 255,
      '#default_value' => !empty($wsconfig->data[$op]) ? $wsconfig->data[$op] : '',
    );
    $form['data'][$op . '_options'] = array(
      '#type' => 'textfield',
      '#title' => t('Options'),
      '#description' => t('Options as a JSON array  (Example: {"headers":{"Content-Type":"application/json"}} )'),
      '#default_value' => isset($form_state['values'][$op . '_options']) ? $form_state['values'][$op . '_options'] : isset($wsconfig->data['options'][$op]) ? drupal_json_encode($wsconfig->data['options'][$op]) : '',
      '#element_validate' => array(
        '_wsconfig_element_validate_json',
      ),
      '#maxlength' => 4096,
    );
  }
  if (!empty($wsconfig->wsconfig_id)) {
    $addmethod_items = $wsconfig
      ->getPossibleMethods();
    if (count($addmethod_items) > 0) {
      $form['addmethod'] = array(
        '#type' => 'select',
        '#title' => t('Add method'),
        '#options' => $wsconfig
          ->getPossibleMethods(),
      );
      $allmethods = $wsconfig->connector
        ->getMethods();
      foreach ($allmethods['multiple'] as $key => $method) {
        $form['addmethod_name_' . $key] = array(
          '#type' => 'textfield',
          '#title' => t('!METHOD Name', array(
            '!METHOD' => $method,
          )),
          '#description' => t('Must contain only lowercase letters, numbers, or underscores'),
          '#states' => array(
            'visible' => array(
              ':input[name="addmethod"]' => array(
                'value' => $key,
              ),
            ),
          ),
        );
      }
      $form['addmethod_submit'] = array(
        '#type' => 'submit',
        '#value' => t('Add method'),
      );
    }
  }
  if ($wsconfig->connector
    ->supportsCaching()) {
    $form['cache_default_time'] = array(
      '#type' => 'textfield',
      '#title' => t('Default Cache Time'),
      '#description' => t('Number of seconds query results should be cached for.'),
      '#default_value' => isset($wsconfig->data['cache_default_time']) ? $wsconfig->data['cache_default_time'] : 0,
    );
    $form['cache_default_override'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override Cache Time'),
      '#description' => t('Override cache time with default value even if another value is returned by the service.'),
      '#default_value' => isset($wsconfig->data['cache_default_override']) ? $wsconfig->data['cache_default_override'] : 0,
    );
    $form['stale_cache'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow stale cache'),
      '#description' => t('Returns expired cache entries when the service is unavailable (if supported by the connector.)'),
      '#default_value' => isset($wsconfig->data['stale_cache']) ? $wsconfig->data['stale_cache'] : 0,
    );
  }

  // Add the field related form elements.
  $form_state['wsconfig'] = $wsconfig;
  field_attach_form('wsconfig', $wsconfig, $form, $form_state);
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 400,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Web Service Configuration'),
    '#submit' => $submit + array(
      'wsconfig_edit_form_submit',
    ),
  );
  if (!empty($wsconfig->name)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete Web Service Configuration'),
      '#suffix' => l(t('Cancel'), 'admin/structure/wsconfig'),
      '#submit' => $submit + array(
        'wsconfig_form_submit_delete',
      ),
      '#weight' => 45,
    );
  }

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'wsconfig_edit_form_validate';
  $form['#submit'][] = 'wsconfig_edit_form_submit';
  return $form;
}