class BlockService in Opigno dashboard 8
Same name and namespace in other branches
- 3.x src/BlockService.php \Drupal\opigno_dashboard\BlockService
Class BlockService.
Hierarchy
- class \Drupal\opigno_dashboard\BlockService implements BlockServiceInterface uses StringTranslationTrait
Expanded class hierarchy of BlockService
2 files declare their use of BlockService
- opigno_dashboard.install in ./
opigno_dashboard.install - Install, update and uninstall functions for the alt_aero_log_contacts module.
- opigno_dashboard.module in ./
opigno_dashboard.module - Contains opigno_dashboard.module.
1 string reference to 'BlockService'
1 service uses BlockService
File
- src/
BlockService.php, line 11
Namespace
Drupal\opigno_dashboardView source
class BlockService implements BlockServiceInterface {
use StringTranslationTrait;
/**
* Constructs a new BlockService object.
*/
public function __construct() {
}
/**
* Returns all blocks.
*/
public function getAllBlocks() {
$blockManager = \Drupal::service('plugin.manager.block');
return $blockManager
->getDefinitions();
}
/**
* Returns available blocks.
*/
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 {
// Check access first.
$block_real = Block::load($this
->sanitizeId($key1));
if (!$block_real) {
// Try to load old version of block.
$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);
}
/**
* Returns blocks contents.
*/
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) {
// Try to load old version of 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;
}
/**
* Creates blocks instances.
*/
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();
}
}
}
/**
* Sanitizes ID string.
*/
public function sanitizeId($id) {
return 'dashboard_' . str_replace([
':',
'-',
], [
'_',
'_',
], $id);
}
/**
* Sanitizes ID string for legacy blocks.
*/
public function sanitizeIdOld($id) {
return 'dashboard_' . str_replace(':', '_', $id);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockService:: |
public | function | Creates blocks instances. | |
BlockService:: |
public | function | Returns all blocks. | |
BlockService:: |
public | function | Returns available blocks. | |
BlockService:: |
public | function | Returns blocks contents. | |
BlockService:: |
public | function | Sanitizes ID string. | |
BlockService:: |
public | function | Sanitizes ID string for legacy blocks. | |
BlockService:: |
public | function | Constructs a new BlockService object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |