编程语言
首页 > 编程语言> > php-如何使用node_save创建节点?

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

作者:互联网

我正在尝试将当前的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

标签:drupal-modules,drupal,drupal-7,php
来源: https://codeday.me/bug/20191208/2089038.html