View source
<?php
namespace Drupal\content_planner\Controller;
use Drupal\content_planner\DashboardBlockPluginManager;
use Drupal\content_planner\DashboardService;
use Drupal\content_planner\DashboardSettingsService;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Driver\mysql\Connection;
class DashboardController extends ControllerBase {
protected $database;
protected $configFactory;
protected $dashboardSettingsService;
protected $dashboardService;
protected $dashboardBlockPluginManager;
public function __construct(Connection $database, ConfigFactoryInterface $config_factory, DashboardSettingsService $dashboard_settings_service, DashboardService $dashboard_service, DashboardBlockPluginManager $dashboard_block_plugin_manager) {
$this->database = $database;
$this->configFactory = $config_factory;
$this->dashboardSettingsService = $dashboard_settings_service;
$this->dashboardService = $dashboard_service;
$this->dashboardBlockPluginManager = $dashboard_block_plugin_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('config.factory'), $container
->get('content_planner.dashboard_settings_service'), $container
->get('content_planner.dashboard_service'), $container
->get('content_planner.dashboard_block_plugin_manager'));
}
public function showDashboard() {
if (!$this->dashboardService
->isContentCalendarEnabled() && !$this->dashboardService
->isContentKanbanEnabled()) {
$this
->messenger()
->addMessage($this
->t('This dashboard can only be used with Content Calendar or Content Kanban enabled'), 'error');
return [];
}
$blocks = $this->dashboardSettingsService
->getBlockConfigurations();
if (!$blocks) {
$this
->messenger()
->addMessage($this
->t('Dashboard is not configured yet. Please do this in the Settings tab.'), 'error');
return [];
}
$plugins = $this->dashboardBlockPluginManager
->getDefinitions();
$block_builds = $this
->buildBlocks($blocks, $plugins);
$build = [
'#theme' => 'content_planner_dashboard',
'#blocks' => $block_builds,
'#attached' => [
'library' => [
'content_planner/dashboard',
],
],
];
return $build;
}
protected function buildBlocks(array &$blocks, array &$plugins) {
$block_builds = [];
foreach ($blocks as $block_id => $block) {
if (array_key_exists($block_id, $plugins)) {
$instance = $this->dashboardBlockPluginManager
->createInstance($block_id, $block);
if (\Drupal::currentUser()
->hasPermission('administer content planner dashboard settings')) {
$has_permission = TRUE;
}
else {
$has_permission = FALSE;
}
if ($block_build = $instance
->build()) {
$block_builds[$block_id] = [
'#theme' => 'content_planner_dashboard_block',
'#css_id' => str_replace('_', '-', $block_id),
'#block_id' => $block_id,
'#name' => isset($block['title']) && $block['title'] ? $block['title'] : $instance
->getName(),
'#has_permission' => $has_permission,
'#block' => $block_build,
'#weight' => $block['weight'],
];
}
}
}
return $block_builds;
}
}