Uncategorized

Drupal 8/9 Migration: Migrating Taxonomy Term References (Part 2)

Importing tags as taxonomy terms

As a general rule for migrating relations between two entities, first, we need to write a migration for the target entities. In this case, since academic programs have a field named field_tags and we wish to store the IDs of certain taxonomy terms of the vocabulary tags in the program nodes, we need to write a migration to import tags first. Thus, while running the migrations for academic programs, the tags would already exist on the Drupal 8 site and migrate API would be able to refer to these tags using their IDs.

To achieve this, we write a simple migration for the tags as in the migrate_plus.migration.program_tags.yml file. One noteworthy thing about this migration is that we use the tags themselves as the unique ID for the tags. This is because:

  • The tags’ data-source, program.tags.csv, does not provide any unique key for the tags.
  • The academic programs’ data-source, program.data.csv, refers to the tags using the tag text (instead of unique IDs).

Once the tag data is imported, all we have to do is add some simple lines of YAML in the migration definition for academic programs to tell Drupal how to migrate the field_tags property of academic programs.

As we did for program_level in the previous article, we will be specifying multiple plugins for this property:

field_tags:
  -
    plugin: explode
    delimiter: ', '
    source: Tags
  -
    plugin: migration
    migration: program_tags

Here is an explanation of what we are actually doing with the multiple plugins:

  • explode: Taking a look at the data source, we notice that academic programs have multiple tags separated by commas. So, as a first step, we use the explode plugin in order to split (or “explode”) the tags by the delimiter , (comma), thereby creating an array of tags. We do this using plugin: explode and delimiter: ', '. We leave a space after the comma because the data source has a space after its commas, meaning that adding that space in the delimiter will allow us to exclude the spaces from the imported tags.
  • migration_lookup: Now that we have an array of tags, each tag identifying itself using its unique tag text, we can tell the migrate module that these tags are the same ones we imported in migrate_plus.migration.program_tags.yml and that the tags generated during that migration are to be used here in order to associate them to the academic programs. We do this using plugin: migration_lookup and migration: program_tags. You can read more about the migration_lookup plugin on Drupal.org.
migration_dependencies:
  optional:
    - program_tags
#     - program_image

To make sure that tag data is imported and available during the academic program migration, we specify the program_tags migration in the migration_dependencies for the program_data migration. Now, when you re-run these migrations, the taxonomy terms get associated with the academic program nodes. At this stage, you can keep the program_image dependency still commented, because we haven’t written it yet.

 

Migrated tags visible in UI
Migrated tags visible in UI.

 

As simple as it may sound, this is all that is needed to associate the tags to the academic programs! All that is left is re-installing the c11n_migrate module and executing the migrations using the following drush command: drush mi --group=c11n --update. You should see the following output:

$ drush mi --group=c11n --update
Processed 8 items (8 created, 0 updated, 0 failed, 0 ignored) - done with 'program_tags'
Processed 4 items (0 created, 4 updated, 0 failed, 0 ignored) - done with 'program_data'

Because of the migration_dependencies we specified, the program_tags migration was run before the program_data migration.

 

