【问题标题】:Drupal API - Attach Image to field_productimageDrupal API - 将图像附加到 field_productimage
【发布时间】:2013-06-04 08:14:15
【问题描述】:
我一直在尝试将图像附加到我使用 drupal 的 API 创建的产品上。到目前为止,我已经能够创建产品并将信息添加到基本文本字段(sku、标题、价格等......)
我在添加所有产品都有的 field_productimage 字段时遇到了问题。到目前为止,我可以让 drupal 将文件上传到其公共目录并将其注册到 file_managed 表中。如何将图像文件附加到 field_productimage 数据字段?
【问题讨论】:
标签:
image
api
file
drupal
【解决方案1】:
假设您的field_productimage 是image 类型的字段,以下是如何将图像附加到该字段。
$node = new stdClass();
// Build other node stuff here.
// $file_id is the file ID from file_managed table.
$file = file_load($file_id);
$node->field_productimage[LANGUAGE_NONE][] = (array) $file;
// again, other stuff.
$node = node_submit($node); // Prepare node for saving
node_save($node);
确保$file 对象具有$file->fid 属性并且是数字。