Commit 6d2de0b9 authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-3908: Adding link to dashboard tray and renaming classes.

parent eeb368e4
......@@ -9,7 +9,7 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UWDashboardBlockRestriction extends ConfigFormBase {
class AllowedBlocksForm extends ConfigFormBase {
const SETTINGS = 'uw_dashboard.allowed_blocks';
/** @var \Drupal\Core\Block\BlockManagerInterface */
......
......@@ -4,9 +4,7 @@
namespace Drupal\uw_dashboard\Plugin\LayoutBuilderRestriction;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestrictionBase;
use Drupal\layout_builder_restrictions\Traits\PluginHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -20,23 +18,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* description = @Translation("Restrict blocks/layouts for UW Dashboard"),
* )
*/
class UWDashboardBlockRestriction extends LayoutBuilderRestrictionBase {
class AllowedBlocks extends LayoutBuilderRestrictionBase {
use PluginHelperTrait;
/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Database connection service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/** @var ConfigFactoryInterface */
protected $configFactory;
/**
* @inheritDoc
......@@ -46,8 +33,7 @@ class UWDashboardBlockRestriction extends LayoutBuilderRestrictionBase {
$configuration,
$plugin_id,
$plugin_definition,
$container->get('module_handler'),
$container->get('database')
$container->get('config.factory')
);
}
......@@ -60,17 +46,14 @@ class UWDashboardBlockRestriction extends LayoutBuilderRestrictionBase {
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory to load blocks allowed to be displayed on dashboard.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Connection $connection) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $configFactory) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->moduleHandler = $module_handler;
$this->database = $connection;
$this->configFactory = $configFactory;
}
/**
......@@ -78,18 +61,21 @@ class UWDashboardBlockRestriction extends LayoutBuilderRestrictionBase {
*/
public function alterBlockDefinitions(array $definitions, array $context) {
// Respect restrictions on allowed blocks specified by the section storage.
// if (isset($context['section_storage'])) {
// $default = $context['section_storage'];
//
// if ($default->getPluginId() === 'dashboards') {
// foreach ($definitions as $key => $single_definition) {
// if (strpos($key, 'uw') === FALSE) {
// unset($definitions[$key]);
// }
// }
// }
// }
$allowed_blocks = $this->configFactory->get('uw_dashboard.allowed_blocks')->get('allowed_blocks');
$allowed_block_definitions = [];
if (isset($context['section_storage'])) {
$default = $context['section_storage'];
if ($allowed_blocks && $default->getPluginId() === 'dashboards') {
foreach ($allowed_blocks as $key => $allowed_block) {
if (isset($definitions[$key])) {
$allowed_block_definitions[$key] = $definitions[$key];
}
}
}
}
return $definitions;
return $allowed_block_definitions ?: $definitions;
}
}
......@@ -5,6 +5,8 @@
* UW Dashboard module file.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\views\ViewExecutable;
/**
......@@ -55,4 +57,14 @@ function uw_dashboard_preprocess_views_view_field(&$variables) {
function uw_dashboard_toolbar_alter(&$items) {
// Move the 'Dashboards' link at the left of the tooltar.
$items['dashboards']['#weight'] = -15;
}
\ No newline at end of file
// Adding config form for user with permission to update it. This prevents
// menu item to be displayed and getting access denied by clicking on it.
if (\Drupal::currentUser()->hasPermission('administer dashboards')) {
$items['dashboards']['tray']['dashboards']['#items'][] = [
'#type' => 'link',
'#title' => 'Allowed blocks',
'#url' => Url::fromRoute('uw_dashboard.allowed_blocks'),
];
}
}
uw_dashboard.uw_dashboard_allowed_blocks:
uw_dashboard.allowed_blocks:
path: '/dashboard/allowed_blocks'
defaults:
_form: '\Drupal\uw_dashboard\Form\UWDashboardBlockRestriction'
_title: 'Blocks allowed on Dashboard'
_form: '\Drupal\uw_dashboard\Form\AllowedBlocksForm'
_title: 'Blocks allowed on the Dashboard'
requirements:
_permission: 'administer dashboards'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment