You are here

function opigno_dashboard_preprocess_region in Opigno dashboard 8

Implements hook_preprocess_region().

File

./opigno_dashboard.module, line 69
Contains opigno_dashboard.module.

Code

function opigno_dashboard_preprocess_region(&$variables) {
  $request = \Drupal::request();
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if (in_array($route_name, [
    'view.frontpage.page_1',
    'opigno_dashboard.dashboard_admin_default_settings',
  ]) && $variables['region'] == 'content') {
    $variables['base_path'] = \Drupal::request()
      ->getBasePath();
    $variables['base_href'] = $request
      ->getPathInfo();
    $variables['route_name'] = $route_name;

    // Get default config for dashboard blocks.
    if ($route_name == 'opigno_dashboard.dashboard_admin_default_settings') {
      $variables['default_config'] = '{}';
    }
    else {
      $dashboard = new DashboardController();
      $default_dashboard = $dashboard
        ->getUserDefaultPositioning();
      $positions = json_encode($default_dashboard['positions']);
      $columns = $default_dashboard['columns'];
      $variables['default_config'] = !empty($positions) ? $positions : OPIGNO_DASHBOARD_DEFAULT_CONFIG;
    }
    $variables['default_columns'] = !empty($columns) ? $columns : 3;

    /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
    $manager = \Drupal::service('plugin.manager.menu.local_task');

    /** @var \Drupal\Core\Cache\CacheableMetadata $cacheability */
    $cacheability = new CacheableMetadata();
    $cacheability
      ->addCacheableDependency($manager);
    $tabs = [
      '#theme' => 'menu_local_tasks',
    ];
    $primary = $manager
      ->getLocalTasks(\Drupal::routeMatch()
      ->getRouteName(), 0);
    $cacheability = $cacheability
      ->merge($primary['cacheability']);
    $tabs += [
      '#primary' => count(Element::getVisibleChildren($primary['tabs'])) > 1 ? $primary['tabs'] : [],
    ];
    $secondary = $manager
      ->getLocalTasks(\Drupal::routeMatch()
      ->getRouteName(), 1);
    $cacheability = $cacheability
      ->merge($secondary['cacheability']);
    $tabs += [
      '#secondary' => count(Element::getVisibleChildren($secondary['tabs'])) > 1 ? $secondary['tabs'] : [],
    ];
    $variables['tabs'] = $tabs;
  }
}