[support] Add Location to a node programmatically

Scott scott at bscottholmes.com
Sun Jan 24 18:02:24 UTC 2010


I have a module meant to create a node at the time a user record is set
to active status.  The user record contains location data.  I wish to
use this same location data in the new node but have so far not
discovered how to do so programmatically.  My module code:

<?php
/**
* DRUPAL 6
* Override the hook_user
*
*/ 

function newbusinessnode_user($op, &$edit, &$account, $category = NULL) {
    if ($op == 'after_update') {
      $bus_uid = $account->uid;
      $bus_name = $account->name;

      // the location is the point of this new node
      $sql = 'SELECT location.lid AS lid, location.name AS location_name FROM {location} location  LEFT JOIN {location_instance} location_instance ON location.lid = location_instance.lid LEFT JOIN {users} users ON location_instance.uid = users.uid WHERE users.uid = '.$bus_uid;
      $result = db_query($sql);
      if ($loc = db_fetch_object($result)) {
        $loc_lid = $loc->lid;
        $loc_name = $loc->location_name;
      }

// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'business_submitted');
$form_state['values']['title'] = $loc_name;
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = $bus_name;
$form_state['values']['location']['lid'] = $loc_lid;
$form_state['values']['op'] = t('Save');
drupal_execute('business_submitted_node_form', $form_state, (object)$node);
    }
}
?>

How do I include the lid with the node?  This module always returns an
"Location field is required" error message.



More information about the support mailing list