You are here

function layout_builder_browser_update_8001 in Layout Builder Browser 8

Migrate layout builder blocks to be config entities on their own.

File

./layout_builder_browser.install, line 6

Code

function layout_builder_browser_update_8001() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('layout_builder_browser.layout_builder_browser_blockcat.') as $blockcat_configname) {
    $blockcat = $config_factory
      ->getEditable($blockcat_configname);
    $category_id = $blockcat
      ->get('id');
    $config_blocks = $blockcat
      ->get('blocks');
    if ($config_blocks) {
      foreach ($config_blocks as $config_block) {
        $lBBlockStorage = \Drupal::entityTypeManager()
          ->getStorage('layout_builder_browser_block');
        $blockdefinition = \Drupal::service('plugin.manager.block')
          ->getDefinition($config_block['block_id']);
        $lBlock = $lBBlockStorage
          ->create([
          'id' => str_replace(':', '', $config_block['block_id']),
          'block_id' => $config_block['block_id'],
          'category' => $category_id,
          'label' => (string) $blockdefinition['admin_label'],
          'weight' => $config_block['weight'] ?: 0,
          'image_path' => $config_block['image_path'] ?: '',
          'image_alt' => $config_block['image_alt'] ?: '',
        ]);
        $lBlock
          ->save();
      }
      $blockcat
        ->clear('blocks')
        ->save();
    }
  }
}