Created
June 9, 2018 16:45
-
-
Save algotrader-dotcom/95128f808167f114ecc6613884204a29 to your computer and use it in GitHub Desktop.
Drupal 8 - Create term programatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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