Issue #3236712 by evamtinez, m.stenta: Allow render custom blocks in the dashboard

This commit is contained in:
Michael Stenta 2021-09-22 15:18:49 -04:00
commit 9e7598955c
9 changed files with 41 additions and 78 deletions

View File

@ -32,7 +32,7 @@ function hook_farm_dashboard_panes() {
'view' => 'my_view',
'view_display_id' => 'block_1',
// Specify a 'block' to automatically load a Block.
// Specify a block plugin ID to automatically load a Block.
'block' => 'my_block',
// Optional arguments to pass to the view or block.

View File

@ -2,8 +2,10 @@
namespace Drupal\farm_ui_dashboard\Controller;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -21,18 +23,38 @@ class DashboardController extends ControllerBase {
*/
protected $layoutPluginManager;
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Class constructor.
*/
public function __construct(LayoutPluginManagerInterface $layout_plugin_manager) {
public function __construct(LayoutPluginManagerInterface $layout_plugin_manager, BlockManagerInterface $block_manager, AccountInterface $current_user) {
$this->layoutPluginManager = $layout_plugin_manager;
$this->blockManager = $block_manager;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('plugin.manager.core.layout'));
return new static(
$container->get('plugin.manager.core.layout'),
$container->get('plugin.manager.block'),
$container->get('current_user'),
);
}
/**
@ -103,25 +125,18 @@ class DashboardController extends ControllerBase {
// Or if a block is provided, display the block.
elseif (!empty($pane['block'])) {
/** @var \Drupal\block\Entity\Block $block */
$block = $this->entityTypeManager()->getStorage('block')->load($pane['block']);
// Render plugin block if is set.
$block = $this->blockManager->createInstance($pane['block'], $args);
if ($block) {
// Set the block plugin config if provided.
if (!empty($args)) {
$block->getPlugin()->setConfiguration($args);
// Check block access.
$access_result = $block->access($this->currentUser);
if ($access_result == TRUE) {
// Builds renderable array of the block.
$output = $block->build();
}
}
// If the block plugin displays the label by default, set the title.
$block_config = $block->getPlugin()->getConfiguration();
if ($block_config['label_display']) {
$title = $block->label();
}
// Use the block's weight by default.
$weight = $block->getWeight();
// Build the blocks renderable output.
$output = $this->entityTypeManager()->getViewBuilder('block')->view($block);
}
// If a specific title was provided, use it.

View File

@ -1,12 +0,0 @@
langcode: en
status: true
id: dashboard_test
theme: claro
weight: 1000
provider: null
plugin: dashboard_test_block
settings:
label: 'Dashboard test block label'
provider: farm_ui_dashboard_test
label_display: '1'
visibility: { }

View File

@ -11,7 +11,7 @@
function farm_ui_dashboard_test_farm_dashboard_panes() {
return [
'dashboard_block' => [
'block' => 'dashboard_test',
'block' => 'dashboard_test_block',
],
'dashboard_view' => [
'view' => 'dashboard_test_view',

View File

@ -45,7 +45,6 @@ class DashboardTest extends FarmBrowserTestBase {
$this->assertSession()->statusCodeEquals(200);
// Assert that the test block was added.
$this->assertSession()->pageTextContains('Dashboard test block label');
$this->assertSession()->pageTextContains('This is the dashboard test block.');
}

View File

@ -1,23 +0,0 @@
langcode: en
status: false
dependencies:
enforced:
module:
- farm_ui_dashboard
module:
- farm_map
theme:
- claro
id: dashboard_map
theme: claro
region: content
weight: -100
provider: null
plugin: map_block
settings:
id: dashboard_map
map_type: dashboard
label: 'Dashboard map'
provider: farm_ui_dashboard
label_display: '0'
visibility: { }

View File

@ -15,7 +15,10 @@ use Drupal\Core\Link;
function farm_ui_map_farm_dashboard_panes() {
return [
'dashboard_map' => [
'block' => 'dashboard_map',
'block' => 'map_block',
'args' => [
'map_type' => 'dashboard',
],
'region' => 'top',
],
];

View File

@ -1,19 +0,0 @@
langcode: en
status: false
dependencies:
module:
- farm_ui_metrics
theme:
- claro
id: farm_metrics
theme: claro
region: content
weight: 0
provider: null
plugin: farm_metrics_block
settings:
id: farm_metrics_block
label: 'Metrics'
provider: farm_ui_metrics
label_display: '0'
visibility: { }

View File

@ -11,7 +11,7 @@
function farm_ui_metrics_farm_dashboard_panes() {
return [
'metrics' => [
'block' => 'farm_metrics',
'block' => 'farm_metrics_block',
'title' => t('Metrics'),
'region' => 'second',
],