以编程方式附加文件


25

我创建了“图库”内容类型,并添加了两个字段:“照片”和“文档”。然后,我使用以下代码在“文档”字段中上传文件:

$file = file_save_upload('document', array(
    'file_validate_extensions' => array('txt doc'), // Validate extensions.
));

// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://')) {
  $file->status = FILE_STATUS_PERMANENT;
 // $file->file_display = 1;
  $file = file_save($file);

} else {
  $output = t('Failed to write the uploaded file the site\'s file folder.');
}       
 } else {
$output = t('No file was uploaded.');
 }

我使用以下代码将此文件附加到节点:

$customNode->field_document[$customNode->language][0] = (array)$file;

当我调用node_submit()函数时,出现以下错误:

违反完整性约束:1048字段“​​ field_document_display”不能为空

有人知道我在做什么错吗?

Answers:


29

我通常不会放弃这(array)$file条线,因为字段数据真正唯一需要的是字段,描述和显示。因此,我通常执行以下操作:

$node->field_image[LANGUAGE_NONE][] = array(
  'fid' => $file->fid,
  'display' => 1,
  'description' => '',
);
node_save( $node );

这样,如果需要显示,就不会出错。但这就是我...


让我感到困惑的是为什么它没有默认值。
2012年

您看不到默认值,因为这是直接分配。
Lester Peabody,2015年

7

您的解决方案几乎是正确的;但是,在某些情况下,还需要设置显示和描述。

要使代码正常工作,请执行以下操作:

$file = file_save_upload('document', array(
    'file_validate_extensions' => array('txt doc'), // Validate extensions.
));

// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://')) {
  $file->status = FILE_STATUS_PERMANENT;
 // $file->file_display = 1;
  $file = file_save($file);
  //set the extra values needed to make node_save work
  $file->display = 1;
  $file->description = "";
} else {
  $output = t('Failed to write the uploaded file the site\'s file folder.');
}       
 } else {
$output = t('No file was uploaded.');
 }

2

我认为这里的关键是那些线

$file->display = 1;
$file->description = "";

正如Eric van Eldik指出的那样。我正在努力解决完全相同的问题,只是添加了

$file->display = 1;

没有帮助,但是

$file->description = "";

让我开心。


0

为了以编程方式将文件添加到节点,您可以使用

$managed = TRUE; // Whether or not to create a Drupal file record
$filename = 'public://imdb-cast-' . time() . '.jpg';
$iamge_file = system_retrieve_file($url,$filename , $managed);
if($iamge_file){
$file = file_load(db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField());
$node->field_image['und'][0] = (array) $file;
  }
}

0

只需将解决方案也粘贴到这里,我需要创建一个新节点,并以编程方式上传图像。

$filepath = variable_get('file_public_path') . '/xmas_banner.jpg';
$file_temp = file_get_contents($filepath);
$file_temp = file_save_data($file_temp, file_default_scheme() . '://' .'xmas_banner_nl.jpg', FILE_EXISTS_RENAME);

$node = new stdClass();
$node->type = 'carousel'; // custom content type
$node->title = 'XMAS NL';
$node->field_banner_image['und'][0] = (array) $file_temp;
$node->uid = 1;
$node->status = 0;
$node->active = 0;
$node->promote = 0;
node_save($node);

0

在Drupal 8中以编程方式附加多个文件:

foreach ($fileIds as $fid) {
  $node->field_images[] = [
    'target_id' => $fid,
    'alt' => 'ALT TEXT',
    'title' => 'TITLE TEXT'
  ];
}
$node->save();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.