1

I have a Drupal 7 submit method for updating CSV row by row to a custom table which I found under this link. I have tried to update it so that it works in Drupal 8 to no avail. How do I to modify it to work in Drupal 8?

function custom_module_submit($form, &$form_state) {

    $file = $form_state['values']['csv_upload_file'];
    $file->status = FILE_STATUS_PERMANENT;
    $file->filename = str_replace(' ', '_', $file->filename);
    file_save($file);

    $csv_file = file_load($file->fid);
    $file = fopen($csv_file->uri, "r");


    while(! feof($file))
    {
        $customer = fgetcsv($file));
        db_insert('your_db_table')
            ->fields(array(
                'column1' => $customer[0],
                'column2' => $customer[1]
            ))
            ->execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}

The Drupal 8 code that I am trying to use is as below:

public function submitForm(array &$form, FormStateInterface $form_state)
{
    $file = $form_state->getValue('csv_upload');
    $file->status = FILE_STATUS_PERMANENT;
    $file->filename = str_replace(' ', '_', $file->filename);
    file_save_data($file);

    $csv_file = \Drupal\file\Entity\File::load($file->fid);
    $file = fopen($csv_file->getFileUri(), "r");

    while (!feof($file)) {
        $customer = fgetcsv($file);
        \Drupal::database()->insert('your_db_table')
            ->fields(array(
                'column1' => $customer[0],
                'column2' => $customer[1]
            ))
            ->execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}

Errors include:

  1. Fatal error: Call to a member function getFileUri() on null in /home/annuities/public_html/modules/custom_module/src/Form/C‌​ustomForm.php on line 90.

Line 90: $file = fopen($csv_file->getFileUri(), "r");

  1. Warning: Creating default object from empty value in Drupal\custom_module\Form\CustomForm->submitForm() (line 85 of modules/custom_module/src/Form/CustomForm.php).

Line 85: $file->status = FILE_STATUS_PERMANENT;

  1. Notice: Undefined property: stdClass::$filename in Drupal\custom_module\Form\CustomForm->submitForm() (line 86 of modules/custom_module/src/Form/CustomForm.php).

Line 86: $file->filename = str_replace(' ', '_', $file->filename);

  1. The file could not be created. Notice: Undefined property: stdClass::$fid in Drupal\custom_module\Form\CustomForm->submitForm() (line 89 of modules/custom_module/src/Form/CustomForm.php).

Line 89: $csv_file = \Drupal\file\Entity\File::load($file->fid);

  1. Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
  • What did you try? The code you posted in this question is exactly the same code in the SO post. – Patrick Kenny Oct 6 '17 at 8:19
  • Hello Patrick. I have edited the question to show the drupal 8 code I am using. Thanks. – Tim Kariuki Oct 6 '17 at 8:25
  • And what error are you getting? What do the logs show? – Patrick Kenny Oct 6 '17 at 8:26
  • Fatal error: Call to a member function getFileUri() on null in /home/annuities/public_html/modules/custom_module/src/Form/CustomForm.php on line 90. Line 90 corresponds to $file = fopen($csv_file->getFileUri(), "r"); – Tim Kariuki Oct 6 '17 at 8:40
  • When you debug with something like kint($csv_file); what do you see? That error suggests that $csv_file is empty. – Patrick Kenny Oct 6 '17 at 8:43
1

I used the code below;

public function submitForm(array &$form, FormStateInterface $form_state)
{

    $file = $form_state->getValue('csv_upload_file');

    $destination = $file[0]->toArray()['uri'][0]['value'];

    $file = fopen($destination, "r");

    while (!feof($file)) {
        $customer = fgetcsv($file);
        \Drupal::database()->insert('your_db_table')
            ->fields(array(
                'column1' => $customer[0],
                'column2' => $customer[1]
            ))
            ->execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}
  • Did this work? If it works, I'll use it too. – Kartagis Aug 16 '18 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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