2010-08-25 89 views
4

嗨,我正在与Drupal 7合作,并试图通过解析它使用PHP和稍后创建节点与node_save($node)从XML导入数据。

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

我知道drupal 7仍然在alpha 6中,但它很好。 node_save($node)功能与drupal 6中的功能几乎相同,但略有不同。

确定这里是我的代码映像文件的路径被存储在一个变量...任何帮助将是great..thanks提前

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); 
} 
+1

当您以编程方式创建节点时,一定要填充$ node-> name,因为对于有权限管理的所有用户,node_submit()会将您的$ node-> uid更改为0(匿名)节点,包括您的管理员用户。 – kiamlaluno 2010-08-25 20:16:28

+0

感谢您的提示...但我怎么可以附加图片与此节点作为图像 – 2010-08-25 20:58:34

+0

在drupal 7这不像你不必填充$ node->名称。 – 2010-08-28 01:41:26

回答

2

HI。阅读10小时我finaly做到了文档后...我包括我的代码在这里

$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的数据库

0

原来的问题零酷问的是“如何在创建时将图像附加到节点上”。

我还没有看到任何地方完全回答,所以这里是一些代码,在Drupal 7.27对我的作品,在Windows 8 :)

的“分类”的内容类型为这个节点有3个字段:标题,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脚本运行。

希望它可以帮助别人。