Refactor dashboard block handling to always expect block plugin ID.

This commit is contained in:
Michael Stenta 2021-09-22 15:12:07 -04:00
parent 3ea20c1d63
commit 7092a0830f
9 changed files with 13 additions and 89 deletions

View File

@ -32,12 +32,9 @@ 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',
// Specify a block plugin id to load custom Blocks.
'block_plugin' => 'my_block_plugin_id',
// Optional arguments to pass to the view or block.
// This is useful if the view or block can be used in different contexts.
'args' => [

View File

@ -125,37 +125,16 @@ 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']);
// Set the block plugin config if provided.
if (!empty($args)) {
$block->getPlugin()->setConfiguration($args);
}
// 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);
}
// Or if a plugin block id is provided, display the block.
elseif (!empty($pane['block_plugin'])) {
// Render plugin block if is set.
$block_plugin = $this->blockManager->createInstance($pane['block_plugin'], $args);
if ($block_plugin) {
$block = $this->blockManager->createInstance($pane['block'], $args);
if ($block) {
// Check block access.
$access_result = $block_plugin->access($this->currentUser);
$access_result = $block->access($this->currentUser);
if ($access_result == TRUE) {
// Builds renderable array of the block.
$output = $block_plugin->build();
$output = $block->build();
}
}
}

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',
],