View source
<?php
namespace Drupal\opigno_dashboard;
use Drupal\block\Entity\Block;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class BlockService implements BlockServiceInterface {
use StringTranslationTrait;
public function __construct() {
}
public function getAllBlocks() {
$blockManager = \Drupal::service('plugin.manager.block');
return $blockManager
->getDefinitions();
}
public function getAvailableBlocks() {
$blocks = $this
->getAllBlocks();
$availables = \Drupal::config('opigno_dashboard.settings')
->get('blocks');
$account = \Drupal::currentUser();
$account_roles = $account
->getRoles();
foreach ($blocks as $key1 => &$block) {
if (!isset($availables[$key1]) || isset($availables[$key1]) && !$availables[$key1]['available']) {
unset($blocks[$key1]);
}
else {
$block_real = Block::load($this
->sanitizeId($key1));
if (!$block_real) {
$block_real = Block::load($this
->sanitizeIdOld($key1));
}
$role_access = TRUE;
if (!empty($block_real)) {
$block_visibility = $block_real
->getVisibility();
if (isset($block_visibility['user_role']) && !empty($block_visibility['user_role'])) {
$role_access = FALSE;
foreach ($block_visibility['user_role']['roles'] as $block_role) {
if (in_array($block_role, $account_roles)) {
$role_access = TRUE;
}
}
}
}
if (!$role_access) {
unset($blocks[$key1]);
continue;
}
foreach ($block as &$value) {
if (is_object($value)) {
$value = $value
->render();
}
}
$blocks[$key1]['id'] = $key1;
unset($blocks[$key1]['config_dependencies'], $blocks[$key1]['class'], $blocks[$key1]['provider'], $blocks[$key1]['category'], $blocks[$key1]['deriver'], $blocks[$key1]['context']);
}
}
return array_values($blocks);
}
public function getDashboardBlocksContents() {
$ids = [];
foreach ($this
->getAvailableBlocks() as $block) {
$ids[] = $block['id'];
}
$blocks = [];
foreach ($ids as $id) {
$block = Block::load($this
->sanitizeId($id));
if (!$block) {
$block = Block::load($this
->sanitizeIdOld($id));
}
if (!empty($block)) {
$render = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
$blocks[$id] = \Drupal::service('renderer')
->renderRoot($render);
}
}
return $blocks;
}
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];
unset($settings[$key]);
}
foreach ($values['visibility'] as $id => $visibility) {
$values['visibility'][$id]['id'] = $id;
}
$values['settings'] = $settings;
$block = Block::create($values);
$block
->save();
}
}
}
public function sanitizeId($id) {
return 'dashboard_' . str_replace([
':',
'-',
], [
'_',
'_',
], $id);
}
public function sanitizeIdOld($id) {
return 'dashboard_' . str_replace(':', '_', $id);
}
}