2

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.

Community
  • 1
  • 1
Windwalker
  • 1,915
  • 5
  • 23
  • 44

2 Answers2

3

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.

Community
  • 1
  • 1
Windwalker
  • 1,915
  • 5
  • 23
  • 44
0

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.

RichardB
  • 2,615
  • 1
  • 11
  • 14
  • Thanks a log, but I think, it's not exactly what I was looking for: This generated URLs like http://./<1st-level-menu>/<2nd-level-menu>.html?id=, which leads to 404 error. Meanwhile I was able to find a propertly working solution, see my answer below. – Windwalker May 06 '16 at 09:39