DisgruntledGoat
DisgruntledGoat

Reputation: 72510

Is there a Joomla function to generate the 'alias' field?

I'm writing my own component for Joomla 1.5. I'm trying to figure out how to generate an "alias" (friendly URL slug) for the content I add. In other words, if the title is "The article title", Joomla would use the-article-title by default (you can edit it if you like).

Is there a built-in Joomla function that will do this for me?

Upvotes: 12

Views: 17253

Answers (4)

Gratia-Mira
Gratia-Mira

Reputation: 105

If anyone is looking for how to make an alias in Joomla 4, here is the solution:

use Joomla\CMS\Filter\OutputFilter;

(insert at the top of the document)

And then in the code.

$alias = OutputFilter::stringUrlSafe($string);

Upvotes: 2

Paul van Jaarsveld
Paul van Jaarsveld

Reputation: 1564

It's simple PHP.

Here is the function from Joomla 1.5 source:

Notice, I have commented the two lines out. You can call the function like

$new_alias = stringURLSafe($your_title);

function stringURLSafe($string)
    {
        //remove any '-' from the string they will be used as concatonater
        $str = str_replace('-', ' ', $string);
        $str = str_replace('_', ' ', $string);

        //$lang =& JFactory::getLanguage();
        //$str = $lang->transliterate($str);

        // remove any duplicate whitespace, and ensure all characters are alphanumeric
        $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);

        // lowercase and trim
        $str = trim(strtolower($str));
        return $str;
    }

Upvotes: 1

nanhe
nanhe

Reputation: 41

If you are trying to generate an alias for your created component it is very simple. Suppose you have click on save or apply button in your created component or suppose you want to make alias through your tile, then use this function:

$ailias=JFilterOutput::stringURLSafe($_POST['title']);

Now you can insert it into database.

Upvotes: 4

jlleblanc
jlleblanc

Reputation: 3510

Line 123 of libraries/joomla/database/table/content.php implements JFilterOutput::stringURLSafe(). Pass in the string you want to make "alias friendly" and it will return what you need.

Upvotes: 34

Related Questions