سؤال

لقد قمت بمراجعة المستندات والكود المصدري بحثًا عن شيء ما دون أن يحالفني الحظ.

هل هناك خطاف Drupal 6 يتم استدعاؤه بعد Hook_search()، ولكن قبل تسليم النتائج $ إلى نظام القالب؟

أحتاج إلى إجراء تقليم مخصص إلى حد ما وإعادة ترتيب النتائج التي يتم إرجاعها.يمكنني فقط إعادة تنفيذ Hook_search()، ولكن يبدو أن هذا مبالغة.

شكرًا.

هل كانت مفيدة؟

المحلول

لا يوجد؛ search_view() (الذي يعرض النتائج) يدعو search_data(), ، الذي يستدعي hook_search() ثم المواضيع النتائج على الفور.إعادة التنفيذ hook_search() ربما يكون هو الطريق الأكثر مباشرة.

ومع ذلك، يمكنك بدلاً من ذلك التنفيذ hook_menu_alter() واطلب من صفحة البحث استدعاء وظيفتك المخصصة بدلاً من الاتصال search_view() ( ثم يدعو بعد ذلك search_data()).شيء مثل:

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);
        }
      }
    }
  }
}

نصائح أخرى

يمكنك استخدام hook_search_page() لإعادة ترتيب أو تنسيق نتيجة البحث.

يتيح لك Hook Search_execute تعديل الاستعلام بالطريقة التي تحتاجها. يمكنك حتى إطلاق استفسارات جديدة باستخدام SQL مخصص ، على سبيل المثال:

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,
      );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top