Block instanciation and placement using code

Created on 26 June 2024, 2 months ago
Updated 29 July 2024, about 1 month ago

Problem/Motivation
Would like to add a menu and place it in sidebar region in an update hook

Steps to reproduce
Create an update hook
Create menu
Create block
Place block in sidebar region in the hook update (undone)

Proposed resolution
Add code api interface for block instantiation and placement

๐Ÿ’ฌ Support request
Status

Postponed: needs info

Version

10.3 โœจ

Component
Blockย  โ†’

Last updated 3 days ago

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia Pemson18 Goa

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Pemson18
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    This should be possible today with code. You havenโ€™t demonstrated it is not.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Pemson18 Goa

    I had to create a menu in the drupal admin backend ui and place it using the block layout admin section and then export it and in the menu link update the menu links for the menu in update hook. so its a mixture of ui config + update code not pure code.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    You are saying that Drupal has no block placement API. Do I have that right?

    Is Block::create not working in this case?

  • Status changed to Postponed: needs info about 1 month ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen
  • ๐Ÿ‡จ๐Ÿ‡ทCosta Rica yuvania

    Hi @Pemson18,

    I've reviewed the discussion, and I'd like to offer a suggestion that might help clarify the process of block instantiation and placement using code in Drupal 10.

    From my experience, it is possible to create and place blocks programmatically without using the UI configuration. Here's a basic example of how you might achieve this in an update hook:

    use Drupal\block\Entity\Block;
    
    /**
     * Implements hook_update_N().
     */
    function mymodule_update_10001() {
      // Create a new custom block.
      $block = Block::create([
        'id' => 'my_custom_block',
        'theme' => 'bartik',
        'plugin' => 'my_custom_block_plugin',
        'settings' => [],
        'visibility' => [],
        'region' => 'sidebar_first',
        'weight' => 0,
      ]);
      $block->save();
    
      // Ensure the block is placed in the desired region.
      \Drupal::service('theme_handler')->rebuildThemeData();
      \Drupal::service('theme_handler')->refreshInfo();
      $theme = \Drupal::theme()->getActiveTheme();
      $theme_name = $theme->getName();
    
      // Clear cache to apply changes.
      \Drupal::service('cache.render')->invalidateAll();
    }
    

    This code snippet creates a custom block and places it in the 'sidebar_first' region of the 'bartik' theme. Note that you need to adjust 'my_custom_block_plugin' to your actual block plugin ID.

    Make sure to clear the cache after running the update hook to see the changes. This approach should bypass the need for UI configuration and ensure that your block is placed directly via code.

    I hope this helps clarify the problem! Let me know if you need further clarification or assistance.

Production build 0.71.5 2024