Search with custom condition

2.1k views Asked by At

How can I add a condition to default search module?

I want to add a checkbox field called "Allow to search" to nodes, and unchecked items will not show in search results.

Extending Drupal 7 search seems to be my solution, but I can't make it work; hook_search_execute() is not executed.

Can you explain why this happens?

1

There are 1 answers

1
apaderno On BEST ANSWER

You need to first select your module on admin/config/search/settings, and possibly unselect the Node module in the "Active search modules." If your module is not selected there, your hook will not invoked.

screenshot

As for the reason why one hook is invoked, and one is not invoke, the code executed by search_get_info() (the function called from search_menu() to build the search menu) first invokes every implementation of hook_search_info(), and then it checks for which modules the search integration has been enabled. Since your module doesn't have the search integration enabled, hook_search_execute() for your module will never be invoked.

  if (!isset($search_hooks)) {
    foreach (module_implements('search_info') as $module) {
      $search_hooks[$module] = call_user_func($module . '_search_info');
      // Use module name as the default value.
      $search_hooks[$module] += array(
        'title' => $module,
        'path' => $module,
      );
      // Include the module name itself in the array.
      $search_hooks[$module]['module'] = $module;
    }
  }

  if ($all) {
    return $search_hooks;
  }

  $active = variable_get('search_active_modules', array('node', 'user'));
  return array_intersect_key($search_hooks, array_flip($active));