使用cck位置字段以编程方式创建节点

时间:2010-10-15 20:30:37

标签: drupal location

我尝试以编程方式在Drupal 6中创建自定义内容类型“location”的节点,该节点包含一个名为“location”的位置字段(http://drupal.org/project/location)(是的,我知道,命名可能会更好,但我现在只是试验这个。)

创建节点工作正常,但我找不到设置位置字段内容的方法 - 即创建的节点包含所有内容,但是后面的位置字段的值。

我尝试创建这样的节点:

        $newNode = (object) NULL;
        $newNode->type = 'location';
        $newNode->title = $locationName;
        $newNode->uid = $userId;
        $newNode->created = strtotime("now");
        $newNode->changed = strtotime("now");
        $newNode->status = 1;
        $newNode->comment = 0;
        $newNode->promote = 0;
        $newNode->moderate = 0;
        $newNode->sticky = 0;

        $newNode->field_location[0]['street'] = 'Teststraße';
        $newNode->field_location[0]['postal_code'] = '12345'; 
        $newNode->field_location[0]['city'] = 'Musterstadt'; 

        node_save($newNode);

使用正确的标题创建节点,但位置字段仍未设置。

如何以编程方式设置与位置相关的字段?

提前致谢!

4 个答案:

答案 0 :(得分:4)

希望将此添加为评论,但似乎将代码放入评论中是相当有问题的。所以我们去了:我更改了内部,以便我不再使用cck字段,但使用googletorp建议的默认位置选项。

创建新位置并将其分配给新节点的实际代码如下所示:

$location['street'] = "myStreet";
$location['postal_code'] = "12345";
...

$newLocationId = location_save($location);

$newNode = ...
$newNode->locations[0]['lid'] = $newLocationId;

node_save($newNode);

感谢您的指导:)

答案 1 :(得分:3)

许多人建议使用node_save以编程方式提交节点编辑表单,而不是drupal_execute。这为您提供了表单验证的好处。

有关使用drupal_execute的一个很好的示例,请参阅http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute。不要忘记查看评论http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute#comment-70以查看有关CCK字段的其他信息。

drupal_execute的优点是你也可以获得表单验证。因此,在drupal_execute语句后,您可以看到使用form_get_errorshttp://api.drupal.org/api/function/form_get_errors/6)是否存在任何错误。有关使用form_get_errors

的示例,请参阅http://civicactions.com/blog/cck_import_and_update中的代码段(粘贴在下面)
$node->type = 'yourtype';
$values = array();
$values[...] = ...;
drupal_execute('yourtype_node_form', $values, $node);
$errors = form_get_errors();
if (count($errors)) {
  // do something ...
}

使用drupal_execute进行程序化提交节点的另一个非常好的资源可以在http://drupal.org/node/293663

找到

答案 2 :(得分:2)

我已经完成了这项工作,但不是使用CCK字段,而是可以添加到节点的默认位置选项。

我所做的是首先保存位置(它有一个API函数),然后从保存的位置添加位置ID。

示例代码:

注意,$center来自外部来源,所以它与Drupal无关。我知道我的所有地点都来自丹麦,所以这部分只是硬编码。

如果不使用CCK字段,则无需在节点上保存位置数据,而只需保存位置并自行配对位置。这是一个快速的解决方案,而不是像建议的那样运行节点表单。对于复杂节点,这可能是更好的选择,但是当它很简单时,这样做会更快。

// Update the location data.
$location = is_array($node->location) ? $node->location : array();
$location += array(
  'street' => $center->address->address2,
  'city' => $center->address->zipName,
  'postal_code' => $center->address->zip,
  'country' => 'dk',
  'country_name' => 'Denmark',
);
location_save($location);

// Insert location instance, if it's not set yet.
$criteria = array(
  ':nid' => $node->nid,
  ':vid' => $node->vid,
  ':lid' => $location['lid'],
);
if (!db_result(db_query("SELECT COUNT(*) FROM {location_instance} WHERE nid = %d AND vid = %d AND lid = %d;", $criteria))) {
  db_query("INSERT INTO {location_instance} (nid, vid, lid) VALUES (%d, %d, %d)", $criteria);
}

答案 3 :(得分:0)

对于Drupal 7,保存为默认位置选项卡。

$location = array(
  'latitude' => $row->gmapycord,
  'longitude' => $row->gmapxcord,
);
$lid = location_save($location);
if ($lid) {
  $entity->locations['0']['lid'] = $lid;
}

灵感来自:here