How to add a taxonomy term programmatically in drupal 8 ?
Create taxonomy term programmatically on Drupal 8
Example : Create a terom of the vocabulary 'test_vocabulary'
//Minimum
$term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'test_vocabulary',
'name' => 'My tag',
]);
$term->save();
//With options
$term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'test_vocabulary',
'langcode' => 'en',
'name' => 'My tag',
'description' => [
'value' => '<p>My description.</p>',
'format' => 'full_html',
],
'weight' => -1,
'parent' => array(0),
]);
$term->save();
//Add term with a custom field. Example : 'field_url'
$term = Term::create([
'vid' => 'test_vocabulary',
'name' => 'The Name',
'field_url' => [$url],
]);
$term->save();
//Check Taxonomy name exist
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', "test_vocabulary");
$query->condition('name', "My tag");
$tids = $query->execute();
Thanks to : https://gist.github.com/facine/35bb291811c146b6fc9e