4

I would like to switch admin themes from a core theme (seven) to a custom sub-theme so I can add some customization without hacking core.

When I install the new sub-theme it creates all the block placement settings and leaves them header or content. I haven't changed any regions between the two themes, and just want to copy all the blocks from the seven theme to the current theme. I know I can check block settings with block plugin manager (plugin.manager.block) but I don't know what blocks to edit. I would like to know how to get a list of all the blocks from the old theme and copy all the placement to new theme programmatically.

0
2

You can entity query your relevant block config then create a cloned entity object via Block::createDuplicateBlock() then modify the sub-theme clones before saving the new block config into sub-theme region.

For example:

$block_ids = \Drupal::entityQuery('block')->condition('theme', 'my_base_theme')->condition('region','my_region')->execute();
foreach ($block_ids as $block_id) {
  $parent_block = \Drupal\block\Entity\Block::load($block_id);

  $new_id = str_replace('my_base_theme', 'my_sub_theme', $parent_block->get('id'));
  $child_block = $parent_block->createDuplicateBlock($new_id, 'my_sub_theme');
  // @TODO: set other properties that might need to be unique to this new theme's block

  $child_block->save();
}
1

I have done this before but it's been a while. First, make sure the regions match between your two themes. I think this is what I did:

  • Export configs:
$ drush cex
  • Use text editor to locate all of the config files that start with block.block....

  • Run a bulk find theme: seven and replace with theme: subtheme.

  • Delete the uuid: ... line from each of the updated config files.

  • Import configs:

drush cim
5
  • 1
    Thank you, but I am asking how to this in an update hook programmatically. – Frank Robert Anderson May 6 '20 at 17:23
  • @FrankRobertAnderson – Maybe add that for clarification to your question. – leymannx May 6 '20 at 17:30
  • The post already says that I am asking for a programmatic solution. – Frank Robert Anderson May 6 '20 at 18:49
  • I apologize, @FrankRobertAnderson, sometimes I see programmatically and I just think "not with the GUI." Shawn Conn's answer seems like more the approach you need. – Isaiah Nixon May 7 '20 at 19:15
  • No need to apologize @IsaiahNixon – Frank Robert Anderson May 7 '20 at 19:32

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.