I wanted articles during save,to take a different alias than title if the alias field is empty...Joomla's taking the string and adds '-' after every word.
I wanted it to be blog-post_100. Where 100 is the id of the newly created article! I've been looking around the web for sometime for possible methods but find only a few things ... Anyways, i think that a lot of ppl want the articles aliases to be like that, so i am giving you the code over here ... Everything is at the filtering so we going to libraries/joomla/filter/output.php @ line 86 :
Code: Select all
public static function stringURLSafe($string)
{
/* This method turning every alias (article's or menu's) into a string
to a form of blog_post_+id(where id is the last id in contents +1 to give us a valid number).
Suggesting no to use it if u need to create new menus etc.Use it if your menu structure is rdy
and you need something like that for better SEO or if u want better looking aliases. */
/* We asking the db to give us the last article id so we put
+1 to the result variable and we get the new id */
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id')
->from('#__content')
->where('id = (SELECT MAX(id)FROM #__content)');
$db->setQuery($query);
$result = $db->loadResult();
// Taking the result which is a number and we add +1 to give us the next saved item id
$item_id = $result +1 ;
// New line that passes a string
$blog_post = "blog-post";
// New line that customizes the alias
$string = $blog_post . '_' . $item_id;
// Remove any '-' from the string since they will be used as concatenaters
//$str = str_replace('-', ' ', $string);
$lang = JFactory::getLanguage();
$str = $lang->transliterate($str);
// Trim white spaces at beginning and end of alias and make lowercase
$str = trim(JString::strtolower($str));
// Remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);
// Trim dashes at beginning and end of alias
$str = trim($str, '-');
return $str;
}
/**
* This method implements unicode slugs instead of transliteration.
*
* @param string $string String to process
*
* @return string Processed string
*
* @since 11.1
*/
public static function stringURLUnicodeSlug($string)
{
/* This method turning every alias (article's or menu's) into a string
to a form of blog_post_+id(where id is the last id in contents +1 to give us a valid number).
Suggesting no to use it if u need to create new menus etc.Use it if your menu structure is rdy
and you need something like that for better SEO or if u want better looking aliases. */
/*We asking from db to give us the last article id so we put
+1 to variable and we get the new id*/
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id')
->from('#__content')
->where('id = (SELECT MAX(id)FROM #__content)');
$db->setQuery($query);
$result = $db->loadResult();
// Taking the result which is a number and we add +1 to give us the next saved item id
$item_id = $result +1 ;
//New line that passes a string
$blog_post = "blog-post";
//New line that customizes the alias
$string = $blog_post . '_' . $item_id;
// Replace double byte whitespaces by single byte (East Asian languages)
$str = preg_replace('/\xE3\x80\x80/', ' ', $string);
// Remove any '-' from the string as they will be used as concatenator.
// Would be great to let the spaces in but only Firefox is friendly with this
$str = str_replace('-', ' ', $str);
// Replace forbidden characters by whitespaces
$str = preg_replace('#[:\#\*"@+=;!><&\.%()\]\/\'\\\\|\[]#', "\x20", $str);
// Delete all '?'
$str = str_replace('?', '', $str);
// Trim white spaces at beginning and end of alias and make lowercase
$str = trim(JString::strtolower($str));
// Remove any duplicate whitespace and replace whitespaces by hyphens
$str = preg_replace('#\x20+#', '-', $str);
return $str;
}
Any ideas on how to make it work only for articles are welcome .