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

发布于 2024-09-28 01:37:34 字数 971 浏览 1 评论 0原文

我尝试在 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);

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

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

提前致谢!

I try to programmatically create a node of custom contenttype "location" in Drupal 6, with the node containing a location field (http://drupal.org/project/location) called "location" (yes I know, the nomenclature could be better, but I am just experimenting on this at the moment).

Creating the node works just fine, but I cannot find a way to set the contents for the location field - i.e. the node is created with all content but the value for then location field.

I try creating the node like this:

        $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);

The node gets created with the correct title, but the location fields remain unset.

How can I programmatically set the location-related fields?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听闻余生 2024-10-05 01:37:34

想将其添加为注释,但似乎将代码放入注释中是相当有问题的。所以我们开始吧:我更改了 internas,以便不再使用 cck 字段,而是使用 googletorp 建议的默认位置选项。

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

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

$newLocationId = location_save($location);

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

node_save($newNode);

感谢您的指导:)

Wanted to add this as a comment, but it seems like putting code into the comment is rather problematic. So here we go: I changed the internas so that I do not use a cck field anymore, but use the default location option as suggested by googletorp.

The actual code to create a new location and assign this to a new node looks like this:

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

$newLocationId = location_save($location);

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

node_save($newNode);

Thanks for the guidance :)

情仇皆在手 2024-10-05 01:37:34

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

请参阅http://thedrupalblog.com/programmatically-create-any- node-type-using-drupal-execute 是使用 drupal_execute 的绝佳示例。不要忘记查看评论 http: //thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute#comment-70 查看有关 CCK 字段的一些附加信息。

drupal_execute 的优点是您还可以获得表单验证。因此,在 drupal_execute 语句之后,您可以使用 form_get_errors 查看是否有任何错误(http://api.drupal.org/api/function/form_get_errors/6 )。有关使用示例,请参阅 http://civicactions.com/blog/cck_import_and_update 中的片段(粘贴在下面) form_get_errors

$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

Instead of node_save, many people recommend using drupal_execute to programmatically submit the node edit form. This gives you the benefit of form validation.

See http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute for an excellent example of using drupal_execute. Don't forget to look at the comment http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute#comment-70 to see some additional info on CCK fields.

The advantage of drupal_execute is that you get form validation also. So after the drupal_executestatement you can see if there were any errors using form_get_errors ( http://api.drupal.org/api/function/form_get_errors/6 ). See snippet (pasted below) from http://civicactions.com/blog/cck_import_and_update for an example of using form_get_errors

$node->type = 'yourtype';
$values = array();
$values[...] = ...;
drupal_execute('yourtype_node_form', $values, $node);
$errors = form_get_errors();
if (count($errors)) {
  // do something ...
}

Another very nice resource on programmatic submission of nodes using drupal_execute can be found at http://drupal.org/node/293663

复古式 2024-10-05 01:37:34

我已经这样做了,只是没有使用 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);
}

I have done this, only not with a CCK field but the default location option you can add to nodes.

What I did to make it work, was to first save the location (there's an API function for it) and then add the location id from the saved location.

Sample code:

Note, $center is from an external source, so it's not Drupal related. I know all my locations are from Denmark in my example, so that part is just hardcoded.

When you don't use a CCK field, you don't need to save the location data on the node, instead you can just save the location and pair the location yourself. It's a quick solution, instead of running through the node form like suggested. For complex nodes, that might be the better choice, but when it's simple, this is done faster.

// 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);
}
另类 2024-10-05 01:37:34

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

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

灵感来自:此处

For Drupal 7, saving as default location tab.

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

Inspired from: here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文