Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save algotrader-dotcom/95128f808167f114ecc6613884204a29 to your computer and use it in GitHub Desktop.

Select an option

Save algotrader-dotcom/95128f808167f114ecc6613884204a29 to your computer and use it in GitHub Desktop.
Drupal 8 - Create term programatically
<?php /* Main */
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\taxonomy\Entity\Term;
$term_ids = [];
$tag_vob = 'tags'; // Vocabulary
$tags = ['test 1', 'test 2']; // List of test terms name
foreach ($tags as $tag) {
// Create the taxonomy term.
$new_term = Term::create([
'name' => $tag,
'vid' => $tag_vob,
'parent' => array(),
]);
// Save the taxonomy term.
$new_term->save();
print $new_term->id()."\n";
$term_ids[] = $new_term->id();
}
entity_delete_multiple('taxonomy_term',$term_ids);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment