10 years ago
Topic

Since Joomla 1.6 the Alias for each article should be unique in a category. Joomla checks the alias bevor storing and shows a error if the alias already exists. So the user has to be care of choosing an unique alias. I don't know why it isn't possible to set an unique alias automaticly. I found this solution:

Create a small content plugin named Jo_Unique_Alias with this jo_unique_alias.php:

<?php

defined( '_JEXEC' ) or die;

// Plugin
class plgContentJo_Unique_Alias extends JPlugin
{

  /**
  * @param object  $context  The context of the content passed to the plugin
  * @param object  $article  A reference to the JTableContent object that is
being saved which holds the article data
  * @param boolean  $isNew  A boolean which is set to true if the content is about to be created.
  * @return boolean  returns true, if the article should be stored
  */
  function onContentBeforeSave($context, &$article, $isNew) {
   
  $alias = $article->alias;
  $catid = $article->catid;
   
  // Check: alias already exists in db and add "-2", "-3", ... if yes:
  $table = JTable::getInstance('content');
  while ($table->load(array('alias' => $alias, 'catid' => $catid))) {
  $alias = JString::increment($alias, 'dash');
  }
  $article->alias = $alias;   
  return true;
  }
}
?>

and install it in the first Order of content plugins. Don't forget to aktivate the plugin in backend!

Regards,
Jochen

Get a Book for SEBLOD
10 years ago
0
Level 1

Great Jo, very useful!

Worked fine, thanks.

85 Posts
Jpeg
10 years ago
1
Level 1

Hi thx the sharing.

don't forget to place Seblod plugin system & content on top too :)

Otherwise we have another seblod solution : By using the Typo plugin "Ajax Validation" on title field and configure on alias database field.

So if a writter put the same title in the field, the form wasn't performed.


It's the Jpeg's Tips of today ;) @ Vik

Have a nice day

Jpeg

10 years ago
0
Level 2

Yeah, Jpeg, also we can use concat plugin by Simon with <content> property, but this plugin easier.

98 Posts
Edwin
10 years ago
1
Level 1

Hello

Not sure what I'm doing wrong, but using this plugin doesn't quite work on site where user edits content. It is very useful when users do not edit their content. I notice with this plugin, when user edits content, plugin changes alias by adding -1, -2, etc. I don't want alias of existing articles changed when edited. I want unique alias for new content. When user edits, alias must remain the same.

But still very useful plugin and I would use it if I knew how to stop it adding unique alias to existing content.

regards

10 years ago
0
Level 2

hi ;


u are doing nothing wrong !! 


That its because the plugin is not complete !

try that :

if(!$isNew){
return;
}

also if dont work maybe need to declare like that :

$isNew = ($article->id == 0)? true : false;

10 years ago
2
Level 1

Hi Jo/All,

Can anyone plz, send me the unique alias plugin?


Regards,

kacraj.

188 Posts
uriel
10 years ago
1
Level 2
10 years ago
0
Level 3

Hi uriel,

Thank you very much for this plugin. It is working fine.


Regards,

kacraj

3 Posts
jagwirez
10 years ago
0
Level 1

Confirmed. Works great, it will add the first available number after the dash. And you must use the if !$isNew, so that each edit will not change the alias. Here is the updated code that I used.

<?php

defined( '_JEXEC' ) or die;

// Plugin
class plgContentJo_Unique_Alias extends JPlugin
{

  /**
  * @param object  $context  The context of the content passed to the plugin
  * @param object  $article  A reference to the JTableContent object that is
being saved which holds the article data
  * @param boolean  $isNew  A boolean which is set to true if the content is about to be created.
  * @return boolean  returns true, if the article should be stored
  */
  function onContentBeforeSave($context, &$article, $isNew) {
  // If article is new, continue. If editing existing article, exit function. if(!$isNew){
return;
}
  $alias = $article->alias;
  $catid = $article->catid;
   
  // Check: alias already exists in db and add "-2", "-3", ... if yes:
  $table = JTable::getInstance('content');
  while ($table->load(array('alias' => $alias, 'catid' => $catid))) {
  $alias = JString::increment($alias, 'dash');
  }
  $article->alias = $alias;   
  return true;
  }
}
?>

179 Posts
carin
9 years ago
0
Level 1