Read More 1501
Read More 1502
Read More 1503
Read More 1504
Read More 1505
Read More 1506
Read More 1507
Read More 1508
Read More 1509
Read More 1510
Read More 1511
Read More 1512
Read More 1513
Read More 1514
Read More 1515
Read More 1516
Read More 1517
Read More 1518
Read More 1519
Read More 1520
Read More 1521
Read More 1522
Read More 1523
Read More 1524
Read More 1525
Read More 1526
Read More 1527
Read More 1528
Read More 1529
Read More 1530
Read More 1531
Read More 1532
Read More 1533
Read More 1534
Read More 1535
Read More 1536
Read More 1537
Read More 1538
Read More 1539
Read More 1540
Read More 1541
Read More 1542
Read More 1543
Read More 1544
Read More 1545
Read More 1546
Read More 1547
Read More 1548
Read More 1549
Read More 1550
Read More 1551
Read More 1552
Read More 1553
Read More 1554
Read More 1555
Read More 1556
Read More 1557
Read More 1558
Read More 1559
Read More 1560
Read More 1561
Read More 1562
Read More 1563
Read More 1564
Read More 1565
Read More 1566
Read More 1567
Read More 1568
Read More 1569
Read More 1570
Read More 1571
Read More 1572
Read More 1573
Read More 1574
Read More 1575
Read More 1576
Read More 1577
Read More 1578
Read More 1579
Read More 1580
Read More 1581
Read More 1582
Read More 1583
Read More 1584
Read More 1585
Read More 1586
Read More 1587
Read More 1588
Read More 1589
Read More 1590
Read More 1591
Read More 1592
Read More 1593
Read More 1594
Read More 1595
Read More 1596
Read More 1597
Read More 1598
Read More 1599
Read More 1600
Read More 1601
Read More 1602
Read More 1603
Read More 1604
Read More 1605
Read More 1606
Read More 1607
Read More 1608
Read More 1609
Read More 1610
Read More 1611
Read More 1612
Read More 1613
Read More 1614
Read More 1615
Read More 1616
Read More 1617
Read More 1618
Read More 1619
Read More 1620
Read More 1621
Read More 1622
Read More 1623
Read More 1624
Read More 1625
Read More 1626
Read More 1627
Read More 1628
Read More 1629
Read More 1630
Read More 1631
Read More 1632
Read More 1633
Read More 1634
Read More 1635
Read More 1636
Read More 1637
Read More 1638
Read More 1639
Read More 1640
Read More 1641
Read More 1642
Read More 1643
Read More 1644
Read More 1645
Read More 1646
Read More 1647
Read More 1648
Read More 1649
Read More 1650
Read More 1651
Read More 1652
Read More 1653
Read More 1654
Read More 1655
Read More 1656
Read More 1657
Read More 1658
Read More 1659
Read More 1660
Read More 1661
Read More 1662
Read More 1663
Read More 1664
Read More 1665
Read More 1666
Read More 1667
Read More 1668
Read More 1669
Read More 1670
Read More 1671
Read More 1672
Read More 1673
Read More 1674
Read More 1675
Read More 1676
Read More 1677
Read More 1678
Read More 1679
Read More 1680
Read More 1681
Read More 1682
Read More 1683
Read More 1684
Read More 1685
Read More 1686
Read More 1687
Read More 1688
Read More 1689
Read More 1690
Read More 1691
Read More 1692
Read More 1693
Read More 1694
Read More 1695
Read More 1696
Read More 1697
Read More 1698
Read More 1699
Read More 1700
Read More 1701
Read More 1702
Read More 1703
Read More 1704
Read More 1705
Read More 1706
Read More 1707
Read More 1708
Read More 1709
Read More 1710
Read More 1711
Read More 1712
Read More 1713
Read More 1714
Read More 1715
Read More 1716
Read More 1717
Read More 1718
Read More 1719
Read More 1720
Read More 1721
Read More 1722
Read More 1723
Read More 1724
Read More 1725
Read More 1726
Read More 1727
Read More 1728
Read More 1729
Read More 1730
Read More 1731
Read More 1732
Read More 1733
Read More 1734
Read More 1735
Read More 1736
Read More 1737
Read More 1738
Read More 1739
Read More 1740
Read More 1741
Read More 1742
Read More 1743
Read More 1744
Read More 1745
Read More 1746
Read More 1747
Read More 1748
Read More 1749
Read More 1750
Read More 1751
Read More 1752
Read More 1753
Read More 1754
Read More 1755
Read More 1756
Read More 1757
Read More 1758
Read More 1759
Read More 1760
Read More 1761
Read More 1762
Read More 1763
Read More 1764
Read More 1765
Read More 1766
Read More 1767
Read More 1768
Read More 1769
Read More 1770
Read More 1771
Read More 1772
Read More 1773
Read More 1774
Read More 1775
Read More 1776
Read More 1777
Read More 1778
Read More 1779
Read More 1780
Read More 1781
Read More 1782
Read More 1783
Read More 1784
Read More 1785
Read More 1786
Read More 1787
Read More 1788
Read More 1789
Read More 1790
Read More 1791
Read More 1792
Read More 1793
Read More 1794
Read More 1795
Read More 1796
Read More 1797
Read More 1798
Read More 1799
Read More 1800
Read More 1801
Read More 1802
Read More 1803
Read More 1804
Read More 1805
Read More 1806
Read More 1807
Read More 1808
Read More 1809
Read More 1810
Read More 1811
Read More 1812
Read More 1813
Read More 1814
Read More 1815
Read More 1816
Read More 1817
Read More 1818
Read More 1819
Read More 1820
Read More 1821
Read More 1822
Read More 1823
Read More 1824
Read More 1825
Read More 1826
Read More 1827
Read More 1828
Read More 1829
Read More 1830
Read More 1831
Read More 1832
Read More 1833
Read More 1834
Read More 1835
Read More 1836
Read More 1837
Read More 1838
Read More 1839
Read More 1840
Read More 1841
Read More 1842
Read More 1843
Read More 1844
Read More 1845
Read More 1846
Read More 1847
Read More 1848
Read More 1849
Read More 1850
Read More 1851
Read More 1852
Read More 1853
Read More 1854
Read More 1855
Read More 1856
Read More 1857
Read More 1858
Read More 1859
Read More 1860
Read More 1861
Read More 1862
Read More 1863
Read More 1864
Read More 1865
Read More 1866
Read More 1867
Read More 1868
Read More 1869
Read More 1870
Read More 1871
Read More 1872
Read More 1873
Read More 1874
Read More 1875
Read More 1876
Read More 1877
Read More 1878
Read More 1879
Read More 1880
Read More 1881
Read More 1882
Read More 1883
Read More 1884
Read More 1885
Read More 1886
Read More 1887
Read More 1888
Read More 1889
Read More 1890
Read More 1891
Read More 1892
Read More 1893
Read More 1894
Read More 1895
Read More 1896
Read More 1897
Read More 1898
Read More 1899
Read More 1900
Read More 1901
Read More 1902
Read More 1903
Read More 1904
Read More 1905
Read More 1906
Read More 1907
Read More 1908
Read More 1909
Read More 1910
Read More 1911
Read More 1912
Read More 1913
Read More 1914
Read More 1915
Read More 1916
Read More 1917
Read More 1918
Read More 1919
Read More 1920
Read More 1921
Read More 1922
Read More 1923
Read More 1924
Read More 1925
Read More 1926
Read More 1927
Read More 1928
Read More 1929
Read More 1930
Read More 1931
Read More 1932
Read More 1933
Read More 1934
Read More 1935
Read More 1936
Read More 1937
Read More 1938
Read More 1939
Read More 1940
Read More 1941
Read More 1942
Read More 1943
Read More 1944
Read More 1945
Read More 1946
Read More 1947
Read More 1948
Read More 1949
Read More 1950
Read More 1951
Read More 1952
Read More 1953
Read More 1954
Read More 1955
Read More 1956
Read More 1957
Read More 1958
Read More 1959
Read More 1960
Read More 1961
Read More 1962
Read More 1963
Read More 1964
Read More 1965
Read More 1966
Read More 1967
Read More 1968
Read More 1969
Read More 1970
Read More 1971
Read More 1972
Read More 1973
Read More 1974
Read More 1975
Read More 1976
Read More 1977
Read More 1978
Read More 1979
Read More 1980
Read More 1981
Read More 1982
Read More 1983
Read More 1984
Read More 1985
Read More 1986
Read More 1987
Read More 1988
Read More 1989
Read More 1990
Read More 1991
Read More 1992
Read More 1993
Read More 1994
Read More 1995
Read More 1996
Read More 1997
Read More 1998
Read More 1999
Read More 2000

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button