Domanda

Mi sono state andando attraverso la documentazione e il codice sorgente alla ricerca di qualcosa, senza fortuna.

C'è un gancio Drupal 6 che viene chiamato dopo hook_search (), ma prima che i risultati $ ottiene consegnati fuori al sistema di template?

Ho bisogno di fare un abbastanza personalizzato potatura e riordino dei risultati che vengono restituiti. Potrei hook_search solo reimplementare (), ma questo mi sembra eccessivo.

Grazie.

È stato utile?

Soluzione

Non non lo è; search_view() (che visualizza i risultati) chiamate search_data() , che invoca hook_search() poi immediatamente temi i risultati. Re-attuando hook_search() è probabilmente la via più semplice.

Detto questo, si potrebbe invece implementare hook_menu_alter() e hanno la ricerca pagina chiamare la funzione personalizzata invece di chiamare search_view() (e successivamente chiamando search_data()). Qualcosa di simile:

function test_menu_alter(&$items) {
  $items['search']['page callback'] = 'test_search_view';

  foreach (module_implements('search') as $name) {
    $items['search/' . $name . '/%menu_tail']['page callback'] = 'test_search_view';
  }
}

// Note: identical to search_view except for --- CHANGED ---
function test_search_view($type = 'node') {
  // Search form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle:
  // search/type/keyword+keyword
  if (!isset($_POST['form_id'])) {
    if ($type == '') {
      // Note: search/node can not be a default tab because it would take on the
      // path of its parent (search). It would prevent remembering keywords when
      // switching tabs. This is why we drupal_goto to it from the parent instead.
      drupal_goto('search/node');
    }

    $keys = search_get_keys();
    // Only perform search if there is non-whitespace search term:
    $results = '';
    if (trim($keys)) {
      // Log the search keys:
      watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));

      // Collect the search results:
      // --- CHANGED --- 
      // $results = search_data($keys, $type);
      // Instead of using search_data, use our own function
      $results = test_search_data($keys, $type);
      // --- END CHANGED ---

      if ($results) {
        $results = theme('box', t('Search results'), $results);
      }
      else {
        $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
      }
    }

    // Construct the search form.
    $output = drupal_get_form('search_form', NULL, $keys, $type);
    $output .= $results;

    return $output;
  }

  return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
}

// Note: identical to search_data() except for --- CHANGED ---
function test_search_data($keys = NULL, $type = 'node') {

  if (isset($keys)) {
    if (module_hook($type, 'search')) {
      $results = module_invoke($type, 'search', 'search', $keys);
      if (isset($results) && is_array($results) && count($results)) {
        // --- CHANGED ---
        // This dsm() is called immediately after hook_search() but before
        // the results get themed. Put your code here.
        dsm($results);
        // --- END CHANGED ---

        if (module_hook($type, 'search_page')) {
          return module_invoke($type, 'search_page', $results);
        }
        else {
          return theme('search_results', $results, $type);
        }
      }
    }
  }
}

Altri suggerimenti

È possibile utilizzare hook_search_page() per riordinare o formattare il risultato della ricerca.

Hook search_execute consente di modificare la query in modo avevi bisogno. Si può anche sparare nuove query con SQL personalizzato, ad esempio:

function mymodule_search_execute($keys = NULL, $conditions = NULL) {

      // Do some query here.
      $result = my_fancy_query();

      // Results in a Drupal themed way for search.
      $results[] = array(
        'link' => (string) $result->U,
        'title' => $title,
        'snippet' => $snippet,
        'keys' => check_plain($keys),
        'extra' => array($extra),
        'date' => NULL,
      );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top