以编程方式设置位置数据

时间:2011-10-06 19:43:28

标签: drupal drupal-6 drupal-gmap

我遇到了一个问题,我已经研究了好几天而没有运气,这里的答案通常都是现货。

我有自定义模块代码,可以从表单提供的数据中添加节点:

$edit = array();
$edit['uid'] = $user->id;
$edit['name'] = $user->name;
$edit['status'] = 1;
$edit['taxonomy'] = array($term_id);
$edit['title'] = $Title;
$edit['body'] = $body;

等...

然后保存:

node_invoke_nodeapi($edit, $nType);
node_validate($edit);
if ($errors = form_get_errors()) {
      print_r($errors);
}

$node = node_submit($edit);
node_save($node);

这一切都很完美。但我正在尝试根据提供的(已清理的)zip字段向每个节点添加位置数据。

我安装并运行了gmap和位置模块。当我使用drupal内容编辑器直接添加zip时,一切正常。甚至是gmap的观点。所以我知道版本和mod都是正确的。

我用过这个:

$location = array(
'country' => 'US',
'postal_code' => $zip,
);
$locationID = location_save($location);

和此:

$location['country'] = "US";
$location['postal_code'] = $zip;
$locationID = location_save($location);

有和没有国家元素。 然后在节点数据init部分(上面)中:

$edit->locations[0]['lid'] = $locationID;

if($locationID) $edit['field_location'][0]['lid'] = $locationID;

if($locationID) $edit['location'][0]['lid'] = $locationID;

但这一切都无效。提交实际上会通过确定,但不会保存位置数据。没有错误抛出。

对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我确实让这个工作了,(如果有人遇到同样的问题并且偶然发现),首先创建节点,然后将位置数据添加到节点:

$locations = array();
$locations[0]['postal_code'] = $zip;

$criteria = array();
$criteria['nid'] = $node->nid;
$criteria['vid'] = $node->vid;
$criteria['genid'] = 'NAME OF NODE TYPE HERE';

location_save_locations( $locations, $criteria );

我想location_save_locations是正确的做法,而不是location_save。

答案 1 :(得分:0)

按照您的方法,第4行的location_save_locations更广开,您可以使用以下内容更新位置:

  location_save($locations[$key], TRUE, $criteria);

一些注意事项:

  • location_save返回已保存位置的盖子,如果该位置被视为"则返回FALSE。"
  • 清理缓存或查询数据库(mysql> SELECT * FROM location ORDER BY lid DESC limit 6)以获取新实体位置的全新视图(即:可以缓存node_load数据)。

或者,如果您正在处理具有位置字段的实体,您可以尝试更清洁的方式:

  // A) give to the $node field an updated location
  $node->field_location['und'][0] = $location; 
  // B) Save node with updated location
  node_save ($node);