Does anyone know if it's possible to assign a node a location (zipcode or city/state) automatically by pulling in the location of the author?

I want to group posts of my members by location. I can put the location fields in the node form, but would be simplier and easier for users if it just grabbed the info from their profile.

Any help or guidance would be appreciated.

thanks.

Comments

b__f’s picture

If you have a module, you can use form_alter to alter a form with the specific form id you are looking for. Use a query to look up the location of the user. Then, you can access the $form structure and set the #value entries before the form is rendered. (This would require a bit of coding).

csc4’s picture

This gave me an idea - how hard would it be for the lat/lon to default to the users lat/lon if defined?

We have a multi-national site but typically people enter information about their locality so a site default is less useful than a personal one.

pshadow’s picture

Yes, that is what I am trying to do. A zip code or lat/long based on author (user's) location as defined in his profile, not his current physical location. I'll look into B__F suggestion. If you come up with anything csc please post. thanks.

jamesJonas’s picture

+1

take lat/long from author
or take from group
or take from site

These types of cascading locations would be very helpful. The downside is that last I checked, location ignores geocoding if the lat/lon is filled in. This means that if you push to lat lon to early, if the user inputs an address for the post, which may represent an even more relevant location, that addresse's geocoded lat lon may be ignored.

ju.ri’s picture

this would be great! I already suggested this a while ago : http://drupal.org/node/131878
I'm sure it would be useful for many sites.
a similar way to do it might be to add "author location lat./long." fields to the views part of the module. that way we could at least pull the data to create maps and lists of nodes with their authors' locations.

btw: there is already an author location block in gmap.module
http://drupal.org/cvs?commit=66486

catch’s picture

Title: Automate location in node via author location? » Pre-populate node location from author/organic group etc.
Version: 5.x-1.x-dev » 5.x-2.7
Category: support » feature

Similar request for image.module exif data: http://drupal.org/node/172646
And another duplicate with some 'nasty hack' suggestions: http://drupal.org/node/131878

sgdev’s picture

Version: 5.x-2.7 » 5.x-3.1-rc1

Using hook_form_alter as b__f suggests in post #1 would make sense in most situations, but the Locations fieldset is not exposed as part of $form. I have a module where I run mymodule_form_alter, and in this function I've done a print_r($form);. Nothing in regards to the Locations fieldset is shown.

Unfortunately, the one option that will work is a variation on the 'nasty hack' suggestion in post #6. It does not work in Location 3.x as shown here. Instead, the following needs to be done --

In the location_form function in location.module, add the following two lines after the first conditional:

<?php
function location_form($settings, $locations) {
  if (!isset($settings['multiple']['max']) || $settings['multiple']['max'] == 0) {
    // Location not enabled for this object type.
    // Bail out early.
    return array();
  }

  global $user;  // ADD THIS LINE
  $user_location = location_load_locations($user->uid, 'uid');  // ADD THIS LINE
?>

Further in the same function, when in the for loop, make the following changes:

<?php
  for ($i = 0; $i < $numforms; $i++) {
    $required = FALSE;
    // Check if this is a required location.
    if ($i < $settings['multiple']['min']) {
      $required = TRUE;
    }
    $form[$i] = array(
      '#type' => 'location_element',
      '#title' => t('Location #%number', array('%number' => $i + 1)),
      //'#default_value' => isset($locations[$i]) ? $locations[$i] : NULL,   // COMMENT OUT THIS LINE
      '#default_value' => isset($locations[$i]) ? $locations[$i] : $user_location[$i],   // REPLACE WITH THIS LINE
      '#location_settings' => $settings,
      '#required' => $required,
    );
  }
?>

If anyone has a better suggestion, or has some way in which form_alter will work, I'd definitely like to hear about it.

yesct’s picture

Issue tags: +location auto-complete

tagging with something slightly related.

sgdev’s picture

I'd like to mention that I have further customized what I mentioned in Comment #7. We are using Organic Groups (og), and when a new calendar event is added to the group, we decided to auto-populate the location with the Group location rather than the User location. This makes sense for us because in most situations, an event will happen at or near the Group location.

This involves a modification to the code in Comment #7 to load the location of the group rather than the user if the current node is an event.

yesct’s picture

Issue tags: +location useful code

tagging.

farald’s picture

ron_s, regarding #9, could you post the code for your modification?
I need to do exacly the same thing, so id be very interested to have a look at it.

Thanks:)

EDIT: Nevermind, got it working:)

yesct’s picture

hamaldus, Could you post your steps you took to get it working?

farald’s picture

It was a quick hack of ron_s' version.

I need to be able to create nodes with lat/lon inherited from previous node.
If no node to inherit from, the location needs to be set at the user's location (users only post nodes to their immediate area)

The easiest way is to check for a node id in arg(3) after node/add/nodetype/ to inherit from.

<?php
	if (!is_numeric(arg(3))){
		global $user;  
  		$user_location = location_load_locations($user->uid, 'uid'); 
  	} elseif (is_numeric(arg(3))) {
  		$user_location = location_load_locations(arg(3), 'nid');
  	};
?>

Then I used the last part of ron_s' hack in #7.

It seems to me that it would be relatively easy to implement this to location.module by using something like this following pseudo:

if (admin settings:inherit from node=allowed) : 
- load nodeID using arg(), 
- check if user has read access to node, 
- insert into $loc.
if (admin settings:inherit from user=true) : 
- load user's location, 
- insert into $loc, 
if (no location/default settings): 
- set $loc none/default;

It would also be best if it was possible to edit inherit settings at a per-nodetype basis.

sgdev’s picture

hamaldus, it's slightly different if creating a node which is related to an Organic Group. There needs to be the added aspect of recognizing in which group the user is currently located. This can be found in $_SESSION['og_last'].

So I modified the first half of the code as follows:

<?php
  if(arg(0) != 'user') {
     if(arg(1) == 'ognodeadd' && $_GET['type'] == 'calendarevent') {
        if(!empty($_SESSION['og_last'])) {
            $sql = 'SELECT vid FROM {location_instance} WHERE nid = '. $_SESSION['og_last'];
            $result = db_fetch_object(db_query($sql));
            $user_location = location_load_locations($result->vid, 'vid');
        } else {
            global $user;
            $user_location = location_load_locations($user->uid, 'uid');
        }
     } else {
        global $user;
        $user_location = location_load_locations($user->uid, 'uid');
     }
  }
?>

The first conditional is new. I added arg(0) != 'user' to skip the location pre-population if in the user profile. This addition is important, otherwise an admin who makes a change to a user profile with no location will have their location (the admin's) saved as the user's location.

The second conditional says if adding a group node, and the group node is a calendar event, then check to see if the og_last session variable is set. If it is, use it to get the location of the group. Otherwise, use the user's location.

spidersilk’s picture

I tried applying the changes ron_s posted in comment #7, and it initially didn't seem to be working - until I realized I'd set the location in the wrong place!

So, for anyone else using the Content Profile module with this, you need to make sure that the user's location is set in the core profile (the part under Edit > Account), not just in the content profile!

ankur’s picture

Status: Active » Closed (fixed)

Looks like this support request has been serviced.

rickharington’s picture

Hey guys, I'm sorry to post on a closed thread, but don't want to add to the existing issues. Is this still applicable in 7.x? Should I create a new thread?