2

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).
7
  • What did you try? The code you posted in this question is exactly the same code in the SO post. Oct 6, 2017 at 8:19
  • Hello Patrick. I have edited the question to show the drupal 8 code I am using. Thanks. Oct 6, 2017 at 8:25
  • And what error are you getting? What do the logs show? Oct 6, 2017 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"); Oct 6, 2017 at 8:40
  • When you debug with something like kint($csv_file); what do you see? That error suggests that $csv_file is empty. Oct 6, 2017 at 8:43

3 Answers 3

2

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');
}
1
  • Did this work? If it works, I'll use it too.
    – Kartagis
    Aug 16, 2018 at 3:44
0

see this article.it says how you can have full control over each excel cell value and insert data into a custom table.

1
  • While linking to articles is fine they can die. You should pull the relevant parts of the code/text from your the referenced page and explain how it can solve the problem.
    – Shawn Conn
    Oct 23, 2020 at 17:45
0

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

It will return just file ids. You need to load file I think.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 21, 2021 at 15:54

Your Answer

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

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