Score:0

How do I display a block after changing its ID?

in flag

I have a custom block. I want to change its ID, but after I changed it from main_menu_block to header_block, the block is not displayed anymore.

I tried to:

  1. Remove the block with the old ID
  2. Change the block ID and clear the cache
  3. Add the custom block on the admin side

The following is the block definition.

/**
 * Provides a 'Header Block' block.
 *
 * @Block(
 *   id = "header_block",
 *   admin_label = @Translation("Header Block"),
 *   category = @Translation("Custom"),
 * )
 */
class HeaderBlock extends BlockBase implements ContainerFactoryPluginInterface {
 //...
}

What steps should I take do to display the block after its ID has been changed?

Score:0
in flag

Block placement is usually configuration (e.g. Block Layout, Layout Builder, etc.). That placement configuration will typically reference the block ID. You'll have to rename it everywhere it's been referenced.

You can do it in two ways:

  • On Drupal admin, simply remove and re-add that block.
    • In most cases, the layout page may crash because of a non-existent block. So I'd
      1. Rename the block back to main_menu_block and clear the cache.
      2. Remove the block from Block Layout/Layout Builder.
      3. Rename it back to its header_block and clear cache again.
      4. Re-add the block.
  • If you have a local dev environment
    1. Export your configuration (drush cex)
    2. Find all references to main_menu_block and rename it to header_block.
    3. Import back your configuration (drush cim)

In the odd case where the block is referenced in the content (e.g. node-specific layout, block field, etc.), you will have to update/remove it from the content in some way. If you're still in development, just replace it in the database using a client. But if it's already in production, you can use hook_update_N() to update that content programmatically.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.