Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to add a lot of terms (~200) to a vocabulary, but I can't find any import modules that are updated for Drupal 8, and it seems the functions for doing this in Drupal 7 don't exist in Drupal 8. So, can anyone point me in the right direction for doing this?

I attempted to do it with entity_create, as suggested in the comments, with this code:

$term_create = entity_create('taxonomy_term', array('name' => 'test', 'vocabulary_name' => 'client'));

but I got this error:

Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type taxonomy_term in Drupal\Core\Entity\FieldableEntityStorageControllerBase->create() (line 65 of core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php).

Any ideas?

share|improve this question
1  
A term is an entity, so...entity_create() – Clive Apr 2 '14 at 16:40
    
I attempted to do this with this code: $term_create = entity_create('taxonomy_term', array('name' => 'test', 'vocabulary_name' => 'client'));, but I got the error Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type taxonomy_term in Drupal\Core\Entity\FieldableEntityStorageControllerBase->create() (line 65 of core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php). -- Any ideas? – Samsquanch Apr 2 '14 at 17:20
    
Try vid in place of vocabulary_name. Looks like the column is still vid in taxonomy_term_data, but it's the vocab name instead of id now – Clive Apr 2 '14 at 17:26
    
The entity data should not be derived from the SQL tables, see below. – user49 Apr 2 '14 at 17:37
up vote 17 down vote accepted

You know that you want something from taxonomy module so first you need to look in Drupal\taxonomy\Entity -- or the corresponding directory -- you will find the Term class there. Now look at the annotation, it says @ContentEntityType and in there:

*   entity_keys = {
*     "id" = "tid",
*     "bundle" = "vid",
*     "label" = "name",
*     "uuid" = "uuid"
*   },

So, what you want is

$term = Term::create([
  'name' => 'test', 
  'vid' => 'client',
])->save();

because the label entity key is name and the bundle entity key is vid. I added a ->save() call as well as I presume you wanted to save it too.

share|improve this answer
    
Yup, that did it. Thanks! – Samsquanch Apr 2 '14 at 17:42
Term::create([
 'name' => ''Lama',
 'vid' => $vocabulary_id,
]);

The other answers use entity_create(), which works, but is not quite as nice.

share|improve this answer
    
You are right. I edited my answer. Perhaps this answer should have been a suggested edit too :) – user49 Oct 15 '15 at 18:40
    
I'm new to this StackOverflow thing :) – Wim Leers Oct 19 '15 at 13:49

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();
share|improve this answer
    
Something you may want to know. $term will be equal to 1 most likely because Entity::save() returns an int. Either the constants SAVED_NEW or SAVED_UPDATED depending on the operation performed. However, if you were to remove the ->save() and add $term->save();, you will then see that $term is updated with the information that was saved to the database. Example you can now do $tid = $term->tid->value; – General Redneck Jan 23 at 2:22

You may want to look at how devel/devel_generate does this.

From devel_generate :

$values['name'] = devel_generate_word(mt_rand(2, $maxlength));
$values['description'] = "description of " . $values['name'];
$values['format'] = filter_fallback_format();
$values['weight'] = mt_rand(0, 10);
$values['langcode'] = LANGUAGE_NOT_SPECIFIED;
$term = entity_create('taxonomy_term', $values);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.