0

我被困在一个我研究了几天但没有运气的问题上,这里的答案通常是正确的。

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

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

ETC...

然后保存:

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 和 location 模块。当我使用 drupal 内容编辑器直接添加 zip 时,一切正常。甚至视图gmap。所以我知道版本和模组都是正确的。

我用过这个:

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

和这个:

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

有和没有国家元素。然后在节点数据初始化部分(上图)中:

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

或者

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

或者

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

但这些都不起作用。提交实际上会通过,但没有保存位置数据。并且没有抛出错误。

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

4

2 回答 2

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。

于 2011-10-09T00:36:54.767 回答
0

Following your approach, as exposed by the wider location_save_locations at line 4, you can update a location with:

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

A few notes:

  • location_save return the lid of the saved location, or FALSE if the location is considered "empty."
  • Clean your cache or query the database ( mysql> SELECT * FROM location ORDER BY lid DESC limit 6 ) for a fresh view of the new entity location (ie: node_load data may be cached).

Alternatively, if you are handling an entity with a location field, you may try something cleaner like:

  // A) give to the $node field an updated location
  $node->field_location['und'][0] = $location; 
  // B) Save node with updated location
  node_save ($node);    
于 2017-04-05T14:09:20.987 回答