We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46718
    • 39 Posts
    Hey Everyone,

    I'm building a site for a local animal rescue group that pulls from an API of animals they have in their program.

    I've got a MODX Revo 2.2.10 install that I am using to dynamically create resources from the API feed of animals.

    However, sometimes the API feed has animals with the same name. Currently, my importer does create the resource, logs an error and moves on. But, the resource is not viewable (redirects to home) until someone goes in and manually changes the alias.

    So, my question is how I can have multiple animals with the same name and still have a unique alias, or URI? The manager says something about freezing the URI, but I didn't have much luck finding more information on that.

    Thanks,
    Roger

    This question has been answered by BobRay. See the first response.

      • 4172
      • 5,888 Posts
      you could make sure to have a unique alias by adding the id or datetime, when added or something else unique.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 46718
        • 39 Posts
        Quote from: Bruno17 at Mar 27, 2014, 03:08 PM
        you could make sure to have a unique alias by adding the id or datetime, when added or something else unique.

        Is there a way to hook into MODX's auto alias generation on save() and append my unique ID, or do I need to just generate my own alias?
          • 4172
          • 5,888 Posts
          what is your code, which is creating the resources currently?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 46718
            • 39 Posts
            Quote from: Bruno17 at Mar 27, 2014, 04:09 PM
            what is your code, which is creating the resources currently?

            Not sure how much you want, but this should give you the general idea.

            foreach ($apiPublicSearch->data as $api_value){ // start import
            $document = $modx->getObject('modResource',array('longtitle' => $api_value->animalID)); // look for existing document
            if(!empty($document)){ // update existing document
            	$document = $modx->getObject('modResource',array('longtitle' => $api_value->animalID)); //?? Do I need this
            	$document->set('pagetitle', $api_value->animalName);
            	$document->set('content', $api_value->animalDescription);
            	$document->set('publishedon', $createdDateTime);
            	if ($document->save() === false){// Save the document
            		$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
            		return false;
            	}
            }else{ // create new document
              $newDocument = $modx->newObject('modResource');
            	$newDocument->set('type', 'document'); // Create the default fields for the document
            	$newDocument->set('contentType', 'text/html');
            	$newDocument->set('pagetitle', $api_value->animalName);
            	$newDocument->set('longtitle', $api_value->animalID);
            	$newDocument->set('description', '');
            	$newDocument->set('alias', ''); //letting MODX generate this for me not sure if I need to be setting the value to blank
            	$newDocument->set('link_attributes', '');
            	$newDocument->set('publishedon', $createdDateTime);
            	$newDocument->set('published', 1);
            	$newDocument->set('pub_date', 1);
            	$newDocument->set('unpub_date', 0);
            	if($api_value->animalSpecies == 'Dog'){
            		$newDocument->set('parent', 2);
            	}elseif($api_value->animalSpecies == 'Cat'){
            		$newDocument->set('parent', 3);
            	}else{
            		$newDocument->set('parent', 19);
            	}
            	$newDocument->set('isfolder', 0);
            	$newDocument->set('introtext', '');
            	$newDocument->set('content', $api_value->animalDescription);
            	$newDocument->set('richtext', 1);
            	$newDocument->set('template', 3);
            	$newDocument->set('menuindex', 1);
            	$newDocument->set('searchable', 1);
            	$newDocument->set('cacheable', 1);
            	$newDocument->set('createdby', $modx->user->get('id'));
            	$newDocument->set('createdon', $createdDateTime);
            	$newDocument->set('editedby', '');
            	$newDocument->set('editedon', '');
            	$newDocument->set('deleted', '');
            	$newDocument->set('deletedon', '');
            	$newDocument->set('deletedby', '');
            	$newDocument->set('publishedon', '');
            	$newDocument->set('publishedby', '');
            	$newDocument->set('menutitle', '');
            	$newDocument->set('hidemenu', '');
              $newDocument->set('donthit', '');
            	$newDocument->set('haskeywords', '');
            	$newDocument->set('hasmetatags', '');
            	$newDocument->set('privateweb', '');
            	$newDocument->set('privatemgr', '');
            	$newDocument->set('content_dispo', '');
            	$newDocument->set('hidemenu', '');
            	$newDocument->set('class_key', 'modDocument');
            	$newDocument->set('content_type', 1);
            	if ($newDocument->save() === false){ // Save the document
            		$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
              	return false;
            	}
            } // close else ($newDocument)
            
            • discuss.answer
              • 3749
              • 24,544 Posts
              If you generate a unique alias to begin with, the URI will take care of itself.

              I think I would do something like this:


              $alias = $modx->cleanAlias($api_value->animalName);
              
              $r = $modx->getObject('modResource' array('alias' -> $alias));
              if ($r) {
                /* alias in use */
                $i = 1;
                 for($i = 1; $i < 100; $i++) {
                     $r = $modx->getObject('modResource' array('alias' -> $alias .  $i));
                     if (! $r) {
                         /* Found an unused one */
                         $alias .= $i;
                         break;
                     }
                 }
              }
              
              $newDocument->set('alias', $alias);
              


              For a dog named Rex, this will try aliases of rex, rex1, rex2, etc. until it finds one that's unused and then sets that as the alias.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 4172
                • 5,888 Posts
                you could also add the resource-id:

                    if ($newDocument->save() === false){ // Save the document
                        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
                    return false;
                    }
                    $newDocument->set('alias',$newDocument->cleanAlias($api_value->animalName). '-' .$newDocument->get('id'));
                $newDocument->save();

                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 46718
                  • 39 Posts
                  Thanks guys, $modx->cleanAlias() was what I was missing. I think I'm going to go with Bob's answer mainly so the URL is more human readable.

                  Thanks again!
                    • 46718
                    • 39 Posts
                    If anybody else ever reads this far down, I did have to modify the code from BobRay slightly, as cleanAlias() can only be called on a resource, not the modx object itself. So Bruno's Example works, but not Bob's.

                    This is what I ended up with:
                    $newDocument = $modx->newObject('modResource');
                    $animalName = $api_value->animalName;
                    $alias = $newDocument->cleanAlias($animalName);
                    

                    Followed by lines 3-17 in the BobRay example.
                      • 3749
                      • 24,544 Posts
                      That's a good fix. Thanks for reporting back. smiley
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting