We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36533
    • 65 Posts
    I'm working through an example here:
    http://modxrules.com/articles/new-documents-with-eform#jf199bcbea

    I've replaced this line:

    $values['alias'] = '';


    with:

    $values['alias'] = $modx->db->escape($fields['title']);


    However, this sets the alias uncleansed with uppercase characters and spaces, resulting in %20 between words..

    I'm not quite sure how to correct this in the snippet and I'm assuming I could manipulate the default alias code below with a custom plugin:

    /**
     * Format alias to be URL-safe. Strip invalid characters.
     *
     * @param string Alias to be formatted
     * @return string Safe alias
     */
    function stripAlias($alias) {
        // let add-ons overwrite the default behavior
        $results = $this->invokeEvent('OnStripAlias', array ('alias'=>$alias));
        if (!empty($results)) {
            // if multiple plugins are registered, only the last one is used
            return end($results);
        } else {
            // default behavior: strip invalid characters and replace spaces with dashes.
            $alias = strip_tags($alias); // strip HTML
            $alias = preg_replace('/[^\.A-Za-z0-9 _-]/', '', $alias); // strip non-alphanumeric characters
            $alias = preg_replace('/\s+/', '-', $alias); // convert white-space to dash
            $alias = preg_replace('/-+/', '-', $alias);  // convert multiple dashes to one
            $alias = trim($alias, '-'); // trim excess
            return $alias;
        }
    }
    


    Not sure which is the best way to go or how to customize that code.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Always a good idea to see how MODx does it. In the manager/processors/save_content.processor.php in line 70 you'll find a very simple
      $alias = strtolower($modx->stripAlias(trim($pagetitle)));
      
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 36533
        • 65 Posts
        Very cool.

        Here is my code:

        <?php
        function CreateBlogPost( &$fields ){
            
            global $modx;
        	
            $values = array();
            $values['pagetitle'] = $modx->db->escape($fields['title']);
            $values['longtitle'] = '';
            $values['description'] = '';
            $values['alias'] = strtolower($modx->stripAlias(trim($fields['title'])));
            $values['published'] = 1; //0 - if you want the document to be created unpublished
            $values['parent'] = 47; //id of the container for the the new document
            $values['introtext'] = '';
            $values['content'] = $modx->db->escape($fields['content']);
            $values['template'] = 7; //the template of your new document
            $values['searchable'] = 1;
            $values['cacheable'] = 1;
            $values['createdby'] = 1; //this is not the web user ID. Change it to an existing manager id
            $values['createdon '] = time();
            $values['editedby'] = 1; //this is not the web user ID. Change it to an existing manager id
            $values['editedon '] = time();
            //you can find more document properties by browsing the modx_site_content table
        	
            //insert the new document. In case you need the id of the new document use the  $newId variable
            $newId = $modx->db->insert( $values, $modx->getFullTableName('site_content') );
        	
        	//page specific template variables
        	$tvValues = array();
        	
        	$tvValues['tmplvarid'] = 2;//ID of the TV
        	$tvValues['contentid'] = $newId; //The ID of the DOCUMENT
        	$tvValues['value'] = $modx->db->escape($fields['tags']);
        	$modx->db->insert( $tvValues, $modx->getFullTableName('site_tmplvar_contentvalues') );
            
            //clear the cache. in case you use Ditto to display the new document
            clearCache();
        	
            return true;
        }
        
        function clearCache() {
            include_once "manager/processors/cache_sync.class.processor.php";
            $sync = new synccache();
            $sync->setCachepath("assets/cache/");
            $sync->setReport(false);
            $sync->emptyCache();
        }
        ?>
        


        Just need to figure out how to handle the createdby and I'll be all set to write up a tutorial on putting together front-end blogging step-by-step.

        Thanks again, Susan. =)