25

I have created the "Gallery" content type, and added two fields: "photo," and "document." I then used the following code for uploading a file in the "document" field:

$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.');
 }

I am attaching this file to the node using the following code:

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

When I call node_submit() function, I get the following error:

Integrity constraint violation: 1048 Column 'field_document_display' cannot be null

Does anybody know what I am doing wrong?

6 Answers 6

29

I usually don't throw the (array)$file line because really the only thing the field data needs is the fid, description and display. So I usually do the following:

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

This way if the display is required, I don't get an error. But that's just me...

2
  • Confusing to me why it doesn't have default values.
    – 32i
    Apr 3, 2012 at 9:56
  • You don't see default values because this is direct assignment. May 7, 2015 at 17:40
7

Your solution is almost right; however, in some cases it requires that you also set display and description.

To make your code work do this:

$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

I think the key here are those lines

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

as Eric van Eldik pointed out. I was struggling with the exact same issue, adding just

$file->display = 1;

didn't help, but

$file->description = "";

made my day.

0

For adding file programmatically to node you can use

$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

Just going to paste my solution here as well, I needed to create a new node, and upload an image programmatically.

$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

Attach multiple files programmatically in Drupal 8:

foreach ($fileIds as $fid) {
  $node->field_images[] = [
    'target_id' => $fid,
    'alt' => 'ALT TEXT',
    'title' => 'TITLE TEXT'
  ];
}
$node->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.