Cristi
Cristi

Reputation: 1

Adding a file to a node using a form

I'm trying to create a node (content type: song) using a form. I can set the tile/description, but when it comes to upload a file, I get this error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'field_file1_display' cannot be null: INSERT INTO
{field_data_field_file1} (entity_type, entity_id, revision_id, bundle,
delta, language, field_file1_fid, field_file1_display,
field_file1_description) VALUES (:db_insert_placeholder_0,
:db_insert_placeholder_1, :db_insert_placeholder_2,
:db_insert_placeholder_3, :db_insert_placeholder_4,
:db_insert_placeholder_5, :db_insert_placeholder_6,
:db_insert_placeholder_7, :db_insert_placeholder_8); Array (
[:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 67
[:db_insert_placeholder_2] => 67 [:db_insert_placeholder_3] => song
[:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und
[:db_insert_placeholder_6] => 55 [:db_insert_placeholder_7] =>
[:db_insert_placeholder_8] => ) in
field_sql_storage_field_storage_write() (line 448 of
/Users/blabla/_server/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module).

This is the code I wrote.

function insertnode_form_submit($form, &$form_state) {
    $node = new StdClass();
    $node->type = 'song';
    $node->status = 1;
    $node->language = LANGUAGE_NONE; 
    $node->title = $form['name']['#value'];
    $node->field_song_description[$node->language][0]['value'] = $form['description']['#value'];

    $file_path = drupal_realpath($form['file1']['#file']->uri);

    $file = (object) array(
        'uid' => 1,
        'uri' => $file_path,
        'filemime' => file_get_mimetype($file_path),
        'status' => 1,
    ); 
    $file = file_copy($file, 'public://');
    $node->field_file1[$node->language][0] = (array)$file;

    //$node->field_file1[$node->language][0] = (array)$form['file1']['#file'];//also tried this but nothing

    node_save($node);
}

Upvotes: 0

Views: 1585

Answers (2)

bobylapointe
bobylapointe

Reputation: 191

You have to add into your node object two values : the fid and the display

Try

$node->field_file1[$node->language][0]['fid'] = $file->fid;
$node->field_file1[$node->language][0]['display'] = 1;

Upvotes: 6

Clive
Clive

Reputation: 36956

The way you normally do this in Drupal 7 is by using the managed_file widget type which handles all the file copying/moving for you and adds the file to the files table. In your $form_state array you get passed the fid from the newly created file:

In your form function:

$form['my_file_field'] = array(
  '#type' => 'managed_file',
  '#title' => 'File',
  '#upload_location' => 'public://my-files/'
);

Then in your form submit function:

// Load the file via file.fid.
$file = file_load($form_state['values']['my_file_field']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);

$node->field_file1[$node->language] = array(
  0 => array('fid' => $file->fid, 'display' => 1, 'description' => 'The description')
);

Upvotes: 0

Related Questions