Proximity Search using Views in Drupal

3 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

5 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

3 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: