Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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 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 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 at 17:26
    
The entity data should not be derived from the SQL tables, see below. –  chx Apr 2 at 17:37

1 Answer 1

up vote 8 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 = entity_create('taxonomy_term', array( '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 at 17:42

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.