For one of my recipes, I'm adding a block programmatically in a hook.install function to four layouts. After adding the block, the title display is set to 'default' and I would like to set it to 'none."

Normally, I could change config settings in this way. However, these newly added blocks have hash numbers in path that are random, so this code does not work.

Is there a better way to change this config or how do I find the hash to build the proper path?

Again, the following does not WORK, because the hash is new each time.

    config_set('layout.layout.home', 'content.74a1231a-16dd-4f18-be1f-31c2840bf651.data.settings.title_display', 'none');
    config_set('layout.layout.default', 'content.45b56c0d-1438-45f7-a300-0c4dd8b50f2f.data.settings.title_display', 'none');
    config_set('layout.layout.projects', 'content.20ef52dd-5c9c-442f-b800-b1b819a095db.data.settings.title_display', 'none');
    config_set('layout.layout.services', 'content.43c778b1-a819-45cc-8b39-5728dfa3a8be.data.settings.title_display', 'none');

Comments

I got some help with this in office hours today. I was adding the block in question to the layout with this command.

$default_layout->addBlock('mini_layouts', 'footer_mini_layout', 'footer');

By capturing the results in a variable, like this I would have access to the block hash or UUID in this variable $default_block->uuid:

 $default_block = $default_layout->addBlock('mini_layouts', 'footer_mini_layout', 'footer');

I was then able to do a config_set that looks like this:

config_set('layout.layout.projects', 'content.' . $default_block->uuid . '.data.settings.title_display', 'none');

In the last comment, I showed how I was able to access the uuid for a block immediately after saving the block. I'm now trying to figure out how to find the uuid for a block that already exists.

Any thoughts?

Thanks to @hosef, @docwilmot, and @argiepiano for tips and actual code to try, I ended up with the following which worked for me.

    $home_layout = layout_load('home');
    // loops through blocks in "content" region looking for portfolio_projects-block_1.
    foreach ($home_layout->content as $block_id => $block_info) {
      if ($block_info->delta == 'portfolio_projects-block_1') {
        // Once found, remove it.
        $home_layout->removeBlock($block_info->uuid);
      }
    }
    // Replace with alternative block
    $home_layout->addBlock('views', 'portfolio_projects-block', 'content');
    $home_layout->save();