Hello
I don’t know if this is the right place to post this. I’m working on a website, and have created a page where the client can create and edit news from the frontend. I used the formit2resource snippet I got from the forums. I have the use friendly urls system setting set to "yes", automatically generate alias set to "yes", and allow duplicate aliases set to "no". Here lies my problem. If the client creates a document with a pagetitle that duplicates that of one already existing, the document isn’t created. Is there a way when creating documents to check the alias(pagetitle) against already existing aliases(pagetitles) and then append a unique number to the alias if a duplicate is found? I’m not too sure, but i think this existed in some previous versions of evolution. Thank you.
Iretomiwa.
Thank you Uncle68. I should have posted back that I had resolved this. I ended up using the code below that works perfectly for me:
<code>
$doc = $modx->newObject(’modResource’);
//SOME CODE
$alias = $doc->cleanAlias($pagetitle);
if($modx->getCount(modResource, array(’alias’=>$alias))!= 0) {
$count = 1;
$newAlias = $alias;
while($modx->getCount(modResource, array(’alias’=>$newAlias))!= 0) {
$newAlias = $alias;
$newAlias .= ’-’ . $count;
$count++;
}
$alias = $newAlias;
}
$doc->set(’alias’,$alias);
$doc->save();
</code>
(I’m posting this from a mobile device, so do bear with me if the code section isn’t showing up like it should.)
-
MODX Staff
- 10,725 Posts
This is why creating Resources should be done via the processors and not directly via xPDO objects. There are important rules being enforced in those processors that handle these issues...