I need to use the OR operator on an EntityFieldQuery which I understand can only be accomplished via addTag and hook_query_TAG_alter.
My query at present:
$query = new EntityFieldQuery();
$query->propertyOrderBy('created', 'DESC');
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'news')
->propertyCondition('status', 1)
->fieldCondition('field_pub_year', 'value', $year, '=');
if(isset($_GET['country_id'])){
$country = $_GET['country_id'];
$query->fieldCondition('field_related_country', 'target_id', $country, '=');
}
if(isset($keywords)){
$query->addTag('search');
}
$result = $query->execute();
Without much more than a rudimentary knowledge of Drupal, I would expect that the following function, inserted into my template, might work:
function hook_query_search_alter(QueryAlterableInterface $query){
$or = db_or()
->fieldCondition('title', 'value','%'. $keywords .'%', 'LIKE')
->fieldCondition('body', 'value','%'. $keywords .'%', 'LIKE');
$query->condition($or);
}
Yet, it seems to do nothing...
I'm having equal luck googling this problem. Appreciate any suggestions. Thanks.
Update:
I moved the alter function into a module and re-wrote it to look like:
function modulename_query_search_alter(QueryAlterableInterface $query){
$terms = $query->getMetaData('terms');
$words = "%".$terms."%";
$or = db_or()->condition('body', $words, 'LIKE')->condition('title', $words, 'LIKE');
$query->condition($or);
}
but I now get an unknown column body
error. Removing that condition works correctly and results are filtered by title field. Not sure what the deal is with that...