在 Drupal 7 中以编程方式将图像文件附加到节点 [英] Attaching image files to nodes programmatically in Drupal 7
本文介绍了在 Drupal 7 中以编程方式将图像文件附加到节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以以编程方式向节点添加图像?
Is it possible to add an image to a node programmatically?
推荐答案
这是一个示例代码,您可以使用它与 node_save 一起使用
Here is an example code using which you can use with node_save
$filepath = drupal_realpath('misc/druplicon.png');
// Create managed File object and associate with Image field.
$file = (object) array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath),
'status' => 1,
);
// We save the file to the root of the files directory.
$file = file_copy($file, 'public://');
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
`
这篇关于在 Drupal 7 中以编程方式将图像文件附加到节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文