public function BlockService::createBlocksInstances in Opigno dashboard 3.x
Same name and namespace in other branches
- 8 src/BlockService.php \Drupal\opigno_dashboard\BlockService::createBlocksInstances()
Creates blocks instances.
Overrides BlockServiceInterface::createBlocksInstances
File
- src/
BlockService.php, line 205
Class
- BlockService
- The dashboard block manager service definition.
Namespace
Drupal\opigno_dashboardCode
public function createBlocksInstances() : void {
$items = $this
->getAvailableBlocks();
$theme = $this->configFactory
->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' => $theme ?? $this->configFactory
->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);
try {
$block
->save();
} catch (EntityStorageException $e) {
watchdog_exception('opigno_dashboard_exception', $e);
}
}
}
}