pkp-lib
pkp-lib copied to clipboard
Load a navigation menu defined at the site level
Describe the bug When loading a site-level navigation menu at the context level, the URLs for navigation menu items are not correct. The URLs are generated assuming they point to the same context as the request.
To Reproduce Steps to reproduce the behavior:
- Add the suggested code below to a theme and use it to assign the result to a template variable.
- Add a navigation menu at the site level (Administration > Site Settings > Navigation) with at least one custom page.
- View the resulting code on a journal and see that the custom page URL doesn't point to the site-level custom page.
What application are you using?
stable-3_3_0
Additional information The following code can be used in a theme to load a navigation menu defined at the site level. This is used, for example, to load a publisher or hosting service menu on every journal's site.
protected function getMenu(string $name, int $contextId = CONTEXT_ID_NONE, string $path = ''): string
{
/** @var NavigationMenuDAO $navigationMenuDao */
$navigationMenuDao = DAORegistry::getDAO('NavigationMenuDAO');
$navigationMenus = $navigationMenuDao->getByArea($contextId, $name)->toArray();
if (!isset($navigationMenus[0])) {
return '';
}
$navigationMenu = $navigationMenus[0];
Services::get('navigationMenu')->getMenuTree($navigationMenu);
$templateMgr = TemplateManager::getManager(Application::get()->getRequest());
$templateMgr->assign([
'navigationMenu' => $navigationMenu,
'id' => '',
'ulClass' => '',
'liClass' => '',
]);
if (!$path) {
$path = 'frontend/components/navigationMenu.tpl';
}
return $templateMgr->fetch($path);
}
Diff with suggestions to fix from @NateWr