How to copy blocks placement and settings between themes? suggests that, in Drupal 7, I could run the following two SQL queries to copy block placements and settings between themes.


INSERT INTO block (module, delta, theme, status, weight, region, custom, visibility, pages, title, cache, i18n_mode) (SELECT module, delta, 'NEW-THEME', status, weight, region, custom, visibility, pages, title, cache, i18n_mode FROM block WHERE theme = 'OLD-THEME')

What is the equivalent query/method I could use to achieve the same task in Drupal 8?


Drupal 8 block settings are in configuration files. It is possible to transfer them but takes a bit of editing / file renaming.

You first have to do a complete export of the configuration files. In this, the ones you are looking for have names like block.block....yml, e.g.: block.block.old_theme_main_menu.yml

First step is to rename these to block.block.new_theme...yml, e.g : block.block.new_theme_main_menu.yml. Generally a quick bit of editing to create a script to do the renaming is the easiest.

Next, you need to change the internal theme references to the old theme to the new theme. E.g. the old theme yml file will look like:

langcode: en
status: true
    - system.menu.main
    - block_class
    - system
    - old_theme
    classes: ''
id: old_theme_main_menu
theme: old_theme
region: navigation_collapsible
weight: -34
provider: null
plugin: 'system_menu_block:main'
  id: 'system_menu_block:main'
  label: 'Main navigation'
  provider: system
  label_display: '0'
  level: 1
  depth: 2
visibility: {  }

A good editor should be able to do a replace in files to change old_theme to new_theme. Double check that your theme names are unique in these files.

If the new theme does not have the same regions, then you will need to figure out how to change the region: setting to match your new theme.

In your new theme, create a config directory in the root and then an install directory under it.

Put all the yml files in the install directory.

Uninstall / re-install the new theme to import the settings here.

This assumes the same site with the same code / custom blocks.

If you are going to use this theme in a new site, you will have to make sure the site will provide the blocks defined in the yml config. Generally, the install directory is used to place system level blocks (e.g main menu) that core provides. But if you set up a system with a distro or manually to provide other blocks, this works as well.

There may be some minor gotcha's like visibility settings using the wrong node id and the like.

|improve this answer|||||

There is a module available for Block backup and restore. https://www.drupal.org/project/block_backup_restore

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.