You are here

class DashboardController in Content Planner 8

Class DashboardController.

Hierarchy

Expanded class hierarchy of DashboardController

File

src/Controller/DashboardController.php, line 16

Namespace

Drupal\content_planner\Controller
View source
class DashboardController extends ControllerBase {

  /**
   * Drupal\Core\Database\Driver\mysql\Connection definition.
   *
   * @var \Drupal\Core\Database\Driver\mysql\Connection
   */
  protected $database;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The dashboard settings service.
   *
   * @var \Drupal\content_planner\DashboardSettingsService
   */
  protected $dashboardSettingsService;

  /**
   * The dashboard service.
   *
   * @var \Drupal\content_planner\DashboardService
   */
  protected $dashboardService;

  /**
   * The dashboard block plugin manager.
   *
   * @var \Drupal\content_planner\DashboardBlockPluginManager
   */
  protected $dashboardBlockPluginManager;

  /**
   * Constructs a new DashboardController object.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * Showdashboard.
   *
   * @return array
   *   The content planner dashboard render array.
   */
  public function showDashboard() {

    // Check if Content Calendar or Kanban is enabled.
    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 [];
    }

    // Get enabled blocks.
    $blocks = $this->dashboardSettingsService
      ->getBlockConfigurations();

    // If there are no blocks enabled, display error message.
    if (!$blocks) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Dashboard is not configured yet. Please do this in the Settings tab.'), 'error');
      return [];
    }

    // Get registered Plugins.
    $plugins = $this->dashboardBlockPluginManager
      ->getDefinitions();

    // Build blocks.
    $block_builds = $this
      ->buildBlocks($blocks, $plugins);
    $build = [
      '#theme' => 'content_planner_dashboard',
      '#blocks' => $block_builds,
      '#attached' => [
        'library' => [
          'content_planner/dashboard',
        ],
      ],
    ];
    return $build;
  }

  /**
   * Build blocks.
   *
   * @param array $blocks
   *   The blocks to render.
   * @param array $plugins
   *   The block plugins.
   *
   * @return array
   *   Array of content block render arrays.
   */
  protected function buildBlocks(array &$blocks, array &$plugins) {
    $block_builds = [];

    // Loop over every enabled block.
    foreach ($blocks as $block_id => $block) {

      // If a Dashboard Block plugin exists.
      if (array_key_exists($block_id, $plugins)) {

        /* @var $instance \Drupal\content_planner\DashboardBlockInterface */
        $instance = $this->dashboardBlockPluginManager
          ->createInstance($block_id, $block);
        if (\Drupal::currentUser()
          ->hasPermission('administer content planner dashboard settings')) {
          $has_permission = TRUE;
        }
        else {
          $has_permission = FALSE;
        }

        // Build block render array.
        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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DashboardController::$configFactory protected property The config factory. Overrides ControllerBase::$configFactory
DashboardController::$dashboardBlockPluginManager protected property The dashboard block plugin manager.
DashboardController::$dashboardService protected property The dashboard service.
DashboardController::$dashboardSettingsService protected property The dashboard settings service.
DashboardController::$database protected property Drupal\Core\Database\Driver\mysql\Connection definition.
DashboardController::buildBlocks protected function Build blocks.
DashboardController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
DashboardController::showDashboard public function Showdashboard.
DashboardController::__construct public function Constructs a new DashboardController object.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.