Skip to content

Commit 77af51f

Browse files
csthomasrdeutz
authored andcommitted
Routing: Remove IDs from tags URLs, use menu item of tags view as default for tag view (#11166)
* Remove id from tags, use tags list menu item as default for tag * Code style, remove useless code, feature: first Itemid for tags view, second Itemid for default tag view
1 parent 326036f commit 77af51f

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

components/com_tags/helpers/route.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ public static function getTagRoute($id)
9494
{
9595
$link .= '&Itemid=' . $item;
9696
}
97+
else
98+
{
99+
$needles = array('tags' => array(1, 0));
100+
101+
if ($item = self::_findItem($needles))
102+
{
103+
$link .= '&Itemid=' . $item;
104+
}
105+
}
106+
}
107+
108+
return $link;
109+
}
110+
111+
/**
112+
* Tries to load the router for the tags view.
113+
*
114+
* @return string URL link to pass to JRoute
115+
*
116+
* @since 3.7
117+
*/
118+
public static function getTagsRoute()
119+
{
120+
$needles = array(
121+
'tags' => array(0)
122+
);
123+
124+
$link = 'index.php?option=com_tags&view=tags';
125+
126+
if ($item = self::_findItem($needles))
127+
{
128+
$link .= '&Itemid=' . $item;
97129
}
98130

99131
return $link;
@@ -153,6 +185,10 @@ protected static function _findItem($needles = null)
153185
}
154186
}
155187
}
188+
elseif ($view == 'tags')
189+
{
190+
self::$lookup[$lang]['tags'][] = $item->id;
191+
}
156192
}
157193
}
158194
}

components/com_tags/router.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ public function build(&$query)
103103
for ($i = 0; $i < $total; $i++)
104104
{
105105
$segments[$i] = str_replace(':', '-', $segments[$i]);
106+
$position = strpos($segments[$i], '-');
107+
108+
if ($position)
109+
{
110+
// Remove id from segment
111+
$segments[$i] = substr($segments[$i], $position + 1);
112+
}
106113
}
107114

108115
return $segments;
@@ -137,19 +144,47 @@ public function parse(&$segments)
137144
if (!isset($item))
138145
{
139146
$vars['view'] = $segments[0];
140-
$vars['id'] = $segments[$count - 1];
147+
$vars['id'] = $this->fixSegment($segments[$count - 1]);
141148

142149
return $vars;
143150
}
144151

145-
// From the tags view, we can only jump to a tag.
146-
$id = (isset($item->query['id']) && $item->query['id'] > 1) ? $item->query['id'] : 'root';
147-
148-
$vars['id'] = $segments[0];
152+
$vars['id'] = $this->fixSegment($segments[0]);
149153
$vars['view'] = 'tag';
150154

151155
return $vars;
152156
}
157+
158+
/**
159+
* Try to add missing id to segment
160+
*
161+
* @param string $segment One piece of segment of the URL to parse
162+
*
163+
* @return string The segment with founded id
164+
*
165+
* @since 3.7
166+
*/
167+
protected function fixSegment($segment)
168+
{
169+
$db = JFactory::getDbo();
170+
171+
// Try to find tag id
172+
$alias = str_replace(':', '-', $segment);
173+
174+
$query = $db->getQuery(true)
175+
->select('id')
176+
->from($db->quoteName('#__tags'))
177+
->where($db->quoteName('alias') . " = " . $db->quote($alias));
178+
179+
$id = $db->setQuery($query)->loadResult();
180+
181+
if ($id)
182+
{
183+
$segment = "$id:$alias";
184+
}
185+
186+
return $segment;
187+
}
153188
}
154189

155190
/**

0 commit comments

Comments
 (0)