Auto alias creation addition to existing method

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
User avatar
Paul_F
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 115
Joined: Thu May 09, 2013 5:40 pm
Location: Athens,Greece
Contact:

Auto alias creation addition to existing method

Post by Paul_F » Tue Jan 14, 2014 7:19 am

Ok folks i've been on that thing for some days now and finally today i managed to complete what i wanted...

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;
	}

Now the problem is that if u do that ,when u trying to save menu or anything with an alias, the alias turns into blog_spot_id...I am just giving you guys that one for now and i will repost over here when i manage to fix the menu thingie. You can use that if u just have a website that is rdy with all the menu structuring etc and u have to renew it every day with new articles.

Any ideas on how to make it work only for articles are welcome :laugh: .
http://activewebdt.com
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”

User avatar
muzaru
Joomla! Explorer
Joomla! Explorer
Posts: 279
Joined: Wed Mar 31, 2010 9:34 am
Location: Amersfoort

Re: Auto alias creation addition to existing method

Post by muzaru » Wed Jan 15, 2014 12:19 pm

There is a better solution to this problem :)

Attached you will find a plugin that does what you want. With this plugin there is no need to hack into the core (something you'll have to redo every update most likely), it works only on articles (at this time), supports custom alias titles (only [id] will be replaced, at this time) and you can set it up to work only in specified categories.

Now, as is said in the description; this will force it to a new alias, because at the point where the plugin is called, the alias is already filled so I have no clue if the user filled it in or the system. Also, it will only work on new articles.
You do not have the required permissions to view the files attached to this post.

User avatar
Paul_F
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 115
Joined: Thu May 09, 2013 5:40 pm
Location: Athens,Greece
Contact:

Re: Auto alias creation addition to existing method

Post by Paul_F » Wed Jan 15, 2014 7:22 pm

My friend, plugin's not working for me...idk if i am doing something wrong but it's not working ... i removed my code from output.php i enabled the plugin and it is not working ...

edit : i tested it in a local site and it is working .So now i have to find out what's wrong in the live site... Anyways thanks for the plugin :) it does what i wanted .It's also working with UTF aliases which is great because most of my article titles are in Greek
http://activewebdt.com
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”

User avatar
muzaru
Joomla! Explorer
Joomla! Explorer
Posts: 279
Joined: Wed Mar 31, 2010 9:34 am
Location: Amersfoort

Re: Auto alias creation addition to existing method

Post by muzaru » Wed Jan 15, 2014 8:23 pm

Perhaps you missed the part about the new articles? There is a check in the plugin, if it's not a new article it doesn't change the alias.

If you *really* need to work on older articles I could rewrite it a bit...

User avatar
Paul_F
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 115
Joined: Thu May 09, 2013 5:40 pm
Location: Athens,Greece
Contact:

Re: Auto alias creation addition to existing method

Post by Paul_F » Thu Jan 16, 2014 10:00 am

Yeah , i know it works only for new articles. But the thing is that the plugin doesnt override the joomla's core. I mean that even if i create a new article, the title remains as it is and it doesnt change to post_syntax in our case blog_post_[id].

Something else, u dont think it would be better if we change the event from onContentBeforeSave to onContentAfterSave?
http://activewebdt.com
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”

User avatar
muzaru
Joomla! Explorer
Joomla! Explorer
Posts: 279
Joined: Wed Mar 31, 2010 9:34 am
Location: Amersfoort

Re: Auto alias creation addition to existing method

Post by muzaru » Thu Jan 16, 2014 8:09 pm

Perhaps you could try to do a 'die()' in the function, see if it gets called at all.

I did think about it, but only after everything was made :) so, if I were to expand or it or anything I think I would change that. Because it's not really safe to assume the ID the article will get, even though it's a real slim chance it's wrong.

User avatar
Paul_F
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 115
Joined: Thu May 09, 2013 5:40 pm
Location: Athens,Greece
Contact:

Re: Auto alias creation addition to existing method

Post by Paul_F » Fri Jan 17, 2014 1:16 am

Well yeah, and the think in onContentBeforeSave is that it gets the maximum id from content.

So if u have deleted articles, say that u have article id 112 and 113 and u delete the id 113 the next article wont take 113 it will take 114 but the alias will be 113 cause it gets the max id and increases by 1 which in that case is 112 and not 113 cause we deleted it...

So the alias wont be the actual id of article, just for one article tho,after that it will be the normal id again. But every time u delete 1 article the alias will be like that.
http://activewebdt.com
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”

User avatar
muzaru
Joomla! Explorer
Joomla! Explorer
Posts: 279
Joined: Wed Mar 31, 2010 9:34 am
Location: Amersfoort

Re: Auto alias creation addition to existing method

Post by muzaru » Fri Jan 17, 2014 8:22 am

oke oke...you made me do it... ;)
You do not have the required permissions to view the files attached to this post.

thegeorge75
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Mon Mar 24, 2014 11:23 am
Contact:

Re: Auto alias creation addition to existing method

Post by thegeorge75 » Wed Sep 24, 2014 3:50 pm

Here my customization of AliasChanger.zip. For existing alias add prefix like this: -(1),-(2),-(3)... Writeln for the new article of frontend

Code: Select all

	public function onContentBeforeSave( $context, $article, $isNew )
	{

	  // Is new article and frontend ? For backend use: com_content.article
	  if ($isNew == 1 && $context=='com_content.form'):
	    // Default alias
            $alias=$article->alias;

            // Is default alias exist ?
	    $db	= JFactory::getDBO();
	    $query = $db->getQuery(true);
            $db->setQuery("SELECT alias  FROM #__content WHERE alias = '".$alias."'");
            $result=$db->loadResult();
            $cnt=0;

            // Find free alias with prefix -(N) , where N=1,2,3..
            while ($result):
             $cnt++;
 	     $query = $db->getQuery(true); 
 	     $alias= preg_replace("/\-\(\d+\)$/","",$alias).'-('.$cnt.')';
             $db->setQuery("SELECT alias  FROM #__content WHERE alias = '".$alias."'");
             $result=$db->loadResult();
	     if ($result)
              $alias=$result;	
	    endwhile;

          // Change autoalias
	  $article->alias= $alias;	
	 endif;

      return true;
      }


Locked

Return to “Joomla! 3.x Coding”