You are here

public function BlockService::createBlocksInstances in Opigno dashboard 8

Same name and namespace in other branches
  1. 3.x src/BlockService.php \Drupal\opigno_dashboard\BlockService::createBlocksInstances()

Creates blocks instances.

File

src/BlockService.php, line 127

Class

BlockService
Class BlockService.

Namespace

Drupal\opigno_dashboard

Code

public function createBlocksInstances() {
  $items = $this
    ->getAvailableBlocks();
  $config = \Drupal::configFactory();
  $theme = $config
    ->get('opigno_dashboard.settings')
    ->get('theme');
  foreach ($items as $item) {
    $id = $this
      ->sanitizeId($item['id']);
    if (!Block::load($id)) {
      $settings = [
        'plugin' => $item['id'],
        'region' => 'content',
        'id' => $id,
        'theme' => isset($theme) ? $theme : $config
          ->get('system.theme')
          ->get('default'),
        'label' => $this
          ->t('Dashboard:') . ' ' . $item['admin_label'],
        'visibility' => [
          'request_path' => [
            'id' => 'request_path',
            'pages' => '<front>',
            'negate' => FALSE,
            'context_mapping' => [],
          ],
        ],
        'weight' => 0,
      ];
      $values = [];
      foreach ([
        'region',
        'id',
        'theme',
        'plugin',
        'weight',
        'visibility',
      ] as $key) {
        $values[$key] = $settings[$key];

        // Remove extra values that do not belong in the settings array.
        unset($settings[$key]);
      }
      foreach ($values['visibility'] as $id => $visibility) {
        $values['visibility'][$id]['id'] = $id;
      }
      $values['settings'] = $settings;
      $block = Block::create($values);
      $block
        ->save();
    }
  }
}