og_sm-8.x-1.0/og_sm_admin_menu/og_sm_admin_menu.module
og_sm_admin_menu/og_sm_admin_menu.module
<?php
/**
* @file
* Site Manager Administration module.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\og_sm\OgSm;
use Drupal\og_sm_admin_menu\Render\Element\SiteManagerAdminToolbar;
/**
* Implements hook_local_tasks_alter().
*/
function og_sm_admin_menu_local_tasks_alter(&$local_tasks) {
// We are taking over the admin route and don't want it to be a local task for
// site nodes.
unset($local_tasks['og.og_admin_routes:node.og_admin_routes']);
}
/**
* Implements hook_toolbar().
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
function og_sm_admin_menu_toolbar() {
$site_manager = OgSm::siteManager();
$links = [];
$main_tab = NULL;
$platform_url = Url::fromRoute('<front>');
$platform_access = FALSE;
// Check if we are allowed to the platform, if so set the platform URL as our
// main tab.
if (\Drupal::pathValidator()->isValid($platform_url->getInternalPath())) {
$platform_access = TRUE;
$main_tab = [
'title' => t('Platform'),
'url' => $platform_url,
];
}
$current_site = $site_manager->currentSite();
$tab_cache_tags = [];
// If we are currently within site context, the main tab should be the site,
// if the main tab was previously set it should be added to the links array.
if ($current_site) {
if ($main_tab) {
$links['platform'] = $main_tab;
}
$main_tab = [
'title' => $current_site->label(),
'url' => $site_manager->getSiteHomePage($current_site),
];
$tab_cache_tags[] = 'node:' . $current_site->id();
}
// If we don't have access to the platform, and we are outside of site context
// don't show the site switcher.
if (!$main_tab) {
return [];
}
$switch_sites = $site_manager->getUserManageableSites();
if (!$platform_access && count($switch_sites) < 2) {
return [];
}
$cache_tags = [];
foreach ($switch_sites as $site) {
if ($current_site && $current_site->id() === $site->id()) {
continue;
}
$links['site-' . $site->id()] = [
'title' => $site->label(),
'url' => $site_manager->getSiteHomePage($site),
];
$cache_tags[] = 'node:' . $site->id();
}
$items = [];
$items['site_switcher'] = [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#title' => $main_tab['title'],
'#url' => $main_tab['url'],
'#attributes' => [
'title' => t('Return to site content'),
'class' => ['toolbar-site-switcher'],
],
'#cache' => [
'contexts' => [
'og_group_context',
'user',
],
'tags' => $tab_cache_tags,
],
],
'tray' => [
'#heading' => t('Site switcher'),
'site_switcher' => [
'#theme' => 'links__toolbar',
'#links' => $links,
'#attributes' => [
'class' => ['toolbar-menu'],
],
'#cache' => [
'tags' => $cache_tags,
],
],
],
'#attached' => [
'library' => [
'og_sm_admin_menu/og_sm.site_switcher',
],
],
'#weight' => 200,
];
return $items;
}
/**
* Implements hook_toolbar_alter().
*/
function og_sm_admin_menu_toolbar_alter(&$items) {
$items['administration']['tray']['toolbar_administration']['#pre_render'] = [
[SiteManagerAdminToolbar::class, 'preRenderTray'],
];
$items['administration']['#cache']['contexts'][] = 'og_group_context';
}
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function og_sm_admin_menu_preprocess_html(&$variables) {
// Make sure the root path is set to "admin" for group admin pages. This adds
// a "page-admin" class to the body tags which caused the "Back to home"
// to appear on admin pages.
if ($variables['root_path'] === 'group' && \Drupal::routeMatch()->getRouteObject()->getOption('_admin_route')) {
$variables['root_path'] = 'admin';
}
// Alter the toolbar's #access key to also include the og permission when in
// site context.
$toolbar_access = og_sm_site_user_access('access toolbar');
if ($toolbar_access) {
$variables['page_top']['toolbar']['#access'] = $toolbar_access;
$variables['page_top']['toolbar']['#cache']['contexts'][] = 'og_group_context';
$variables['attributes'] = new Attribute($variables['attributes']);
$variables['attributes']->addClass([
'toolbar-tray-open',
'toolbar-horizontal',
'toolbar-fixed',
'toolbar-loading',
]);
}
}
