Commit eeb368e4 authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-3908: Adding config form for blocks allowed on the dashboard.

parent 5f3cd8c3
<?php
namespace Drupal\uw_dashboard\Form;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UWDashboardBlockRestriction extends ConfigFormBase {
const SETTINGS = 'uw_dashboard.allowed_blocks';
/** @var \Drupal\Core\Block\BlockManagerInterface */
protected $blockManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.block'),
);
}
/**
* UWDashboardBlockRestriction constructor.
*
* @param \Drupal\Core\Block\BlockManagerInterface $pluginManagerblock
*/
public function __construct(BlockManagerInterface $pluginManagerblock) {
$this->blockManager = $pluginManagerblock;
}
/**
* {@inheritDoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* @inheritDoc
*/
public function getFormId() {
return 'uw_dashboard_block_restrictions';
}
/**
* Filter function to remove inline blocks.
*
* @param $value
* Value to check, block id.
*
* @return bool
* TRUE when id/value does not hold inline_block string, FALSE otherwise.
*/
protected function isInlineBlock($value) {
return strpos($value, 'inline_block:') === FALSE;
}
/**
* @inheritDoc
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$rows = [];
$config = $this->config(static::SETTINGS);
$definitions = array_filter($this->blockManager->getDefinitionsForContexts(), [$this, 'isInlineBlock'], ARRAY_FILTER_USE_KEY);
$grouped_definitions = $this->blockManager->getGroupedDefinitions($definitions);
foreach ($grouped_definitions as $group => $group_definitions) {
foreach ($group_definitions as $id => $definition) {
$rows[$id] = [
'label' => "{$definition['admin_label']} ({$group})",
];
}
}
$form['allowed_blocks'] = [
'#type' => 'tableselect',
'#header' => [
'label' => $this->t('Block label (Group)'),
],
'#sticky' => TRUE,
'#options' => $rows,
'#empty' => $this->t('No blocks found.'),
'#default_value' => $config->get('allowed_blocks'),
];
return parent::buildForm($form, $form_state);
}
/**
* @inheritDoc
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$blocks = $form_state->getValue('allowed_blocks');
$selected = array_filter($blocks);
$this->configFactory->getEditable(static::SETTINGS)
->set('allowed_blocks', $selected)
->save();
parent::submitForm($form, $form_state);
}
}
......@@ -78,17 +78,18 @@ 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 (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]);
// }
// }
// }
// }
if ($default->getPluginId() === 'dashboards') {
foreach ($definitions as $key => $single_definition) {
if (strpos($key, 'uw') === FALSE) {
unset($definitions[$key]);
}
}
}
}
return $definitions;
}
}
......@@ -11,3 +11,4 @@ dependencies:
- user
- views
- views_autocomplete_filters
- layout_builder_restrictions
uw_dashboard.uw_dashboard_allowed_blocks:
path: '/dashboard/allowed_blocks'
defaults:
_form: '\Drupal\uw_dashboard\Form\UWDashboardBlockRestriction'
_title: 'Blocks allowed on Dashboard'
requirements:
_permission: 'administer dashboards'
allowed_blocks: []
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