DashboardService

mail@pastecode.io avatar
unknown
php
2 years ago
1.5 kB
2
Indexable
Never
<?php
namespace Drupal\mint_intranet_dashboard\Service;

use Symfony\Component\Yaml\Yaml;

class DashboardService {
  // Get all blocks from installed modules.
  public function getBlocks() {
    $blocks = [];
    $modules = [
      'mint_intranet_communication',
    ];
    $installedModules = [];

    $moduleHandler = \Drupal::service('module_handler');

    foreach ($modules as $module) {
      if ($moduleHandler->moduleExists($module)) {
        $installedModules[] = $module;
      }
    }

    $blocks = $this->getBlockConfig($installedModules);
    return $this->getBlockDisplay($blocks);
  }

  public function getBlockConfig($installedModules) {
    $blocks = [];

    $moduleHandler = \Drupal::service('module_handler');

    foreach ($installedModules as $module) {
      $filePath = $modulePath = $moduleHandler->getModule($module)->getPath() . '/mint_parameters.yml';
      $fileContent = Yaml::parse(file_get_contents($filePath));
      if ($fileContent['dashboard'] != null) {
        foreach($fileContent['dashboard'] as $block) {
          $blocks[] = $block;
        }
      }
    }

    return $blocks;
  }

  public function getBlockDisplay($blocks) {
    $render = [];
    $rendered = [];
    $block_manager = \Drupal::service('plugin.manager.block');
    $config = [];

    for ($i = 0; $i < count($blocks); $i++) {
      $plugin_block = $block_manager->createInstance($blocks[$i]['id'], $config);
      $render[] = $plugin_block->build();
      $rendered[] = render($render[$i]);
    }
    return $rendered;
  }
}