Move blocks from one region to another with code. (Drupal 8)

Events happening in the community are now at Drupal community events on www.drupal.org.
vacho's picture

Hi,

I need help with programing.

I try move block from one region to another with code at Drupal 8.

In specific I try move the "login" block, since sidebar_first region to content region.

Wear hours trying things and can not. Any tips?

Comments

i solve this

vacho's picture

I solve this with this code.

function mymodule_block_access(Block $block, $operation, AccountInterface $account, $langcode) {

  $is0 = false;
  if(drupal_is_front_page() AND !\Drupal::currentUser()->isAuthenticated()) {
    $is0 = true;
  }
 
  if($is0){
    if($block->id == "bartik_login") {
      $block->set('region', 'content');
    }

    if($block->id == "bartik_powered") {
      $block->disable();
    }

    $block->save();
  }

  return;
}