我一直在尝试将图像附加到我使用 drupal 的 API 创建的产品上。到目前为止,我已经能够创建产品并将信息添加到基本文本字段(sku、标题、价格等......)
我在添加到所有产品都有的 field_productimage 字段时遇到了麻烦。到目前为止,我可以让 drupal 将文件上传到其公共目录并将其注册到 file_managed 表中。如何将图像文件附加到 field_productimage 数据字段?
假设您field_productimage
是 type 的字段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
属性并且是数字的。