Drupal's Views module ranks as one of the most powerful tools in the CMS. However, complex queries that involve relationships between different content types often throw up duplicated results. A simple example of this, is an "Artist" has many "Paintings". If all of the paintings are shown on the Artists individual node, you may want to just show the names of the Artist instead of their Paintings. There are many ways to achieve this with Views, but if you are using Views Exposed Filters - we are unable to use Arguments to achieve this, therefore this is our recommended method.
To clarify what we have in our scenario, we have several "Artists" for example: Picasso and Van Gogh. Both Artists have several "Paintings" which have a CCK Node Reference field that both reference the Artist name.
We also are using a Views Exposed Filter and when using this, we can search for paintings with a specific value. The value is a field belonging to the "Painting" - therefore when we search, we are delivered duplicated results showing paintings from both of our Artists. Ideally, we should only be showing their names once as each painting is already shown on the specific node.
This solution will show you how to only show the Artist name once, and link you to their page.
To do this, you should amend the SQL Query that is run by the View. To do this we need a custom module, and a snippet of code.
The function we will use is called:
- function yourmodulename_views_pre_execute(&$view)
Our current view is called "search_artists" and the field that we need to use to make our listings unique values only, is "node_node_data_field_parent_artist".
First we need to replace "yourmodulename" with the name of your custom module, in this case I have created a custom module called "custom_overrides".
Here we are simply using a string replacement on the SQL for the View, and changing "GROUP BY nid" to "GROUP BY node_node_data_field_artist" which then gives us a distinct list of our Artists in the view.
Posted by: garry