From 256475e7169fad9b5798ed800cbb6bdf2b4b03dd Mon Sep 17 00:00:00 2001 From: bachy Date: Sun, 22 Jul 2012 16:27:00 +0200 Subject: [PATCH] disable form patch http://drupal.org/node/1512086 Signed-off-by: bachy --- disable_form-1512086-0_1.patch | 53 +++++++ search_api_page.admin.inc | 16 +++ search_api_page.pages.inc | 4 +- search_api_page.pages.inc.orig | 248 +++++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 disable_form-1512086-0_1.patch create mode 100644 search_api_page.pages.inc.orig diff --git a/disable_form-1512086-0_1.patch b/disable_form-1512086-0_1.patch new file mode 100644 index 00000000..7cdf3883 --- /dev/null +++ b/disable_form-1512086-0_1.patch @@ -0,0 +1,53 @@ +diff --git a/search_api_page.admin.inc b/search_api_page.admin.inc +old mode 100644 +new mode 100755 +index 013672e..a08ee06 +--- a/search_api_page.admin.inc ++++ b/search_api_page.admin.inc +@@ -177,6 +177,14 @@ function search_api_page_admin_add(array $form, array &$form_state) { + '#default_value' => 10, + ); + ++ $form['result_page_search_form'] = array( ++ '#type' => 'radios', ++ '#title' => t('Search form on result page'), ++ '#default_value' => 1, ++ '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), ++ '#description' => t('Enable or disable the search form on the result page'), ++ ); ++ + $form['get_per_page'] = array( + '#type' => 'checkbox', + '#title' => t('Allow GET override'), +@@ -359,6 +367,14 @@ function search_api_page_admin_edit(array $form, array &$form_state, Entity $pag + '#default_value' => $page->options['per_page'], + ); + ++ $form['result_page_search_form'] = array( ++ '#type' => 'radios', ++ '#title' => t('Search form on result page'), ++ '#default_value' => $page->options['per_page'], ++ '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), ++ '#description' => t('Enable or disable the search form on the result page'), ++ ); ++ + $form['options']['get_per_page'] = array( + '#type' => 'checkbox', + '#title' => t('Allow GET override'), +diff --git a/search_api_page.pages.inc b/search_api_page.pages.inc +old mode 100644 +new mode 100755 +index 23f7b30..d43a465 +--- a/search_api_page.pages.inc ++++ b/search_api_page.pages.inc +@@ -22,7 +22,9 @@ function search_api_page_view($id, $keys = NULL) { + $page->options['per_page'] = (int) $_GET['per_page']; + } + +- $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys); ++ if ($page->options['result_page_search_form']) { ++ $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys); ++ } + + if ($keys) { + $results = search_api_page_search_execute($page, $keys); diff --git a/search_api_page.admin.inc b/search_api_page.admin.inc index 013672ee..a08ee060 100644 --- a/search_api_page.admin.inc +++ b/search_api_page.admin.inc @@ -177,6 +177,14 @@ function search_api_page_admin_add(array $form, array &$form_state) { '#default_value' => 10, ); + $form['result_page_search_form'] = array( + '#type' => 'radios', + '#title' => t('Search form on result page'), + '#default_value' => 1, + '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), + '#description' => t('Enable or disable the search form on the result page'), + ); + $form['get_per_page'] = array( '#type' => 'checkbox', '#title' => t('Allow GET override'), @@ -359,6 +367,14 @@ function search_api_page_admin_edit(array $form, array &$form_state, Entity $pag '#default_value' => $page->options['per_page'], ); + $form['result_page_search_form'] = array( + '#type' => 'radios', + '#title' => t('Search form on result page'), + '#default_value' => $page->options['per_page'], + '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), + '#description' => t('Enable or disable the search form on the result page'), + ); + $form['options']['get_per_page'] = array( '#type' => 'checkbox', '#title' => t('Allow GET override'), diff --git a/search_api_page.pages.inc b/search_api_page.pages.inc index 7b72a0db..7de2e9cd 100644 --- a/search_api_page.pages.inc +++ b/search_api_page.pages.inc @@ -22,7 +22,9 @@ function search_api_page_view($id, $keys = NULL) { $page->options['per_page'] = (int) $_GET['per_page']; } - $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys); + if ($page->options['result_page_search_form']) { + $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys); + } if ($keys) { try { diff --git a/search_api_page.pages.inc.orig b/search_api_page.pages.inc.orig new file mode 100644 index 00000000..7b72a0db --- /dev/null +++ b/search_api_page.pages.inc.orig @@ -0,0 +1,248 @@ +options['get_per_page']) && ((int) $_GET['per_page']) > 0) { + // Remember and later restore the true setting value so we don't + // accidentally permanently save the altered one. + $page->options['original_per_page'] = $page->options['per_page']; + $page->options['per_page'] = (int) $_GET['per_page']; + } + + $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys); + + if ($keys) { + try { + $results = search_api_page_search_execute($page, $keys); + } + catch (SearchApiException $e) { + $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.'); + watchdog('search_api_page', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q'])); + } + + // If spellcheck results are returned then add them to the render array. + if (isset($results['search_api_spellcheck'])) { + $ret['search_api_spellcheck']['#theme'] = 'search_api_spellcheck'; + $ret['search_api_spellcheck']['#spellcheck'] = $results['search_api_spellcheck']; + // Let the theme function know where the key is stored by passing its arg + // number. We can work this out from the number of args in the page path. + $ret['search_api_spellcheck']['#options'] = array( + 'arg' => array(count(arg(NULL, $page->path))), + ); + } + + $ret['results']['#theme'] = 'search_api_page_results'; + $ret['results']['#index'] = search_api_index_load($page->index_id); + $ret['results']['#results'] = $results; + $ret['results']['#view_mode'] = isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result'; + $ret['results']['#keys'] = $keys; + + if ($results['result count'] > $page->options['per_page']) { + pager_default_initialize($results['result count'], $page->options['per_page']); + $ret['pager']['#theme'] = 'pager'; + $ret['pager']['#quantity'] = 9; + } + + if (!empty($results['ignored'])) { + drupal_set_message(t('The following search keys are too short or too common and were therefore ignored: "@list".', array('@list' => implode(t('", "'), $results['ignored']))), 'warning'); + } + if (!empty($results['warnings'])) { + foreach ($results['warnings'] as $warning) { + drupal_set_message($warning, 'warning'); + } + } + } + + if (isset($page->options['original_per_page'])) { + $page->options['per_page'] = $page->options['original_per_page']; + unset($page->options['original_per_page']); + } + + return $ret; +} + +/** + * Executes a search. + * + * @param Entity $page + * The page for which a search should be executed. + * @param $keys + * The keywords to search for. + * + * @return array + * The search results as returned by SearchApiQueryInterface::execute(). + */ +function search_api_page_search_execute(Entity $page, $keys) { + $limit = $page->options['per_page']; + $offset = pager_find_page() * $limit; + $options = array( + 'search id' => 'search_api_page:' . $page->path, + 'parse mode' => $page->options['mode'], + ); + + if (!empty($page->options['search_api_spellcheck'])) { + $options['search_api_spellcheck'] = TRUE; + } + + $query = search_api_query($page->index_id, $options) + ->keys($keys) + ->range($offset, $limit); + if (!empty($page->options['fields'])) { + $query->fields($page->options['fields']); + } + return $query->execute(); +} + +/** + * Function for preprocessing the variables for the search_api_page_results + * theme. + * + * @param array $variables + * An associative array containing: + * - index: The index this search was executed on. + * - results: An array of search results, as returned by + * SearchApiQueryInterface::execute(). + * - keys: The keywords of the executed search. + */ +function template_preprocess_search_api_page_results(array &$variables) { + if (!empty($variables['results']['results'])) { + $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results'])); + } +} + +/** + * Theme function for displaying search results. + * + * @param array $variables + * An associative array containing: + * - index: The index this search was executed on. + * - results: An array of search results, as returned by + * SearchApiQueryInterface::execute(). + * - items: The loaded items for all results, in an array keyed by ID. + * - view_mode: The view mode to use for displaying the individual results, + * or the special mode "search_api_page_result" to use the theme function + * of the same name. + * - keys: The keywords of the executed search. + */ +function theme_search_api_page_results(array $variables) { + drupal_add_css(drupal_get_path('module', 'search_api_page') . '/search_api_page.css'); + + $index = $variables['index']; + $results = $variables['results']; + $items = $variables['items']; + $keys = $variables['keys']; + + $output = '

