So far I'm re-inventing the wheel here in the most uncomfortable way. I can feel in my gut that this will break one day and cause me a lot of pain. Therefore I'm looking for a better way to take an article alias and build either the menu item url or the article url. Are there no joomla api calls that make this easier/cleaner/more future-proof?
/* Find article by alias */
$db =& JFactory::getDBO();
$sql = 'select id from #__content where alias=' . "'$alias'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$artId = $row['id'];
if ($artId != null) {
$artLink = 'index.php?option=com_content&view=article&id='.$artId;
/* Find menu item by article id */
$sql = 'select parent,alias from #__menu where link=' . "'$artLink'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$menuLink = '';
while ($row != null) {
$menuLink = '/' . $row['alias'] . $menuLink;
$sql = 'select parent,alias from #__menu where id=' . $row['parent'];
$db->setQuery($sql);
$row = $db->loadAssoc();
}
$menuLink = 'index.php' . $menuLink;
}
$articleUrl = ($menuLink != '') ? 'index.php' . $menuLink : JRoute::_($artLink);
Use JRoute? Assuming you're still starting with an alias, a more 'Joomla' way do this could be something like:
Docs for JRoute here: http://api.joomla.org/Joomla-Framework/JRoute.html
This also gets around SQL escaping issues that you could get if there were quotes in your alias ;o
I should also mention that if you want the menu link specifically - you need to have the '&itemid=' bit on the end of the path passed to JRoute! Of course, you can't get that itemid from the alias - there can be multiple menu items pointed to the same article ;).