Drupal - Import taxonomy terms in hierarchical order

By xngo on January 22, 2020

In Drupal 8, the best module to import taxonomy terms in hierarchical order is Taxonomy Import. The good thing about this module is duplicate terms are ignored. However, it reorders your terms in alphabetical order.

Here is an example of my CSV file that I used to import.

Country,Parent
Canada,
Alberta,Canada
British Columbia,Canada
Manitoba,Canada
New Brunswick,Canada
Newfoundland and Labrador,Canada
Nova Scotia,Canada
Ontario,Canada
Prince Edward Island,Canada
Quebec,Canada
Saskatchewan,Canada
Northwest Territories,Canada
Yukon,Canada
Nunavut,Canada

The end result look like the followings.

Drupal 8 - Import taxonomy terms in hierarchical order

Error

If your Drupal installation uses prefix for your database table, then you will get the following error message:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'YOUR_PREFIX.file_managed' doesn't exist: SELECT file_managed.uri FROM file_managed ORDER BY file_managed.fid DESC limit 1; Array ( ) in Drupal\taxonomy_import\Form\create_taxonomy() (line 87 of /.../.../modules/taxonomy_import/src/Form/ImportForm.php).

To resolve this issue, use the 8.x-1.x-dev version. For details about the bug can be found at https://www.drupal.org/project/taxonomy_import/issues/3017222.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.