You are here

PmDashboardController.php in Drupal PM (Project Management) 4.x

File

src/Controller/PmDashboardController.php
View source
<?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\PmDashboardItemsManager
   */
  protected $dashboardItemsManager;

  /**
   * 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->dashboardItemsManager = $container
      ->get('plugin.manager.pm_dashboard_items');
    $instance->pathValidator = $container
      ->get('path.validator');
    return $instance;
  }

  /**
   * Dashboard.
   *
   * @return string
   *   Return Hello string.
   */
  public function dashboard() {
    return [
      'dashboard' => [
        '#theme' => 'pm_dashboard',
        '#items' => $this
          ->getDashboardLinks(),
      ],
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }

  /**
   * Get links for dashboard.
   */
  protected function getDashboardLinks() {
    $items = $this->dashboardItemsManager
      ->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;
  }

}

Classes

Namesort descending Description
PmDashboardController Dashboard Controller.