Using Hook Views Query Alter

1 Comment

Building a website I recently ran into an issue where I had HTML search facets interacting with Drupal views. The user would click on the facet and it would filter the view depending on the argument. I ended up implementing hook_views_query_alter. To make sure the values I wanted to filter on were in the view, I added the search facets into the view as fields. This automatically joins the necessary tables so that you don’t have to.

Implementing hook_views_query_alter requires two parameters, view and query, both are passed by reference. Since this hook is called for every view the first thing I did was check that I was altering the right query by adding an if statement that checks the view name. After that you're free to modify the query as you wish. In my example, I looped through the search facets and added them as where statements and arguments to the query.


function search_views_query_alter(&$view, &$query) {
  if($view->name == 'example_view_name'){
    foreach($parsed_url AS $key => $value){
      $query->where[0]['clauses'][] = 'profile_values_profile_' . $key . '.value IN ("%s")';

Nodequeue and Views Combination.

2 Comments

Sometimes it is really hard to show only the needed nodes in the needed order using views only.
Here are some examples when you need to give a Content Manager (CM) some control over the nodes that will be displayed by the view:
- you need to promote on the front page some content Item (for ex., some new Game) and want the CM to be able to change it through the UI any time he pleased

- you need to show the newest Games in the block (sorted by some date), but you need to give the CM ability to override this list and show a couple of Games of their choice at the top of the list (following by rest of the newest Games sorted by a date)

- you need to show a list of Items of some Type, but to make sure that some of the Items (from some "black list") will never be included into this list.

The Nodequeue and Views modules will be a very useful combination here.

Theming Views on Drupal 6 (the simple way)

5 Comments

There are several ways you can theme a view on Drupal, and this tutorial will describe a simple and quick way to accomplish just that. I assume that you’re familiar to working with Views and CCK, so I won’t go in much detail on the process of creating the view and content type.

For this example I’m creating a block simple view that will display a teaser for the 3 latest blog posts.

First thing we’ll have to do is create the actual template file on the themes folder (I’d recommend that you create a folder for all the views, but this is optional). We’ll start by going to the views interface to find out how that file should be named.

On the Views interface select “Theme: information” under “Basic Settings”.

You’ll see a list of the possible files you can use listed from the most general(full page)on top to the most specific on the bottom. We’re going to be selecting one option on the “Row Output”. If you don’t need to be too specific about theming that view, you can use the second option. Copy the file name and create that file on your themes folder.

Back to the view interface again. Select “rescan template files” and the file you created should be highlighted on the interface.

Here is where the real theming begins. We’re going to create the variables we need on the template file. In this case, I’ll be creating $title and $body variables.

Go to your views interface under “fields.” However, the links to the fields you want to use are found at the status bar, on the bottom of the browser. We’re looking for the information on the last section of the link. For the Title field, my link looks like this: http://domainname.com/admin/build/views/nojs/config-item/footer_blog/block_1/field/title so the information we’re looking for is “title”.

Knowing that you can set up or title variable like this: $title = $fields['title']->content; And repeating the process for the body will get you something like this: $body = $fields['body']->content;

Now that you have your variables set, can go ahead and theme your file at will! You’ll find yourself dealing with a very simple and clean template file!

Do you have a different technique to theme your views? Share with us!

Proximity Search using Views in Drupal

8 Comments

Problem:

I am working on a project that requires the user to input a maximum of two zip codes during. When the user is redirected to their dashboard, based on the zip codes provided, the site should list advertisers within the 30 mile radius. Note that if the user decides to change their zip codes later on, somewhere on their profile, the dashboard will show advertisers within the radius of the given zip codes.

Solution:

Implement the advertiser as a node with node location attached to it and create a “Location" type view.

Modules used:
Location – 6.x-3.0
Views - 6.x-2.7

Implementation:

Here are the steps to create the “Location" view, which will do a proximity search, given the user's zip code on their profile:

1) Navigate to "admin/build/views/add".
2) Enter the view name and description.
3) Select “Location" under “View type".
4) Under “Fields", select “Location" and add “Location: Distance / Proximity"
NOTE: The distance from the selected location and either the current user or a specific location.

Things that can go wrong with your views export

2 Comments

I exported a view to another server and lost it, but it was right there, in my code, anyone could see it there - yet not in a list of views. What happened? – Well, after all, it appeared to have an easy explanation:

When I exported my view and put it into hook_views_default_views function, I forgot that I also needed to export some content type to which I had added a new field. When Drupal tried to import my view it was looking for the field that did not exist in a database. The result – no imported view whatsoever. No error messages, nothing! I need to mention that this happened in Drupal 5 – maybe a Drupal 6 would have given me a little warning? – I never tried to repeat my mistake there.

What else can go wrong with your view export?

Views preprocessors in module files

0 Comments

We've all used pre-processors to modify data before it reaches the template file to be printed. These pre-processors are usually defined in template.php with a corresponding template file within the themes folder. The views module is quite helpful because it allows others to use their own themeing files to design their own views.

I was working on a custom node module and wanted to implement views hooks, so that I can expose my tables data to views, and to create default views. Exposing the extra data from my table was relatively simple as I simply implemented the hook: hook_views_data. With this hook, I was able to setup my fields, filters, arguments, and sorting.

So now, with my exposed data ready to be used, I was ready to implement hook_views_default_views. I initially created the views through the user interface and then pasted the exported code in my hook_views_default_views implementation.

Over-riding Views 2 Output in Drupal

7 Comments

I have been using Views 2 module a lot lately and the more I use it, the more I appreciate its elegantly designed architecture. It is very powerful and flexible. Popular modules such as Organic Groups and Location make the Views 2 module even more powerful. Showing information to end-users becomes easier when you want to show certain group posts or perhaps display the list of members of a group given a certain location.

I believe that the biggest factor which contributed to the popularity of Views 2 is its flexibility. You can pretty much override everything, from overriding its query using hook_views_query_alter(...), to being able to override its output at a very granular level(field result level).

Let's say that you want to show a yearly sales report of your 5 departments highlighting the sales per month. You can assume that a “department” is implemented as a node. Your have created a “Page” display of your View. The style of your View is set to “Table”. You have selected “Node:Title” as your department's name.

Here I will show you step by step process on how to override the result of a Views query.

1. Click “Theme:Information” under you Views “Basic settings”.

Modifying Views

4 Comments

We use the Views UI a lot to create custom pages, blocks, etc. Sometimes it does not give us exactly what we want and we would like to modify the generated sql query. It's possible to achieve this through the 'views_query_alter' hook. For example, we can do something like:

Drupal 5 Node Selection: Internationalization Module

1 Comment

My current project involves adding multilingual capability to a Drupal 5 website using the Internationalization (i18n) module. After enabling i18n, I ran into a small issue with contents from Views module.

After selecting a language for a node, that node disappeared from a node list built by Views on other language pages. After adding translations to the node, the translation node was displayed on the page again.

I wanted the option to choose a language, and if it didn't exist, the default language and also no language.

Internationalization module allows you to choose ‘content selection mode’ on Multilingual settings page.

There are 5 options for Drupal 5:
1) Current language / no language
2) Current language / default language / no language
3) Default language / no language
4) Current language
5) All content. No language conditions apply

However, none of the options perfectly matches my need.

I came up with this solution: