Windwalker
Windwalker

Reputation: 1945

Joomla: get article SEF URL in Content Plugin from ID

I'm currently developing a Joomla plugin in which I want to catch the event onContentAfterSave, in order to post the newly saved article to a URL shortener, for use on social networks.

Can someone help me on how to calculate the appropriate SEF URL to an article detail view (no menu entry!)?

The URL should be like: http://<domain>.<tld>/<category-id>-<category-title>/<article-id>-<article-title>.html

I've read this post, but that doesn't really provide a solution.

Upvotes: 2

Views: 3612

Answers (2)

Windwalker
Windwalker

Reputation: 1945

I was able to solve this problem with this post: https://joomla.stackexchange.com/questions/36/how-do-i-generate-a-sef-url-in-a-custom-module-instead-of-the-real-url

The provided solution is:

$rootURL = rtrim(JURI::base(),'/');
$subpathURL = JURI::base(true);
if(!empty($subpathURL) && ($subpathURL != '/')) {
    $rootURL = substr($rootURL, 0, -1 * strlen($subpathURL));
}
$url = $rootURL.JRoute::_(ContentHelperRoute::getArticleRoute($article->id,  $article->catid));

The really good part of this solution is, that it also works with installations contained in a subdirectory.

Upvotes: 3

RichardB
RichardB

Reputation: 2771

Use JRoute to convert the non-SEF parameters-based URL to the SEF one.

In the case of linking to a standard Joomla article, it would need the following information:

$url = JRoute::_('index.php?option=com_content&view=article&id='.$article->id);

$article->id is available via onContentAfterSave, though you may have called the $article object something else, in which case rename as appropriate.

If you do have a menu item for articles, then add an Itemid parameter of the menu item's id, and it will load the modules related to that menu item.

Upvotes: 0

Related Questions