From 5ac65b1fe51822bb407244f36bf9a6e8e50f2b41 Mon Sep 17 00:00:00 2001 From: alekskn <65650873+alekskn@users.noreply.github.com> Date: Tue, 9 Jul 2024 01:02:09 -0400 Subject: [PATCH] Add delimiter to fgetcsv (#3) * Add delimiter to fgetcsv * Use declared properties and sync content with examples --------- Co-authored-by: Niklan --- .../config/install/custom_csv_import.import.yml | 2 +- .../example/custom_csv_import/src/CSVBatchImport.php | 6 +++--- blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md | 6 +++--- .../config/install/custom_csv_import.import.yml | 2 +- .../example/custom_csv_import/src/CSVBatchImport.php | 6 +++--- .../drupal-8-custom-csv-import-optimization/index.ru.md | 8 ++++---- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/config/install/custom_csv_import.import.yml b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/config/install/custom_csv_import.import.yml index d46062c..fb33d66 100644 --- a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/config/install/custom_csv_import.import.yml +++ b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/config/install/custom_csv_import.import.yml @@ -1,3 +1,3 @@ skip_first_line: 1 delimiter: ';' -enclosure: ',' +enclosure: '"' diff --git a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/CSVBatchImport.php b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/CSVBatchImport.php index 234710a..bbbc619 100644 --- a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/CSVBatchImport.php +++ b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/CSVBatchImport.php @@ -36,7 +36,7 @@ class CSVBatchImport { /** * {@inheritdoc} */ - public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') { + public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') { $this->fid = $fid; $this->file = File::load($fid); $this->skip_first_line = $skip_first_line; @@ -65,9 +65,9 @@ public function parseCSV() { # Если необходимо пропустить первую строку csv файла, то мы просто в # холостую грузим и ничего не делаем с ней. if ($this->skip_first_line) { - fgetcsv($handle, 0, ';'); + fgetcsv($handle, 0, $this->delimiter, $this->enclosure); } - while (($data = fgetcsv($handle, 0, ';')) !== FALSE) { + while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) { $this->setOperation($data); } fclose($handle); diff --git a/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md b/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md index 72105c4..68d69a9 100644 --- a/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md +++ b/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md @@ -121,7 +121,7 @@ private $enclosure; /** * {@inheritdoc} */ -public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') { +public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') { $this->fid = $fid; $this->file = File::load($fid); $this->skip_first_line = $skip_first_line; @@ -362,7 +362,7 @@ class CSVBatchImport { /** * {@inheritdoc} */ - public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') { + public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') { $this->fid = $fid; $this->file = File::load($fid); $this->skip_first_line = $skip_first_line; @@ -874,7 +874,7 @@ class ImportForm extends ConfigFormBase { ```yaml {"header":"Листинг config/install/custom_csv_import.import.yml"} skip_first_line: 1 delimiter: ';' -enclosure: ',' +enclosure: '"' ``` ## Добавление страницы для формы diff --git a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/config/install/custom_csv_import.import.yml b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/config/install/custom_csv_import.import.yml index 934a8eb..4bc4595 100644 --- a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/config/install/custom_csv_import.import.yml +++ b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/config/install/custom_csv_import.import.yml @@ -1,4 +1,4 @@ skip_first_line: 1 delimiter: ';' -enclosure: ',' +enclosure: '"' chunk_size: 20 diff --git a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/CSVBatchImport.php b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/CSVBatchImport.php index e51688e..94f6c12 100644 --- a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/CSVBatchImport.php +++ b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/CSVBatchImport.php @@ -36,7 +36,7 @@ class CSVBatchImport { /** * {@inheritdoc} */ - public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $chunk_size = 20, $batch_name = 'Custom CSV import') { + public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $chunk_size = 20, $batch_name = 'Custom CSV import') { $this->importPluginId = $plugin_id; $this->importPlugin = \Drupal::service('plugin.manager.custom_csv_import') ->createInstance($plugin_id); @@ -61,9 +61,9 @@ public function parseCSV() { $items = []; if (($handle = fopen($this->file->getFileUri(), 'r')) !== FALSE) { if ($this->skip_first_line) { - fgetcsv($handle, 0, ';'); + fgetcsv($handle, 0, $this->delimiter, $this->enclosure); } - while (($data = fgetcsv($handle, 0, ';')) !== FALSE) { + while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) { $items[] = $data; } fclose($handle); diff --git a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/index.ru.md b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/index.ru.md index dfa4ca6..ebcaed0 100644 --- a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/index.ru.md +++ b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/index.ru.md @@ -129,7 +129,7 @@ public function startImport(array &$form, FormStateInterface $form_state) { ```yaml {"header":"/config/install/custom_csv_import.import.yml"} skip_first_line: 1 delimiter: ';' -enclosure: ',' +enclosure: '"' # new chunk_size: 20 ``` @@ -147,7 +147,7 @@ CSVBatchImport в котором происходит парсиснг файл private $chunk_size; # В конструкторе добавляем новый аргумент $chunk_size. -public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $chunk_size = 20, $batch_name = 'Custom CSV import') { +public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $chunk_size = 20, $batch_name = 'Custom CSV import') { # … # И пишем его в переменную. $this->chunk_size = $chunk_size; @@ -164,9 +164,9 @@ public function parseCSV() { $items = []; if (($handle = fopen($this->file->getFileUri(), 'r')) !== FALSE) { if ($this->skip_first_line) { - fgetcsv($handle, 0, ';'); + fgetcsv($handle, 0, $this->delimiter, $this->enclosure); } - while (($data = fgetcsv($handle, 0, ';')) !== FALSE) { + while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) { # Данные мы теперь не устанавливаем на операцию, # а сохраняем в массив для дальнейшего дробления. $items[] = $data;