So I wanted to be able to pass a default value to the the Views search filter, to filter by a view by a search term, to display that view in a block.
EDIT: There is an easier way to do this, which renders this whole tutorial useful only for other SQL hijacking. Under Filter, select Node: Body. See screenshot.
I had two options, create a view, create an RSS feed, run it through Yahoo Pipes, and cycle it back through, or modify the views query.
I decided to modify the views query, thanks to Nick Lewis' blog post.
Create a custom module. My module is called cm_custom. All you need for that is a .info file and a .module file.
Put this in your .module file. I commented out my debug methods. Uncomment them with the devel module installed to see your view.
Get your view name from the dpm'ed view, then insert it into the if statement. Check out your query. You can modify the query in whatever way you'd like. You'll have to experiment with the syntax yourself. Group by and Order by don't work. Here's how to do that.
In the case below, I'm modifying the query by restricting the view results to "mysearchterm"
<?php //restricts view listing of content by search term function cm_custom_views_query_alter(&$view, &$query) { // dpm($view); if ( $view->name == 'your_view_name' ) { // dpm($view); //dpm($query); $query->where[0]['clauses'][] = "node_revisions.body like ('%mysearchterm%')"; // dpm($query, 'after edit'); } }
?>
This is what a views query looks like when it's been dpm'ed
Attachment | Size |
---|---|
Edit view Blog | EchoDitto Labs.jpg | 36.11 KB |