You are here

function search_api_admin_add_server in Search API 7

Form callback showing a form for adding a server.

1 string reference to 'search_api_admin_add_server'
search_api_menu in ./search_api.module
Implements hook_menu().

File

./search_api.admin.inc, line 173
Administration page callbacks for the Search API module.

Code

function search_api_admin_add_server(array $form, array &$form_state) {
  drupal_set_title(t('Add server'));
  $class = empty($form_state['values']['class']) ? '' : $form_state['values']['class'];
  $form_state['server'] = entity_create('search_api_server', array());
  if (empty($form_state['storage']['step_one'])) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Server name'),
      '#description' => t('Enter the displayed name for the new server.'),
      '#maxlength' => 50,
      '#required' => TRUE,
    );
    $form['machine_name'] = array(
      '#type' => 'machine_name',
      '#maxlength' => 50,
      '#machine_name' => array(
        'exists' => 'search_api_server_load',
      ),
    );
    $form['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enabled'),
      '#description' => t('Select if the new server will be enabled after creation.'),
      '#default_value' => TRUE,
    );
    $form['description'] = array(
      '#type' => 'textarea',
      '#title' => t('Server description'),
      '#description' => t('Enter a description for the new server.'),
    );
    $form['class'] = array(
      '#type' => 'select',
      '#title' => t('Service class'),
      '#description' => t('Choose a service class to use for this server.'),
      '#options' => array(
        '' => '< ' . t('Choose a service class') . ' >',
      ),
      '#required' => TRUE,
      '#default_value' => $class,
      '#ajax' => array(
        'callback' => 'search_api_admin_add_server_ajax_callback',
        'wrapper' => 'search-api-class-options',
      ),
    );
  }
  elseif (!$class) {
    $class = $form_state['storage']['step_one']['class'];
  }
  foreach (search_api_get_service_info() as $id => $info) {
    if (empty($form_state['storage']['step_one'])) {
      $form['class']['#options'][$id] = $info['name'];
    }
    if (!$class || $class != $id) {
      continue;
    }
    $service = NULL;
    if (class_exists($info['class'])) {
      $service = new $info['class']($form_state['server']);
    }
    if (!$service instanceof SearchApiServiceInterface) {
      watchdog('search_api', t('Service class @id specifies an illegal class: @class', array(
        '@id' => $id,
        '@class' => $info['class'],
      )), NULL, WATCHDOG_ERROR);
      continue;
    }
    $service_form = isset($form['options']['form']) ? $form['options']['form'] : array();
    $service_form = $service
      ->configurationForm($service_form, $form_state);
    $form['options']['form'] = $service_form ? $service_form : array(
      '#markup' => t('There are no configuration options for this service class.'),
    );
    $form['options']['class']['#type'] = 'value';
    $form['options']['class']['#value'] = $class;
    $form['options']['#type'] = 'fieldset';
    $form['options']['#tree'] = TRUE;
    $form['options']['#collapsible'] = TRUE;
    $form['options']['#title'] = $info['name'];
    $form['options']['#description'] = $info['description'];
  }
  $form['options']['#prefix'] = '<div id="search-api-class-options">';
  $form['options']['#suffix'] = '</div>';

  // If $info is not set, there are no service classes. Display an error message
  // telling the user how to change that and return an empty form.
  if (!isset($info)) {
    drupal_set_message(t('There are no service classes available for the Search API. Please install a <a href="@url">module that provides a service class</a> to proceed.', array(
      '@url' => url('https://www.drupal.org/node/1254698'),
    )), 'error');
    return array();
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create server'),
  );
  return $form;
}