Skip to content

Commit

Permalink
Issue #3236712 by evamtinez, m.stenta: Allow render custom blocks in …
Browse files Browse the repository at this point in the history
…the dashboard
  • Loading branch information
mstenta committed Sep 22, 2021
2 parents 2170fd5 + 7092a08 commit 9e75989
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 77 deletions.
2 changes: 1 addition & 1 deletion modules/core/ui/dashboard/farm_ui_dashboard.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 33 additions & 18 deletions modules/core/ui/dashboard/src/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'),
);
}

/**
Expand Down Expand Up @@ -103,25 +125,18 @@ public function dashboard() {
// 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) {

// 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();
// Builds renderable array of the block.
$output = $block->build();
}
}

// 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.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function testDashboardBlock() {
$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.');
}

Expand Down
23 changes: 0 additions & 23 deletions modules/core/ui/map/config/install/block.block.dashboard_map.yml

This file was deleted.

5 changes: 4 additions & 1 deletion modules/core/ui/map/farm_ui_map.module
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion modules/core/ui/metrics/farm_ui_metrics.module
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down

0 comments on commit 9e75989

Please sign in to comment.