Drupal is a robust system with some powerful features and modules. One of those is the Views modules. This modules allows a developer to create sophisticated queries that can pull pretty much anything from the database. Although powerful, it has some limitations and one of them is returning queries based on partial terms. Below I will detail issues I faced and how I solved the problem with hook_views_query_alter function.
The project was to create a Search System using Views, which was pretty straightfoward to do. The view's path was setup as /search/% which would allow terms to be passed on in the url such as /search/special-product.
That worked pretty well, but I quickly ran into an issue where a partial term would not return any results. For example /search/special would not return anything, when /search/special-product would. After some research and tryouts I finally found a solution that works and can return results for partial terms. That solution came thru the hook views_query_alter() function. It took some debugging to come up with this solution, but in the end this is what works for me
function cms_overrides_views_query_alter(&$view, &$query){
if($view->name == 'search_results_display'){
foreach($query->where as &$conditionGroup){
foreach($conditionGroup as &$condition){
if(is_object($condition[0]['field'])){
$conditionP = $condition[0]['field']->conditions();
foreach($conditionP as $idx => &$cValue){
if(is_object($cValue['field'])){
$originalValue = $cValue['field']->conditions()[0]['value'];
$cValue['field']->conditions()[0]['value'] = '%'.$originalValue.'%';
$cValue['field']->conditions()[0]['operator'] = 'LIKE';
}
}
}
}
}
}
}