pm-4.1.x-dev/src/Controller/PmDashboardController.php
src/Controller/PmDashboardController.php
<?php
namespace Drupal\pm\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Dashboard Controller.
*/
class PmDashboardController extends ControllerBase {
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Drupal\Core\Extension\ModuleHandlerInterface definition.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Drupal\Core\Logger\LoggerChannelInterface definition.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $loggerChannelDefault;
/**
* Drupal\pm\PmDashboardItemsManager definition.
*
* @var \Drupal\pm\PmDashboardItemPluginManager
*/
protected $dashboardItems;
/**
* Drupal\Core\Path\PathValidator definition.
*
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->moduleHandler = $container->get('module_handler');
$instance->loggerChannelDefault = $container->get('logger.channel.default');
$instance->dashboardItems = $container->get('plugin.manager.pm_dashboard_item');
$instance->pathValidator = $container->get('path.validator');
return $instance;
}
/**
* Dashboard.
*
* @return array
* Return dashboard render array.
*/
public function dashboard() {
return [
'dashboard' => [
'#theme' => 'pm_dashboard',
'#items' => $this->getDashboardLinks(),
],
'#cache' => [
'max-age' => 0,
],
];
}
/**
* Get links for dashboard.
*/
protected function getDashboardLinks() {
$items = $this->dashboardItems->getDefinitions();
$output = [];
foreach ($items as $item) {
if ($this->pathValidator->isValid($item['link']['path'])) {
$item['link']['url'] = Url::fromUri('internal:' . $item['link']['path']);
$group_label = $item['group'] ?? '';
$output[$group_label]['label'] = $group_label;
$output[$group_label]['items'][] = $item;
}
}
ksort($output);
return $output;
}
}
