gpt4 book ai didi

php - 如何使用 node_save 创建节点?

转载 作者:搜寻专家 更新时间:2023-10-31 20:51:38 25 4
gpt4 key购买 nike

我正在尝试将我当前的 html 站点迁移到 drupal。我有超过 80,000 个页面需要迁移,所以我想与其坐在电脑前 50 年不如创建一个模块。我能够创建一个从每个目录中提取 html 的脚本,现在我遇到了需要创建节点的路障。我正在尝试使用 node_save() 创建一个新节点,但是当执行 node_save 时,我尝试的所有操作都出现 PDOException 错误。我传入 $node,它是一个数组,然后被转换为一个对象。

PDOException: in field_sql_storage_field_storage_write() (line 424 of /srv/www/htdocs/modules/field/modules/field_sql_storage/field_sql_storage.module).

这就是我们当前创建节点的方式,但它会产生错误:

$node= array(
'uid' => $user->uid,
'name' => $user->name,
'type' => 'page',
'language' => LANGUAGE_NONE,
'title' => $html['title'],
'status' => 1,
'promote' => 0,
'sticky' => 0,
'created' => (int)REQUEST_TIME,
'revision' => 0,
'comment' => '1',
'menu' => array(
'enabled' => 0,
'mlid' => 0,
'module' => 'menu',
'hidden' => 0,
'has_children' => 0,
'customized' => 0,
'options' => array(),
'expanded' => 0,
'parent_depth_limit' => 8,
'link_title' => '',
'description' => '',
'parent' => 'main-menu:0',
'weight' => '0',
'plid' => '0',
'menu_name' => 'main-menu',
),
'path' => array(
'alias' => '',
'pid' => null,
'source' => null,
'language' => LANGUAGE_NONE,
'pathauto' => 1,
),
'nid' => null,
'vid' => null,
'changed' => '',
'additional_settings__active_tab' => 'edit-menu',
'log' => '',
'date' => '',
'submit' => 'Save',
'preview' => 'Preview',
'private' => 0,
'op' => 'Save',
'body' => array(LANGUAGE_NONE => array(
array(
'value' => $html['html'],
'summary' => $link,
'format' => 'full_html',
),
)),
'validated' => true,
);

node_save((object)$node);

// Small hack to link revisions to our test user.
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();

最佳答案

通常我会在文档根目录中创建一个 bulkimport.php 脚本来做这种事情。

这是我在 Drupal 7 中使用的代码:

<?php

define('DRUPAL_ROOT', getcwd());

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);



function _create_node($title="", $body="", $language="und") {

$node = (object)array();

$node->uid = '1';
$node->name = 'admin';

$node->type = 'page';

$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->revision = 1;
$node->language = $language;

$node->title = $title;
$node->body[$language][0] = array(
'value' => $body,
'format' => 'full_html',
);

$node->teaser = '';
$node->log = 'Auto Imported Node';

node_submit($node);
node_save($node);

return $node;

} ### function _create_node

$sith = array(
'Darth Vader' => 'Master: Darth Sidious',
'Darth Sidious' => 'Master: Darth Plagous',
'Darth Maul' => 'Master: Darth Sidious',
'Darth Tyranous' => 'Master: Darth Sidious',
);

foreach($sith as $title=>$body) {
print "Creating Node. Title:[".$title."] \tBody:[".$body."] ";
$node = _create_node($title, $body);
print "\t... Created Node ID: [".$node->nid."]\n";
#print_r($node);
} ### foreach

关于php - 如何使用 node_save 创建节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6863128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com