使用 cck 位置字段以编程方式创建节点
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
想将其添加为注释,但似乎将代码放入注释中是相当有问题的。所以我们开始吧:我更改了 internas,以便不再使用 cck 字段,而是使用 googletorp 建议的默认位置选项。
创建新位置并将其分配给新节点的实际代码如下所示:
感谢您的指导:)
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:
Thanks for the guidance :)
许多人建议使用
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
关于使用
drupal_execute
以编程方式提交节点的另一个非常好的资源可以在 http://drupal.org/node/293663Instead of
node_save
, many people recommend usingdrupal_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_execute
statement you can see if there were any errors usingform_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 usingform_get_errors
Another very nice resource on programmatic submission of nodes using
drupal_execute
can be found at http://drupal.org/node/293663我已经这样做了,只是没有使用 CCK 字段,而是使用可以添加到节点的默认位置选项。
为了让它工作,我所做的就是首先保存位置(有一个 API 函数),然后从保存的位置添加位置 ID。
示例代码:
注意,
$center
来自外部源,因此与 Drupal 无关。我知道在我的示例中我的所有位置都来自丹麦,因此该部分只是硬编码的。当您不使用CCK字段时,您不需要在节点上保存位置数据,而只需保存位置并自行配对位置即可。这是一个快速的解决方案,而不是像建议的那样通过节点表单运行。对于复杂的节点,这可能是更好的选择,但是当它很简单时,这会完成得更快。
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.
对于 Drupal 7,保存为默认位置选项卡。
灵感来自:此处
For Drupal 7, saving as default location tab.
Inspired from: here