I'm trying to replace the default search with a LIKE clause in Drupal 7. I tried to alter the query according to Adding an OR condition to an existing query:
function MYMODULE_query_node_access_alter(QueryAlterableInterface $query) {
foreach ($query->getTables() as $table) {
// LIKE for search results.
if ($table['table'] == 'search_index') {
// Get the query args and then the search term
$args =& $query->getArguments();
$search = $args[':db_condition_placeholder_1'];
// Get a reference to the existing query conditions.
$conditions =& $query->conditions();
// Save the former conditions
$former_conditions = $conditions;
// Reset the condition array. It needs a default #conjunction for which AND is fine
$conditions = array('#conjunction' => array_shift($former_conditions));
// Replace the search condition in the query
foreach ($former_conditions as $key => $condition) {
if ($key != 1) {
$query->condition($condition['field'], $condition['value'], $condition['operator']);
}
else {
$query->condition('i.word', '%' . db_like($search) . '%', 'LIKE');
}
}
}
}
}
Searching with the word "declaration" displays the same results as the default drupal search, but searching with "decl" doesn't find any results.
Any ideas why my code isn't working?
$search
variable? $search = $args[':db_condition_placeholder_1']; If it is from views go forhook_views_query_alter()
it would be simple.