8

Drupal 8 provides some blocks by default. Eg. Site Branding, Main Navigation, Powered by Drupal, Search etc.

I have created a theme that has Site branding & Main Navigation named regions in which corresponding blocks can be placed.

When I enable my theme, Site Branding & Main Navigation blocks are placed in Header & First Sidebar region of my theme by default respectively.

How can I place Site Branding & Main Navigation blocks to their respective regions by default on theme install?

  • 2
    You have to create block instances which are configuration objects and put them into your theme's config/install directory. – user21641 Mar 3 '16 at 10:37
7

Found the answer:

This can be done by adding a yml file (block instance) for each block in the theme/config/install folder.

For eg., To assign the Site Branding block (machine name system_branding_block) to the Site Branding region (machine name site_branding) add the following code in the config/install/block.block.THEME_NAME_branding.yml file:

langcode: en
status: true
dependencies:
  module:
    - system
  theme:
    - DEPENDENT_THEME
id: THEME_NAME_branding
theme: THEME_NAME
region: site_branding
weight: 0
provider: null
plugin: system_branding_block
settings:
  id: system_branding_block
  label: 'Site branding'
  provider: system
  label_display: '0'
  use_site_logo: true
  use_site_name: true
  use_site_slogan: true
visibility: {  }
6

First of all, you should change each desired block region you want manually at Administration>Structure>Block layout Tab and then go to the config export function at:
Administration>Configuration>Development>Configuration synchronization>Export Tab>Single item to export each the block settings for your theme.
Then you should take a look at to the generated YAML files and placed that in a new folder within your theme at /config/install. you then uninstalled the theme and then reinstalled it as the default.

Summary:
1. Install MY_THEME
2. Place the blocks in the desired regions
3. Export config files and remove from them uuid and core config hash
for example Site Branding block block.block.MY_THEME_branding.yml file

langcode: en
status: true
dependencies:
  module:
    - system
  theme:
    - DEPENDENT_THEME
id: MY_THEME_branding
theme: MY_THEME
region: site_branding
weight: 0
provider: null
plugin: system_branding_block
settings:
  id: system_branding_block
  label: 'Site branding'
  provider: system
  label_display: '0'
  use_site_logo: true
  use_site_name: true
  use_site_slogan: true
visibility: {  }
  1. Place config files to MY_THEME/config/optional or MY_THEME/config/install directory (if you're absolutely sure all dependencies are met)
  2. Uninstall MY_THEME and clear all caches
  3. Reinstall MY_THEME again

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.