Drupal considers menus, custom blocks and taxonomy terms as content (although it really isn’t going to be used as content). While importing or exporting these so-called content, you might run into issues such as getting only the configurations exported/imported but not the entire block (or menu or taxonomies). Or sometimes when you try to export a menu, the menu items are often missing. The Structure Sync module in Drupal 9 (and Drupal 8) comes in handy to import or export menus, taxonomy terms and custom blocks via the configuration.
This helpful module reduces the burden of creating the same content in different places and allows for complete syncing between environments. With the Structure Sync module, you can also perform partial imports or exports to blend well with specific environments. The module lets you synchronize content using Drush commands and admin UI, both. Instead of importing and exporting content like menus and vocabularies manually, the Structure Sync module automates the process and saves a lot of time.
Installing the Structure Sync Module
The Drupal 9 Structure Sync module is a contributed module that can be easily downloaded here or with the help of composer.
composer require drupal/structure_sync drush en structure_sync
Implementing Structure Sync with an example
Here, for the sake of the explaining the usage of this module, I will create two Drupal 9 instances.
Let’s create a Custom block in the Drupal9.local instance. We will be exporting this block using the Structure Sync module.
Let’s create a custom block called “Copyrights”, which I will be adding to the footer block of my page layout.
Here in the below screengrab, you can see the Copyrights block created and placed in the footer.
Now comes the interesting part of exporting the footer block that I’ve just created. Let me first push this custom block normally (without using the Structure Sync block) with the push command and then take a pull from the other instance (techx.local).
And now when you check if the Copyrights custom block has been moved to the techx.local instance or not, what you get is a broken block. Here, the configuration got exported but not the block.
To avoid these ugly situations, we will now use the Structure sync module. Now that you have already installed the module, navigate to /admin/structure/structure-sync. Next click on the Custom blocks tab since we will be exporting a custom block.
Here, under Export, you will find a list of your custom blocks. Select the ones that you need to export and then click on the Export custom blocks button. Once you hit export, it becomes a part of the configuration. Now go to your configuration and type the command “drush cex” to export all configurations.
Here in the above screengrab, you can see that structure_sync.data is now a part of your config management. To get the entire block, you will now need to import it from the “techx.local” instance. For that, first you need to run the command “drush cim”. This will import all the configurations into that instance.
Now, in the techx.local instance, navigate to /admin/structure/structure-sync/custom-blocks and click on import custom blocks (safely).
You will now find the custom blocks from the “Drupal9.local” instance have been successfully imported into the “techx.local” instance. Notice that you have three options to import –
- Safely - this is a safe option as it imports only those custom blocks/taxonomies/menus in config that were not previously there.
- Full - this will import all the custom blocks/taxonomies/menus regardless if they have already existed before. It deletes any custom blocks/taxonomies/menus that are not in config.
- Force - Be careful while using this one. The force option will not check for any existing blocks, menus or taxonomies. It deletes all custom blocks/taxonomies/menus in the system and creates new ones.
For this example, I will be selecting the Import custom blocks (safe) option.
On successfully importing the custom blocks, you will now be able to see the “Copyrights” custom block that has been completely imported in the techx.local instance.
If you are not a UI person, you can use these commands to perform successful export and import using the Structure sync module.
The available Drush commands are:
- drush em (export menus)
- drush im (import menus)
- drush eb (export blocks)
- drush ib (import blocks)
- drush et (export taxonomy terms)
- drush it (import taxonomy terms)
- drush ea (export all)
- drush ia (import all)
While importing blocks/menus/taxonomy using drush it prompts you these options:
Select the number mentioned in the boxes to make your choice.
We can use the same steps to export and import Menus and Taxonomy terms as well. One thing that you do need to keep in mind is that when you are exporting / importing menus, blocks or taxonomies, the entire content gets imported/exported. For example, you cannot selectively choose some of the menu items in that menu to be exported/imported.