Search code examples
phpjoomlajoomla-extensions

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.


Solution

  • 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.