You are here

function _search_api_saved_searches_create_search_form in Search API Saved Searches 7

Helper function for creating a form for manually creating a saved search.

1 call to _search_api_saved_searches_create_search_form()
search_api_saved_searches_save_form in ./search_api_saved_searches.module
Form builder for creating a new saved search.

File

./search_api_saved_searches.module, line 896
Offers the ability to save searches and be notified of new results.

Code

function _search_api_saved_searches_create_search_form(SearchApiSavedSearchesSettings $settings) {
  $index = $settings
    ->index();
  $wrapper = $index
    ->entityWrapper();
  $options = isset($settings->options['manual']) ? $settings->options['manual'] : array();
  $form['#tree'] = TRUE;
  $form['fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search'),
  );
  if (!empty($options['fulltext'])) {
    $form['fields']['search_api_saved_searches_fulltext'] = array(
      '#type' => 'textfield',
      '#title' => t('Keywords'),
    );
  }
  if (!empty($options['fields'])) {
    foreach ($options['fields'] as $field) {
      if (!empty($index->options['fields'][$field])) {

        // Extract the necessary field information out of the wrapper.
        $tmp = $wrapper;
        foreach (explode(':', $field) as $part) {
          if (!isset($tmp->{$part})) {
            continue 2;
          }
          $tmp = $tmp->{$part};
        }
        $info = $tmp
          ->info();
        $form['fields'][$field]['#title'] = isset($info['label']) ? $info['label'] : $field;
        if ($optList = $tmp
          ->optionsList('view')) {
          $optList = array(
            NULL => t('- Any -'),
          ) + $optList;
          $form['fields'][$field]['#type'] = 'select';
          $form['fields'][$field]['#options'] = $optList;
        }
        else {
          $form['fields'][$field]['#type'] = 'textfield';
        }
      }
    }
  }
  return $form;
}