Problem/Motivation
During theme initialization, block_theme_initialize() copies over existing blocks and gives them a machine name prefixed with the theme name. It does not check to see if a block exists with this name already.
Steps to reproduce
See #2858897-17: Block name collision on theme creation:
Initial setup
Prepare two themes:
- Theme 1: themeone, Active, default
- Theme 2: themetwo, Disabled
Setup steps:
- Using Theme 1 theme, place a block with id: a_block_with_a_cool_id
- In the same theme, place a block with this id: themetwo_a_block_with_a_cool_id
- Enable Theme 2. In the process, this will copy the blocks from the default theme themeone to themetwo
- and prepend the theme machine name to the final block ID.
The copy process fails because, when trying to copy a_block_with_a_cool_id to themetwo, this will create themetwo_a_block_with_a_cool_id, resulting in a failure for ID duplicate (because already exists in themeone).
This is from memory, so I hope I don't give any wrong indication.
Proposed resolution
Move the logic in \Drupal\block\BlockForm::getUniqueMachineName to a location where it can be shared with block_theme_initialize.
Remaining tasks
Review latest patch in #2858897-29: Block name collision on theme creation, and get core maintainer review to push this forward.
User interface changes
None?
API changes
@tbd
Data model changes
Drupal will add an integer-increasing counter (e.g. _1, _2) to conflicting blocks when processing a cloned block for an enabled theme (in block_theme_initialize()).