Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Last active May 12, 2019 17:13
Show Gist options
  • Save DanLaufer/4d8e0e497f8fd8c0912384462adc4c84 to your computer and use it in GitHub Desktop.
Save DanLaufer/4d8e0e497f8fd8c0912384462adc4c84 to your computer and use it in GitHub Desktop.
Drupal 8 - Create Taxonomy terms programmatically from CSV
<?php
use Drupal\taxonomy\Entity\Term;
ini_set('auto_detect_line_endings', TRUE);
$rows = array_map('str_getcsv', file('./scripts/media/taxonomy.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
$csv[] = array_combine($header, $row);
}
foreach($csv as $item) {
$create_result = Term::create([
'name' => $item['name'],
'vid' => $item['vocabulary'],
])->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment