Views Search by Link Module's URL Title: More Views Hacking
So recently I wanted to filter by URL title, having used it extensively as a text field with an optional link in a site.
The code below is supposed to apply to an exposed Link module field filter in Views. Instead of searching to see if the search string matches the URL, this will match the URL title that was provided.
In the code below, you would replace node_data_{field_publication_author.field_publication_author}
with the name of your field, in all places that field_publication_author
occurs in the below.
Basically I should be patching the Link module and contributing back instead of using this query alter function. If I run across this in Drupal 7 I will do so.
But, it works, and due to the array_search function it is reasonably robust, meaning someone could change the view and it would still work.
<?php
function modulename_views_query_alter(&$view, &$query) {
//this allows us to search by Link title rather than by link URL
if($view->name == 'viewname'){
$old_value = "UPPER(node_data_field_publication_author.field_publication_author_url) LIKE UPPER('%%%s%%')";
$new_value = "UPPER(node_data_field_publication_author.field_publication_author_title) LIKE UPPER('%%%s%%')";
$where_clause_key = array_search($old_value, $query->where[0]['clauses']);
if (is_numeric($where_clause_key)) {
$query->where[0]['clauses'][$where_clause_key] = "UPPER(node_data_field_publication_author.field_publication_author_title) LIKE UPPER('%%%s%%')";
}
}
}
?>Tags: query alter