You are here

function search_api_page_admin_add in Search API Pages 7

Displays a form for adding a search page.

1 string reference to 'search_api_page_admin_add'
search_api_page_menu in ./search_api_page.module
Implements hook_menu().

File

./search_api_page.admin.inc, line 58
Admin page callbacks for the Search pages module.

Code

function search_api_page_admin_add(array $form, array &$form_state) {
  $form = array();
  if (empty($form_state['step_one'])) {
    $indexes = search_api_index_load_multiple(FALSE);
    if (!$indexes) {
      drupal_set_message(t('There are no searches indexes which can be searched. Please <a href="@url">create an index</a> first.', array(
        '@url' => url('admin/config/search/search_api/add_index'),
      )), 'warning');
      return array();
    }
    $index_options = array();
    foreach ($indexes as $index) {
      if ($index->enabled) {
        $index_options[$index->machine_name] = $index->name;
      }
    }
    foreach ($indexes as $index) {
      if (!$index->enabled) {
        $index_options[$index->machine_name] = $index->name . ' (' . t('disabled') . ')';
      }
    }
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Search name'),
      '#maxlength' => 50,
      '#required' => TRUE,
    );
    $form['machine_name'] = array(
      '#type' => 'machine_name',
      '#maxlength' => 32,
      '#machine_name' => array(
        'exists' => 'search_api_page_load',
      ),
    );
    $form['index_id'] = array(
      '#type' => 'select',
      '#title' => t('Index'),
      '#description' => t('Select the index this page should search. This cannot be changed later.'),
      '#options' => $index_options,
      '#required' => TRUE,
    );
    $form['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enabled'),
      '#description' => t('This will only take effect if the selected index is also enabled.'),
      '#default_value' => TRUE,
    );
    $form['description'] = array(
      '#type' => 'textarea',
      '#title' => t('Search description'),
    );
    $form['path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path'),
      '#description' => t('Set the path under which the search page will be accessible, when enabled.'),
      '#maxlength' => 50,
      '#required' => TRUE,
    );
    $form['empty_behavior'] = array(
      '#type' => 'radios',
      '#title' => t('Behavior on empty search'),
      '#options' => array(
        '' => t('Just show the search box'),
        'results' => t('Show the first page of all available results'),
        'blocks' => t("Perform an empty search but don't display any results."),
      ),
      '#description' => t('This determines what is shown when the user first comes to the search page or submits an empty keyword string. For example if you want <a href="@url">Facet API</a> facets to show without having entered any search terms, select the "Perform an empty search …" option. If you would like a page of results displayed regardless of the presence of any search terms then select the "Show the first page …" option.', array(
        '@url' => 'https://drupal.org/project/facetapi',
      )),
      '#default_value' => '',
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create page'),
    );
    return $form;
  }
  $index = search_api_index_load($form_state['step_one']['index_id']);
  if ($index->enabled) {
    $modes = array();
    foreach ($index
      ->query()
      ->parseModes() as $mode => $info) {
      $modes[$mode] = $info['name'];
    }
  }
  else {
    $modes = array();
    $modes['direct'] = t('Direct query');
    $modes['single'] = t('Single term');
    $modes['terms'] = t('Multiple terms');
  }
  $form['mode'] = array(
    '#type' => 'select',
    '#title' => t('Query type'),
    '#description' => t('Select how the query will be parsed.'),
    '#options' => $modes,
    '#default_value' => 'terms',
  );
  $fields = array();
  $index_fields = $index
    ->getFields();
  foreach ($index
    ->getFulltextFields() as $name) {
    $fields[$name] = $index_fields[$name]['name'];
  }
  if (count($fields) > 1) {
    $form['fields'] = array(
      '#type' => 'select',
      '#title' => t('Searched fields'),
      '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
      '#options' => $fields,
      '#size' => min(4, count($fields)),
      '#multiple' => TRUE,
    );
  }
  else {
    $form['fields'] = array(
      '#type' => 'value',
      '#value' => array(),
    );
  }
  $form['max_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum keywords length'),
    '#description' => t('The maximum number of characters that should be allowed as search keywords. Set to an empty string to not have any limit.'),
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#default_value' => 128,
  );
  $form['per_page'] = array(
    '#type' => 'select',
    '#title' => t('Results per page'),
    '#description' => t('Select how many items will be displayed on one page of the search result.'),
    '#options' => drupal_map_assoc(array(
      5,
      10,
      20,
      30,
      40,
      50,
      60,
      80,
      100,
    )),
    '#default_value' => 10,
  );
  $form['get_per_page'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow GET override'),
    '#description' => t('Allow the „Results per page“ setting to be overridden from the URL, using the "per_page" GET parameter.<br />' . 'Example: http://example.com/search_results?per_page=7'),
    '#default_value' => FALSE,
  );
  $form['result_page_search_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show search form on result page'),
    '#description' => t('Enable or disable the search form on the result page'),
    '#default_value' => TRUE,
  );
  $view_modes = array(
    'search_api_page_result' => t('Themed as search results'),
  );

  // For entities, we also add all entity view modes.
  if ($index
    ->getEntityType() && ($entity_info = entity_get_info($index
    ->getEntityType()))) {
    foreach ($entity_info['view modes'] as $mode => $mode_info) {
      $view_modes[$mode] = $mode_info['label'];
    }
  }
  if (count($view_modes) > 1) {
    $form['view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#options' => $view_modes,
      '#description' => t('Select how search results will be displayed.'),
      '#size' => 1,
      '#default_value' => 'search_api_page_result',
    );
  }
  else {
    $form['view_mode'] = array(
      '#type' => 'value',
      '#value' => key($view_modes),
    );
  }
  $languages = array(
    'current' => t("Current user's language"),
    'default' => t('Default site language'),
  );
  $languages += entity_metadata_language_list();
  if (count($languages) > 4) {
    $form['language_filter'] = array(
      '#type' => 'select',
      '#title' => t('Language filter'),
      '#description' => t('Filter the results by language. Select none to show results independent of language.'),
      '#options' => $languages,
      '#multiple' => TRUE,
      '#size' => min(4, count($languages)),
      '#default_value' => array(),
    );
  }
  if (module_exists('search_api_spellcheck') && ($server = $index
    ->server()) && $server
    ->supportsFeature('search_api_spellcheck')) {
    $form['search_api_spellcheck'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable spell check'),
      '#description' => t('Display "Did you mean … ?" above search results.'),
      '#default_value' => TRUE,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create page'),
  );
  return $form;
}