You are here

public function ThemeHandler::placeBlocks in Commerce Demo 8

File

src/ThemeHandler.php, line 30

Class

ThemeHandler

Namespace

Drupal\commerce_demo

Code

public function placeBlocks($theme) {
  $method = 'dataFor' . Container::camelize($theme) . 'Blocks';
  if (!method_exists($this, $method)) {
    return;
  }
  $blocks = $this
    ->blockDefinitions();
  $this
    ->{$method}($blocks);
  foreach ($blocks as $block_id => $block_data) {

    /** @var \Drupal\block\Entity\Block $existing_block */
    $block_id = "{$theme}_{$block_id}";
    $existing_block = $this->blockStorage
      ->load($block_id);
    if (!$existing_block) {
      $block_data['id'] = $block_id;
      $block_data['theme'] = $theme;

      /** @var \Drupal\block\Entity\Block $block */
      $block = $this->blockStorage
        ->create($block_data);
      $block
        ->setStatus(TRUE);
      $block
        ->save();
    }
    else {
      $existing_block
        ->setRegion($block_data['region']);
      $existing_block
        ->setWeight($block_data['weight']);
      foreach ($block_data['visibility'] as $instance_id => $instance_config) {
        $existing_block
          ->setVisibilityConfig($instance_id, $instance_config);
      }
      $existing_block
        ->setStatus(TRUE);
      $existing_block
        ->save();
    }
  }
}