Import CSV, row by row, into custom table

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 = DrupalfileEntityFile::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 Drupalcustom_moduleFormCustomForm->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 Drupalcustom_moduleFormCustomForm->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 Drupalcustom_moduleFormCustomForm->submitForm() (line 89 of modules/custom_module/src/Form/CustomForm.php).

Line 89: $csv_file = DrupalfileEntityFile::load($file->fid);

  1. Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalCoreEntityEntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Import CSV, row by row, into custom table

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.