Thanks for this app. But there is a bug: the appended number is "dynamic"! Imagine you have 3 content items with the same "title" which leads to the aliases "title" and "title-1" and "title-2" where the latter two were automatically set up. Now "title-1" becomes obselete and you delete that item. Next time you safe "title-2" it becomes "title-1" and in case you use title aliases to build your sef urls you have 2 major problems: 1. the "title-2" url just disappears and 2. the old and deleted "title-1" url appears to search engines still valid but points now to a different content.

I dont have the skill to fix it but I guess the creator might be able to do it quickly.

Thanks

310 Posts
ricco
9 years ago
0
Level 1

I use this plugin for the aliases: http://www.seblod.com/v2/forum/Field-Types-Plugin/46273-NEW-FIELD-Alias-Generator.htm

It works good for now.

Regards,

Ricco

37 Posts
geolits
9 years ago
1
Level 1

Hello, 

Jo thanks for this wonterful plugin.

I made some changes, so if the article exist, to add the article id at the end of the alias. I think with this change there will not be the problems mentioned from carin.

I upload the changed file.

Because i'm new at all this, If you have some observations please tell us.


Thanks again

188 Posts
uriel
9 years ago
0
Level 2

Good job :)

8 years ago
0
Level 1

Thanks for this plugin that avoids many problems we had so far!  Thanks again

Cyril

8 years ago
2
Level 1

Hi Geolits

the plugin seems to work only if the contents are in the same category. Can you confirm it please? With seblod we would need the plugin to work even if the contents are in different categories (corrresponding to a different content type). is it possible please?

thanks

cyril

8 years ago
1
Level 2

Hi

We modified it slightly so that the plugins ensure the alias is unique even from several categories:

just replace

 while ($table->load(array('alias' => $alias, 'catid' => $catid))) { 

with

 while ($table->load(array('alias' => $alias))) {

thanks

cyril

548 Posts
joomleb
7 years ago
0
Level 3

Hi guys,

Please, Is this the last solution to have same Title / different alias ?

Is there not an "official" Sebod plugin to do it ?

...and, I don't understand, It add the article id and/or add a consequential number counting how many same titles yet exist ?

8 years ago
2
Level 1

Hi,

Thanks for the plugin, it saved my weeks of find a fix.

Thanks for your time.

4229 Posts
Kadministrator
7 years ago
1
Level 2

This is now part of the core - when alias exists a suffix is appended to it.

548 Posts
joomleb
7 years ago
0
Level 3

Hi Klas, 

many thanks, and, Please,

when alias exists a suffix is appended to it




What kind of rule follows?

Does It add the article id and/or adds a consequential number counting/depending on how many same titles yet exist ?

6 years ago
4
Level 1

Hi

"This is now part of the core - when alias exists a suffix is appended to it."

it doesn't seem to bo true. if you create a first content in category1 you can create another content with the same title in category2. Both aliases will be the same

so the plugin still seems to be needed 

cyril

4229 Posts
Kadministrator
6 years ago
3
Level 2

No, it is not "Alias for each article should be unique in a category."

6 years ago
2
Level 3

Yes, this is why we modified the plugin so that it checks accross categories :)

Cyril

4229 Posts
Kadministrator
6 years ago
1
Level 4

not sure whether this is needed or not,but you can suggest the same change for seblod core

11 Posts
jlowell
6 years ago
0
Level 5

Hello thank for this wonderful plugin... really help but san we create a no per category ? Example if Category A we have 5 article the ID will be from 1 to 5 and in category B we have 3 article it should be 1 to 3. Because for now the ID is followed in different category. Because my ideas is to create an Invoice category and use seblod for invoicing and I will need that the number follow only in invoice category that I will use an an automatique invoice no.

Thank for help.

548 Posts
joomleb
6 years ago
1
Level 1

Hi guys, 

Please, 

1 - Is it possible to make a summary here ?

2 - Is the plugin still needed or the feature is yet into the core ?

3 - "when alias exists a suffix is appended to it" - What kind of rule follows? - Does It add the article id and/or adds a consequential number counting/depending on how many same titles yet exist ?

4229 Posts
Kadministrator
6 years ago
0
Level 2

Core only cares about akes unique alias inside the category, the same as Joomla. For 3 - try it.

5 years ago
0
Level 1

The modified plugin doesn't work anymore with current versions of joomla/seblod.

the issue remains: if you have two content types in two different joomla categories but sharing the SAME title, the aliases will be the same. However the joomla router can be fooled and you may be redirected to the wrong page / content.

this is why we need to automatically have different aliases

cyril

Get a Book for SEBLOD