From 0155f35c9676a4905dbe82494fc3d1d16c183ee2 Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Sat, 27 Jan 2018 17:27:15 +0100 Subject: [PATCH] Do not add unnecessary parameters in the archive link (#19447) * Do not add unnecessary parameters in the archive link * Remove php notice * Unset parameter month=0 when year is not set --- .../com_content/helpers/legacyrouter.php | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/components/com_content/helpers/legacyrouter.php b/components/com_content/helpers/legacyrouter.php index 4529c8c942399..3bf72bdc1dd47 100644 --- a/components/com_content/helpers/legacyrouter.php +++ b/components/com_content/helpers/legacyrouter.php @@ -225,25 +225,34 @@ public function build(&$query, &$segments) if (!$menuItemGiven) { $segments[] = $view; - unset($query['view']); } - if (isset($query['year'])) + unset($query['view']); + + // If there is no year segment then do not add month segment + if (isset($query['year']) && $menuItemGiven) { - if ($menuItemGiven) + if ($query['year']) { $segments[] = $query['year']; - unset($query['year']); + + if (isset($query['month'])) + { + if ($query['month']) + { + $segments[] = $query['month']; + } + + unset($query['month']); + } } + + unset($query['year']); } - if (isset($query['month'])) + if (isset($query['month']) && empty($query['month'])) { - if ($menuItemGiven) - { - $segments[] = $query['month']; - unset($query['month']); - } + unset($query['month']); } } @@ -320,7 +329,7 @@ public function parse(&$segments, &$vars) * Standard routing for articles. If we don't pick up an Itemid then we get the view from the segments * the first segment is the view and the last segment is the id of the article or category. */ - if (!isset($item)) + if ($item === null) { $vars['view'] = $segments[0]; $vars['id'] = $segments[$count - 1]; @@ -328,6 +337,25 @@ public function parse(&$segments, &$vars) return; } + // Manage the archive view + if ($item->query['view'] === 'archive') + { + $vars['view'] = 'archive'; + + if ($count >= 2) + { + $vars['year'] = $segments[$count - 2]; + $vars['month'] = $segments[$count - 1]; + } + else + { + $vars['year'] = $segments[$count - 1]; + $vars['month'] = null; + } + + return; + } + /* * If there is only one segment, then it points to either an article or a category. * We test it first to see if it is a category. If the id and alias match a category, @@ -384,7 +412,7 @@ public function parse(&$segments, &$vars) * because the first segment will have the target category id prepended to it. If the * last segment has a number prepended, it is an article, otherwise, it is a category. */ - if ((!$advanced) && ($item->query['view'] !== 'archive')) + if ((!$advanced)) { $cat_id = (int) $segments[0]; @@ -405,16 +433,6 @@ public function parse(&$segments, &$vars) return; } - // Manage the archive view - if ($item->query['view'] == 'archive' && $count !== 1) - { - $vars['year'] = $count >= 2 ? $segments[$count - 2] : null; - $vars['month'] = $segments[$count - 1]; - $vars['view'] = 'archive'; - - return; - } - // We get the category id from the menu item and search from there $id = $item->query['id']; $category = JCategories::getInstance('Content')->get($id);