CSV Importer
Mon Jan 17 2022 21:39:57 GMT+0000 (Coordinated Universal Time)
<?php
namespace Drupal\csv_import\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CSVImportForm extends FormBase {
public function getFormId() {
return 'csv_import';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['csv_file'] = [
'#title' => 'Upload your file',
'#type' => 'managed_file',
'#upload_location' => 'public://',
'#upload_validators' => [
'file_validate_extensions' => ['csv']
]
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$csv_file = $form_state->getValue('csv_file');
$csv_file = reset($csv_file);
$file = File::load($csv_file);
$data = $this->csvtoarray($file->getFileUri(), ',');
foreach ($data as $row) {
$operations[] = ['\Drupal\csv_import\Form\CSVImportForm::addImportContentItem', [$row]];
}
$batch = [
'title' => $this->t('Importing data...'),
'operations' => $operations,
'init_message' => $this->t('Import is starting.'),
'finished' => '\Drupal\csv_import\Form\CSVImportForm::addImportContentItemCallback',
];
batch_set($batch);
$this->messenger()->addMessage($this->t('Your form was submitted!'));
}
public function csvtoarray($filename='', $delimiter) {
if (!file_exists($filename) || !is_readable($filename)) return FALSE;
$header = NULL;
if(($handle = fopen($filename, 'r')) !== FALSE) {
while(($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
if(!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
public static function addImportContentItem($item, &$context) {
$message = 'Creating ' . $item['Title'];
$results = [];
self::create_node($item);
$context['message'] = $message;
$context['results'] = $item;
}
public function addImportContentItemCallback($success, $results, $operations) {
$number = count($results);
if ($success) {
$message = new PluralTranslatableMarkup($number, 'One item processed.','@count items processed.');
} else {
$message = t('Finished with an error.');
}
\Drupal::messenger()->addMessage(t($message->render()));
}
public static function create_node($item)
{
$data = file_get_contents($item['Image']);
$file = file_save_data($data, 'public://sample.png', \Drupal\Core\File\FileSystemInterface::EXISTS_RENAME);
$nodes = [
'type' => 'articles_about_programming',
'title' => $item['Title'],
'field_description' => $item['Description'],
'field_myimage' => [
'target_id' => $file->id(),
'alt' => 'Sample',
'title' => 'Sample file',
],
'field_link_to_website' => [
'uri' => 'https://www.' . $item['Link to website'],
'title' => $item['Title'],
'options' => ['target' => '_blank'],
],
];
$node = Node::create($nodes);
$node->setPublished(TRUE);
$node->save();
}
}



Comments