Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughMigrating Drupal 7 textfield to Drupal 8 vocabulary
In our Drupal 7 site we have an enhanced textfield that autocompletes already stored values. When migrating to Drupal 8 we could normalize this by using a vocabulary and terms instead.
So in our current Drupal 7 setup we have something like:
Content type: Employee
nid | name | field_role |
---|---|---|
1 | John | Web developer |
2 | Emil | Web developer |
3 | Henrik | Web designer |
4 | Karl | Web designer |
5 | David | Sales |
6 | John | Sales |
And in Drupal 8 we want this instead:
Vocabulary: Role
tid | title |
---|---|
1 | Web developer |
2 | Web designer |
3 | Sales |
Content type: Employee
nid | name | field_role |
---|---|---|
1 | John | 1 |
2 | Emil | 1 |
3 | Henrik | 2 |
4 | Karl | 2 |
5 | David | 3 |
6 | John | 3 |
Using entity_generate process plugin
The first approach we can take is to use the entity_generate
plugin provided by Migrate Plus module.
In our migration file:
process:
field_role:
# Plugin to use
plugin: entity_generate
# Field from source configuration
source: field_role
# Value to compare in the bundle
value_key: name
# Bundle key value
# If you get errors consider using only bundle
bundle_key: vid
# Bundle machine name
bundle: role
# Type of entity
entity_type: taxonomy_term
# Set to true to ignore case on lookup
ignore_case: true
Running this migration will take the value of the textfield and try to lookup the term by name on the destination and if it does not exist the term will be created.
In the second part of this article (coming soon) we will take a look at how we can deal with translations.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web