' . format_plural($results['result count'], + 'The search found 1 result in @sec seconds.', + 'The search found @count results in @sec seconds.', + array('@sec' => round($results['performance']['complete'], 3))) . '

'; + + if (!$results['result count']) { + $output .= "\n

" . t('Your search yielded no results') . "

\n"; + return $output; + } + + $output .= "\n

" . t('Search results') . "

\n"; + + if ($variables['view_mode'] == 'search_api_page_result') { + $output .= '
    '; + foreach ($results['results'] as $item) { + $output .= '
  1. ' . theme('search_api_page_result', array('index' => $index, 'result' => $item, 'item' => isset($items[$item['id']]) ? $items[$item['id']] : NULL, 'keys' => $keys)) . '
  2. '; + } + $output .= '
'; + } + else { + // This option can only be set when the items are entities. + $output .= '
'; + $render = entity_view($index->item_type, $items, $variables['view_mode']); + $output .= render($render); + $output .= '
'; + } + + return $output; +} + +/** + * Theme function for displaying search results. + * + * @param array $variables + * An associative array containing: + * - index: The index this search was executed on. + * - result: One item of the search results, an array containing the keys + * 'id' and 'score'. + * - item: The loaded item corresponding to the result. + * - keys: The keywords of the executed search. + */ +function theme_search_api_page_result(array $variables) { + $index = $variables['index']; + $id = $variables['result']['id']; + $item = $variables['item']; + + $wrapper = $index->entityWrapper($item, FALSE); + + $url = $index->datasource()->getItemUrl($item); + $name = $index->datasource()->getItemLabel($item); + + if (!empty($variables['result']['excerpt'])) { + $text = $variables['result']['excerpt']; + } + else { + $fields = $index->options['fields']; + $fields = array_intersect_key($fields, drupal_map_assoc($index->getFulltextFields())); + $fields = search_api_extract_fields($wrapper, $fields); + $text = ''; + $length = 0; + foreach ($fields as $field_name => $field) { + if (search_api_is_list_type($field['type']) || !isset($field['value'])) { + continue; + } + $val_length = drupal_strlen($field['value']); + if ($val_length > $length) { + $text = $field['value']; + $length = $val_length; + + $format = NULL; + if (($pos = strrpos($field_name, ':')) && substr($field_name, $pos + 1) == 'value') { + $tmp = $wrapper; + try { + foreach (explode(':', substr($field_name, 0, $pos)) as $part) { + if (!isset($tmp->$part)) { + $tmp = NULL; + } + $tmp = $tmp->$part; + } + } + catch (EntityMetadataWrapperException $e) { + $tmp = NULL; + } + if ($tmp && $tmp->type() == 'text_formatted' && isset($tmp->format)) { + $format = $tmp->format->value(); + } + } + } + } + if ($text && function_exists('text_summary')) { + $text = text_summary($text, $format); + } + } + + $output = '

' . ($url ? l($name, $url['path'], $url['options']) : check_plain($name)) . "

\n"; + if ($text) { + $output .= $text; + } + + return $output; +}