







Those days a friend of mine came up with a question: how do I display a list of similar content bellow a node. He had researched and tested a bunch of modules, for example the relevant_content project, but the result it's giving is a list of nodes with common taxonomy terms. What he wanted to show is a list of nodes having equal values in their CCK fields. He also told me that he played with views_attach - no luck with that either.
My immediate suggestion was that creating a simple custom module will solve the problem easily and that's what I would have done if I had such a task to complete. Later I was told by greggles that there's a comparison of Content recommendation modules. I was not able to find a suitable solution among them. On the other hand, he also told me about the Recommender API which seems to be too complicated for our task and besides, at the time of this writing the available release is 6.x-2.0-beta4 since 2010-Jan-06.
Last night I've decided to play a bit with views and see if there's a solution for the problem based on that module. And the answer is yes, there is, up to a point. So here I'll describe how I did it - I've been developing Drupal sites full time since more than 3 years but I have to confess that I don't have much experience with views. I've always preferred to write a custom module solving a specific issue - that way I'm sure that the optimal SQL queries will be used (or at least they'll be as good as I could write them) and I can provide theme functions and template files for the designers/themers to modify the output. But let's get back to the topic of this post, views are good solution for some things and for others they're not, as with everything in life.
I've started with a fresh Drupal installation and added the CCK, views and views_attach modules. Then I've created a new content type and called it "Car offer". I've added three new fields to that type - two text fields for Make - field_make and Model - field_model and one decimal field for Price - field_price. Then I've created some nodes of this type to play with. Let's take three of them for example - they all had "Citroen" for Make and two of them had "C3" for Model and 50000 for Price. The end result I wanted to achieve was a list bellow each car containing another cars from the same Make and having a similar Price.
I've created a view and added a "Node content" display, which is provided by views_attach. I won't describe everything from the view configuration, only the important parts.
Screenshot 1: An overview of Node content views display
Let's start with configuring the Arguments property of "Node content settings" section - that's the most important thing. There I've chosen the "Use tokens from the node the view is attached to" radio button and then entered the following in the textfield that appeared under it: [nid]/[field_make-raw]/[field_price-raw]:
Screenshot 2: Node content arguments settings
Now save that and let's continue with configuring the views arguments. The order of arguments here must match the order you've entered them in the tokens string above. First I've added a Nid argument and configured it to be Excluded because I don't want the same node to appear in the list under itself.
Screenshot 3: Nid argument settings (view arguments)
Then I've added two more arguments - for Make and Price, and configured them to "Provide default argument" -> Node ID from URL. The last is really not self-explanatory but that's how views_attach works. See the last screenshot.
Screenshot 4: Field Make argument settings (view arguments)
And that's it - we're almost done. What remains to be configured is how the list of similar nodes will look like - under the "Basic settings" view section.
Now you've probably noticed that I've said "having a similar Price", not "having equal Price" - that's something I was not able to achieve with this "views based" solution and I could easily have done it in a custom module. So a question goes to all views experts out there (or for me when I have some time to browse through the views module source code): Is there a way to modify the view SQL query to be something like "... field_price > 48000 AND field_price < 52000 ..." - that's something I would very much like to know.
*Update: I've just found out the answer of my question, it's hook_views_pre_execute(&$view). By implementing this hook one can alter the SQL query to match the specific needs described above. Maybe this deserves a blog post of it's own.
Comments
Post new comment