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

    I don’t find how to generate an alias computed from a string, using the api and php, ( without activating this in the global options ).
    Users can create documents but just fill the page title and I would like the alias automatically generated :
    
    	$doc = $modx->newObject('modDocument');
    	$data = array(
    		'pagetitle'   	=> $title,         // $title="Joé, the slow one !"
    		'alias'   	=> $title          // how to create the alias ? "joe-the-slow-one"
    	);
    


    does someone know the name of the api function ?
      • 3749
      • 24,544 Posts
      If I’m understanding you, there’s no API call for that. You’d have to create a plugin attached to the OnBeforeDocFormSave event and do it there.

      Your users would leave the alias blank and the alias would be generated as they saved the document.
        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
        • 10152
        • 156 Posts
        thanks wink
        • Quote from: BobRay at Feb 23, 2010, 02:44 AM

          If I’m understanding you, there’s no API call for that. You’d have to create a plugin attached to the OnBeforeDocFormSave event and do it there.

          Your users would leave the alias blank and the alias would be generated as they saved the document.
          There is an API call for it, but it is on the list of things to be addressed/improved before RC1 release, including adding the event for custom TransAlias plugins to work with, as well as adding error handling for duplicate aliases or other potential conflicts. Some of these features were added to Evolution after Revolution was branched off and needs to be synced back up. This should be addressed in the next couple of days.
            • 10152
            • 156 Posts
            The dream would be that it automatically checks if the alias already exists, and if necessary adds a prefix (or suffix) to ensure each alias is always unique.

            this old-fashioned code (shame on me) can make it work, until the api is 100% complete :

            $title = 'Title of the document';
            makeAlias($title);
            function makeAlias($url){
            
            		$url	= no_accent($url);
            		$url	= ereg_replace("[^a-z A-Z0-9-]*","",$url);
            		$url	= ereg_replace("-{2,}","-",$url);
            		$url	= ereg_replace(' ','-',$url);
            		$url	= trim(strtolower($url));
            		$url	= ltrim($url,'-');
            		$url	= rtrim($url,'-');
            		return $url;
            }
            function no_accent($filename) {
            		$str = strtr(utf8_decode($filename),utf8_decode("ÀÁÂÃÄÅÇÑñÇçÈÉÊËÌÍÎÏÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöøùúûüýÿ' "),
            										   "AAAAAACNnCcEEEEIIIIOOOOOOUUUUYaaaaaaceeeeiiiiooooooouuuuyy--" );
            		return $str;
            	}
            
            • FYI, the official process for building/checking the URLs is in the core/model/modx/processors/resource/create.php processor right now, but that functionality is going to be moved into an official API function before RC1. Work is in progress on this now.
                • 10152
                • 156 Posts
                i just saw that modX::makeUrl can generate a URL representing a specified resource.
                • Quote from: French at Feb 24, 2010, 12:14 AM

                  i just saw that modX::makeUrl can generate a URL representing a specified resource.
                  modX::makeUrl() is for getting the URL of existing Resources, not of ones that are being created. The code in the processor is what checks to make sure unique URLs are being generated when you attempt to save the Resource with a particular alias. The alias is cleansed (using modResource::cleanAlias()) and then the URL is constructed (from the parents if friendly_alias_path is enabled) and checked against the existing URLs in the system. This entire process will be moved into a single function before RC1, and will include an invocation of the event OnStripAlias to which you can attach plugins to alter the process.
                  • If we have a component that get a blog post on Wordpress, will the API will allow us to create an alias dynamically based on the blog post title?

                    Details:

                    a MODx Resource named -blog- with a snippets call -Wordpress- and i want to obtain an accessible url like http://www.mysite.com/blog/mygeneratedalias.html without modifying the htaccess file manually.
                    • Dynamic aliasing for a single Resource (if that’s what you are talking about) needs to be done with a plugin OnPageNotFound. I’ve posted several examples of this in the forum. Start here.