We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13082
    • 2 Posts
    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.
      • 31037
      • 358 Posts
      This code is probably very ugly but I use it as a part of a custom front end system and it works for me. I’m sure there are shorter and cleaner ways of doing it.

          $objNewArticle = $modx->newObject('modResource');
          if ($objNewArticle) {
      // Some code
              // ALIAS: First create alias, after that check if that alias already exists, if it does, make it unique
              $alias = strtolower(trim($objNewArticle->cleanAlias($title)));
              $query = $modx->newQuery('modResource');
              $query->select($modx->getSelectColumns('modResource', '', '', array('alias')));
              $query->where('`alias` = ' . "'" . $alias . "'");
              $result = '';
              if ($query->prepare() && $query->stmt->execute()) {
                  $result = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
                  $count = count($result);
                  if ($count > 0) {
                      $alias = $alias . '-' . uniqid();
                  }
              } 
              $objNewArticle->set('alias', $alias);
              $objNewArticle->save();
      }
      

      uniqid() is a built in php function if someone should wonder.
        • 13082
        • 2 Posts
        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.)
          • 22303 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...