diff --git a/libraries/src/Component/Router/Rules/MenuRules.php b/libraries/src/Component/Router/Rules/MenuRules.php index e1e7d37ffd05c..34bd9b1e58306 100644 --- a/libraries/src/Component/Router/Rules/MenuRules.php +++ b/libraries/src/Component/Router/Rules/MenuRules.php @@ -110,33 +110,42 @@ public function preprocess(&$query) $needles = $this->router->getPath($query); - $layout = ''; - - if (isset($query['layout'])) - { - $layout = ':' . $query['layout']; - } + $layout = isset($query['layout']) && $query['layout'] !== 'default' ? ':' . $query['layout'] : ''; if ($needles) { foreach ($needles as $view => $ids) { - if (isset($this->lookup[$language][$view . $layout])) + $viewLayout = $view . $layout; + + if ($layout && isset($this->lookup[$language][$viewLayout])) { if (is_bool($ids)) { - $query['Itemid'] = $this->lookup[$language][$view . $layout]; + $query['Itemid'] = $this->lookup[$language][$viewLayout]; return; } foreach ($ids as $id => $segment) { - if (isset($this->lookup[$language][$view . $layout][(int) $id])) + if (isset($this->lookup[$language][$viewLayout][(int) $id])) { - $query['Itemid'] = $this->lookup[$language][$view . $layout][(int) $id]; + $query['Itemid'] = $this->lookup[$language][$viewLayout][(int) $id]; return; } + } + } + + if (isset($this->lookup[$language][$view])) + { + if (is_bool($ids)) + { + $query['Itemid'] = $this->lookup[$language][$view]; + return; + } + foreach ($ids as $id => $segment) + { if (isset($this->lookup[$language][$view][(int) $id])) { $query['Itemid'] = $this->lookup[$language][$view][(int) $id]; diff --git a/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php b/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php index d50e5b60beac6..fb46a601f2cb6 100644 --- a/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php +++ b/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php @@ -265,6 +265,37 @@ public function testPreprocessActive() $this->restoreFactoryState(); } + /** + * Tests the preprocess() method + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testPreprocessLayout() + { + $this->saveFactoryState(); + + $router = $this->object->get('router'); + + // Unset an active menu + $router->menu->active = null; + + // Check link if default layout is set explicitly + $query = array('option' => 'com_content', 'view' => 'category', 'id' => '22', 'layout' => 'default'); + $expect = array('option' => 'com_content', 'view' => 'category', 'id' => '22', 'Itemid' => '49', 'layout' => 'default'); + $this->object->preprocess($query); + $this->assertEquals($expect, $query); + + // Check link if the layout is different than in menu item for parent category + $query = array('option' => 'com_content', 'view' => 'category', 'id' => '22', 'layout' => 'blog'); + $expect = array('option' => 'com_content', 'view' => 'category', 'id' => '22', 'Itemid' => '49', 'layout' => 'blog'); + $this->object->preprocess($query); + $this->assertEquals($expect, $query); + + $this->restoreFactoryState(); + } + /** * Tests the buildLookup() method *