Skip to content

Commit

Permalink
Merge pull request #8 from koertho/main
Browse files Browse the repository at this point in the history
Fix exceptions when pageModel request attribute is of type int
  • Loading branch information
richardhj authored Feb 10, 2023
2 parents 8d3a422 + b4016be commit 8f6348c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null,
return $root;
}

$pageAdapter = $this->framework->getAdapter(PageModel::class);

$groups = [];
$request = $this->requestStack->getCurrentRequest();
$requestPage = $request->attributes->get('pageModel');

if (is_numeric($requestPage)) {
$requestPage = $pageAdapter->findByPk($requestPage);
}

/** @var FrontendUser $user */
$user = $this->framework->createInstance(FrontendUser::class);
if ($user->id) {
Expand All @@ -80,15 +86,17 @@ public function getMenu(ItemInterface $root, int $pid, $level = 1, $host = null,

// Check whether there will be subpages
if ($page->subpages > 0) {
$this->getMenu($item, (int) $page->id, $level++, $host, $options);

$level++;
$childRecords = Database::getInstance()->getChildRecords($page->id, 'tl_page');
if (!$options['showLevel']
|| $options['showLevel'] >= $level
|| (!$options['hardLimit']
&& ((null !== $requestPage && $requestPage->id === $page->id) || \in_array($requestPage->id, $childRecords, true)))) {
$item->setDisplayChildren(false);

$item->setDisplayChildren(false);
if (!$options['showLevel'] || $options['showLevel'] >= $level || (
!$options['hardLimit'] && ($requestPage->id == $page->id || \in_array($requestPage->id, $childRecords)))) {
$item->setDisplayChildren(true);
}

$this->getMenu($item, (int) $page->id, $level, $host, $options);
}

switch ($page->type) {
Expand Down
5 changes: 5 additions & 0 deletions src/Menu/NavigationModuleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function get($name, array $options = []): ItemInterface
{
$request = $this->requestStack->getCurrentRequest();
$moduleAdapter = $this->framework->getAdapter(ModuleModel::class);
$pageAdapter = $this->framework->getAdapter(PageModel::class);

/** @var ModuleModel $module */
if (null === $module = $moduleAdapter->findBy('menuAlias', $name)) {
Expand All @@ -50,6 +51,10 @@ public function get($name, array $options = []): ItemInterface

$currentPage = null !== $request ? $request->attributes->get('pageModel') : null;

if (is_numeric($currentPage)) {
$currentPage = $pageAdapter->findByPk($currentPage);
}

$menu = $this->factory->createItem('root');
$options = array_merge($module->row(), $options);

Expand Down

0 comments on commit 8f6348c

Please sign in to comment.