background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

In this article, I'll explain one way to embed a GMAP view into your search results.

I downloaded and installed the following modules:

content (cck)
location_cck
gmap
gmap_location
location
location_search
views
views_ui

Configuring the GMap and Location modules:

  • Location main settings (admin/settings/location), ensure "Enable JIT geocoding" is enabled.
  • Location map links (admin/settings/location/maplinking), ensure "Google Maps" is enabled for Unites States.
  • Location Geocoding options (admin/settings/location/geocoding), enable "Google Maps" for United States.
  • GMap (admin/settings/gmap), enter your Google Maps API key.

I then imported US zip codes into my Drupal database:

$ cd sites/all/modules/contrib/location
$ mysql db_drupal < zipcodes.us.mysql

I created a content type with a single Location CCK field for address. I made this field required and set my desired collection settings. I added some sample nodes, and verified the location coordinates were being added dynamically.

Next, I added a new view with the following settings:

  • Name: search_gmap
  • Type: Node
  • Style: GMap; macro: [gmap behavior=+autozoom]; Data source: Location.module;
  • Fields: Node: Title
  • Filters: Node: Published
  • Arguments: Node: Nid; Action to take if argument is not present: display empty text; Enabled: Allow multiple terms per argument.

Lastly, I added some code to my module:

<?php
function MYMODULE_preprocess_search_results(&$vars) {
 
 
// get nodeids from search results
 
$node_ids = array();
  foreach (
$vars['results'] as $result) {
    if (!
is_object($result['node'])) {
      continue;
    }
   
$node_ids[] = $result['node']->nid;
  }

 
// ensure node ids exist
 
if (!count($node_ids)) {
    return;
  }

 
// generate views output
 
$view_output = views_embed_view('search_gmap', 'default', implode('+', $node_ids));
  if (
$view_output) {   
   
$vars['search_results'] = $view_output . $vars['search_results'];
  }

}
?>

After flushing my caches, and running cron to index my nodes, I ran a search for "nashua":

GMap search results

In this tutorial I'll show how you can programmatically and dynamically remove a column from your view. In my example, I chose to hide an email column from unauthenticated users, but you could apply this code to do pretty much anything.

To get things started, I defined a content type of "Profile" to contain some user details (using the Content Profile module) and used the Devel module's generate users and nodes functionality to create some sample data. I then created a view to show the data:

Here is my page view display:

Now you can add the hook_views_pre_build() function in your module. Since the $view object is large, I recommend using krumo() (which comes with the Devel module) to browse through the $view object's properties.

<?php
function MYMODULE_views_pre_build(&$view) {

 
// check for your view name (in my example: people)
 
if ($view->name=='people') {
   
krumo($view);
  }

}
?>

The above code will neatly format the $view object in a hierarchical display. You can click to expand each class property.

As you can see, the $view object has a lot of data in it. At this point, you'll need to get acquainted with the structure of the handler object of the view ($view->display['default']->handler) and determine what to modify. To remove the email column for unauthenticated users, you can add the following code:

<?php
function MYMODULE_views_pre_build(&$view) {

 
// check for your view name (in my example: people)
 
if ($view->name=='people') {
  
   
// check if the user is anon
   
if (in_array('anonymous user',$GLOBALS['user']->roles)) {

     
// remove view hander properties for email column
     
unset(
       
$view->display['default']->handler->options['fields']['mail'],
       
$view->display['default']->handler->options['style_options']['columns']['mail'],
       
$view->display['default']->handler->options['style_options']['info']['mail']
      );

    }

  }

}
?>

When I logout, my view no longer shows the email column:

I recently created a view to show all the nodes that exist in a user's organic groups. I originally created the same view in Drupal 5 by adding the filter "OG: Post in User Subbed Groups" is equal to "Currently Logged In User". Unfortunately, I could not find the equivalent filter in Drupal 6 Views, so I decided to use arguments. Argument handling code is structured a differently in Drupal 6:

1) add a new argument
2) for "Action to take if argument is not present" choose "Provide default argument"
3) for "Default argument type" choose "PHP Code"
4) for "PHP argument code" I entered:

return MYMODULE_views_group_nids();

It's a personal preference of mine to NOT insert code into my database, so I added the previously called function into a module. IMHO, code belongs in subversion so it can be versioned properly, not in a database. Here's my function definition:

<?php
function MYMODULE_views_group_nids() {
 
// create SQL statement to look up all group nodes of the current user
 
$sql = "select nid from og_uid where uid = '%d' and is_active='1'";
   
 
// query database
 
$resource = db_query($sql, db_escape_string($GLOBALS['user']->uid));

 
// fetch results from resource
 
$results = array();
  while (
$row = db_fetch_array($resource)) $results[] = $row['nid'];

 
// rollup results, comma separated
 
$results = implode(',', $results);

 
// return result
 
return $results;
}
?>

Syndicate content