如何使用node_save($ node)创建图像时将图像附加到节点;

时间:2010-08-25 19:26:48

标签: php drupal drupal-6 drupal-7

您好我正在使用drupal 7并尝试通过使用php解析它以及稍后使用node_save($node)创建节点来从xml导入数据。

到目前为止,我已经成功地从xml创建节点而没有任何图像。我想在导入时将图像附加到节点。

我知道drupal 7仍处于alpha 6状态,但很好。 node_save($node)函数与drupal 6中的函数几乎相同,但稍有不同。

好的,我的代码图片文件路径存储在一个变量中...任何帮助都会很棒..提前谢谢

function main($head, $txt) {
  $nodes = array();
  $nodes[0]['title'] = $head; // node name
  $nodes[0]['body'] = $txt; // body text for the node
  $nodes[0]['teaser'] = '<p>A node</p>';
  $nodes[0]['timestamp'] = 1281765101;
  $nodes[0]['format'] = 2;
  make_nodes($nodes);
}

function make_nodes($nodes) {
  $new_node = $nodes[0];
  $node = new stdClass();
  $node->type = 'article';
  $node->status = 1;
  $node->uid = 1;
  $node->title = $new_node['title'];
  $node->promote = 1;
  $node->created = $new_node['timestamp'];
  $node->timestamp = $new_node['timestamp'];
  $node->changed= $new_node['timestamp'];
  $node->sticky = 0;
  $node->language = 'en';
  $node->body['und'][0]['format'] = 3;
  $node->body['und'][0]['summary'] = $new_node['teaser'];
  $node->body['und'][0]['value'] = $new_node['body'];
  $node->revision = 0;
  node_submit($node);
  node_save($node);
}

2 个答案:

答案 0 :(得分:2)

HI。阅读文档10个小时后,我终于做到了......我在这里包含我的代码

$uri = 'bird/bird_image.jpg';
$files =  new stdClass();
$files->uid = (isset($local_user->uid) && !empty($local_user->uid)?$local_user->uid:1);
$files->filename = 'bird.jpg';
$files->uri = $uri; 
$files->filemime = file_get_mimetype($uri);
$files->status = 1;
$files->timestamp = $new_node['timestamp'];

file_copy($files);

这就是人们如何上传文件和drupal 7数据库

答案 1 :(得分:0)

Zero Cool问的原始问题是&#34;如何在创建节点时将图像附加到节点&#34;。

我还没有在任何地方看到完全回答,所以这里有一些代码适用于Drupal 7.27,适用于Windows 8:)

&#39;分类&#39;此节点的内容类型有3个字段:title,body和field_advert_image(它只是一个Image字段类型)。

$node = new stdClass();
$node->type = 'classified';

node_object_prepare($node); // Set some default values.

$node->language = LANGUAGE_NONE; 
$node->title = "Test node with image";

$node->body[LANGUAGE_NONE][0]['value'] = strip_tags("<b>Body text example</b>");
$node->body[LANGUAGE_NONE][0]['format'] = 'plain_text';     // or 'full_html' if you want

// Get pathauto to generate an URL alias
$node->path['pathauto'] = 1;
// Created by user 1
$node->uid = 1;

// The image directory has to be visible to Drupal on its local file-system,
// e.g. here it would be sites/default/files/adverts
$directory_uri = 'public://adverts';

// example image file in that directory:
$my_image_file = '32449.jpg';

// First create a file object, and add it to Drupal's managed_files table...
$file =  new stdClass();
$file->filename = $my_image_file;
$file->uri = $directory_uri.'/'.$my_image_file;
$file->filemime = file_get_mimetype($file->uri);
$file->status = FILE_STATUS_PERMANENT;
$file->uid = 1;
$file = file_save($file);
// ...then use the new file object.
$node->field_advert_image[LANGUAGE_NONE][0] = (array)$file;

node_save($node);       

// N.B. node_save() gets the count incremented in the file_managed table; no need to call file_usage_add()

if ($node->nid) {
    echo "- Created node ".$node->nid." ... ".print_r($node,true)."\n";
} else {
    echo "- Drupal node_save API call failed\n";
}

正如您可能猜到的,此代码从命令行PHP脚本运行。

希望它有所帮助。