Drupal 7 |在entity_wrapper未设置后保留文件?

时间:2016-08-31 05:22:21

标签: drupal drupal-7 drupal-fields drupal-entities

问题很简单:如何在服务器和文件表中保留文件,因此在使用实体包装器取消/更改值后,其fid仍然有效?

$ewrapper = entity_metadata_wrapper('node', $sourceNode);
unset($sourceNode->field_image[$sourceNode->language][0]);
$ewrapper->save();

现在,只要调用上述内容,就会删除相关文件。如果我使用相同的事情:

$ewrapper->field_image->set($newImage);

在这种情况下,我需要保留旧图像。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为您应该将文件状态从FILE_STATUS_TEMPORARY更改为FILE_STATUS_PERMANENT。看看这个答案:

https://api.drupal.org/comment/23493#comment-23493

基本上,没有file_set_status()函数,就像Drupal 6有一个函数一样,但现在这段代码应该做同样的工作:

  // First obtain a file object, for example by file_load...
  $file = file_load(10); 

  // Or as another example, the result of a file_save_upload...
  $file = file_save_upload('upload', array(), 'public://', FILE_EXISTS_REPLACE);

  // Now you can set the status
  $file->status = FILE_STATUS_PERMANENT;

  // And save the status.
  file_save($file);

因此,您以一种或另一种方式加载文件对象,更改它的status属性并再次保存对象。

相关问题