How to use alias instead of article id in link? Topic is solved

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
CDXXonline
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 201
Joined: Fri Oct 13, 2017 8:00 pm

How to use alias instead of article id in link?

Post by CDXXonline » Sun Oct 20, 2019 12:34 pm

I use this code to get article link in a history

Code: Select all

<?php $board = $item['object_id']; ?>
<?php $article = JTable::getInstance("content"); ?>
<?php $article->load($board); ?> 

<a href="?option=com_content&view=article&id=<?php echo $board; ?>">
<?php echo $article->get("title"); ?>
</a>
It gives me a link like

site/?option=com_content&view=article&id=40

Is there a way to get the alias link instead of the id number link? $board is used to get the article id in a foreach. Looking for a link more like

site/menu-alias/menu-alias/article-alias


Sorry about that toivo, realized after i posted it should be on the coding board
Last edited by toivo on Sun Oct 20, 2019 12:42 pm, edited 1 time in total.
Reason: mod note: moved from 3.x General Questions

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22316
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: How to use alias instead of article id in link?

Post by pe7er » Sun Oct 20, 2019 12:59 pm

You can use the Route function:
https://docs.joomla.org/J3.x:Supporting ... _component

Code: Select all

JRoute::_('index.php?option=com_content&view=article&id=' . $item->id);
or since Joomla 3.8, you can use Namespacing:

Code: Select all

use Joomla\CMS\Router\Route;

Route::_('index.php?option=com_content&view=article&id=' . $item->id);
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
Co-developer of d2 Content https://data2site.com/joomla-extensions/d2-content

CDXXonline
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 201
Joined: Fri Oct 13, 2017 8:00 pm

Re: How to use alias instead of article id in link?

Post by CDXXonline » Sun Oct 20, 2019 9:45 pm

Thanks pe7er. I looked at that stuff for awhile and played around with it, but it was too confusing for me, maybe someone else can figure it out. Seemed more complicated than just using queries and building the link, it seems to work well so far that way. Here's the code if anyone is interested. The previous query was for jcomments info and the article id (not included).

Code: Select all

<?php 

$board = $item['object_id'];
$article = JTable::getInstance("content");
$article->load($board); 

   $db = JFactory::getDbo();
   $db->setQuery('SELECT catid 
                  FROM #__content
                  WHERE id = ' . $board . '');
   $cat = $db->loadResult(); 

   $db = JFactory::getDbo();
   $db->setQuery('SELECT path 
                   FROM #__menu
                   WHERE link LIKE "%?option=com_content&view=category&id=' . $cat . '%"');
   $path = $db->loadResult(); 
   $alias = $article->get("alias"); 

if ($path > null) { ?>

           <a href="<?php echo $path; ?>/<?php echo $board; ?>-<?php echo $alias; ?>">
                   <?php echo $article->get("title"); ?>
           </a>

<?php } else { 

   $db = JFactory::getDbo();
   $db->setQuery('SELECT path 
                  FROM #__menu
                  WHERE link LIKE "%?option=com_content&view=article&id=' . $board . '%"');
   $path = $db->loadResult(); ?>

           <a href="<?php echo $path; ?>">
                   <?php echo $article->get("title"); ?>
           </a>           

<?php } ?>

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22316
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: How to use alias instead of article id in link?

Post by pe7er » Mon Oct 21, 2019 6:03 am

Using the Route function in a proper way is more efficient regarding speed than doing extra database queries.

If you would solve it with extra database queries, then I would advice you to improve your code against SQL injection:
make sure that $board is not a string with SQL code that does unwanted things.

If you are using J3.8 or higher with PHP 7.x then I would also use best practices like name spacing.
Here's how I would rewrite the first part of your code.

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;

// Cast to integer!
$board = (int) $item['object_id'];
$article = Table::getInstance("content");
$article->load($board); 

   $db = Factory::getDbo();
   $query = $db->getQuery(true)
	->select(
		$db->quoteName('catid')
	)
	->from($db->quoteName('#__content'))
	->where($db->quoteName('id') . ' = ' . $db->quote($board));
   $cat = $db->loadResult(); 
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
Co-developer of d2 Content https://data2site.com/joomla-extensions/d2-content

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1636
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: How to use alias instead of article id in link?

Post by SharkyKZ » Mon Oct 21, 2019 7:17 am

You don't need to run custom queries to get article data as you're already using JTable to load the entire row. $article holds all article properties, e.g. $article->catid for category ID. Although, if you don't need other data like article text, you could reverse this: omit the use JTable and run database queries manually. This should be faster as you could load only the needed columns.

To get correct links you can use ContentHelperRoute.

Code: Select all

<?php

// Register helper class.
JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

// Build article link.
$link = ContentHelperRoute::getArticleRoute($article->id . ':' . $article->alias, $article->catid, $article->language);

?>

<a href="<?php echo JRoute::_($link); ?>"><?php echo $article->title; ?></a>

CDXXonline
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 201
Joined: Fri Oct 13, 2017 8:00 pm

Re: How to use alias instead of article id in link?

Post by CDXXonline » Mon Oct 21, 2019 9:56 am

Thanks, will definitely incorporate some of that. I take it (int) insures a number is being used? Didn't know that. And yeah, after you mention the catid thing SharkyKZ it seems obvious, not sure what i was thinking. The first query was totally unnecessary. I may just reverse that as the few values showing above are all i'm using from the row.

It's just for links to the pages from a comment history in profiles, some have single article menu items and others need the full path to the article using category menu aliases. Had to switch to alias because when you would log out from the first method after going to the page some fragments from the url were still present and it brought modules from the homepage onto the fragmented page, kind of combined them. Doing it this way logs out correctly with the alias intact, no homepage modules, and the link just looks cleaner. Was hoping there was just some short magical translate code i could stick in there.


Post Reply

Return to “Joomla! 3.x Coding”