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

    Im trying to add parameter to a snippet that allows me to set a value when calling the snippet.

    This is the original snippet.

    <?php
    function createNewResource( &$fields ){
        
        global $modx;
        $values = array();
    	
        $values['pagetitle'] = $modx->db->escape($fields['title']);
        $values['longtitle'] = '';
        $values['description'] = '';
        $values['alias'] = '';
        $values['published'] = 1; //0 - if you want the document to be created unpublished
        $values['parent'] = 0; //id of the container for the the new document
        $values['introtext'] = '';
        $values['content'] = $modx->db->escape($fields['content']);
        $values['template'] = 4; //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') );
        
        //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();
    }
    ?>
    


    My edited version is

    <?php
    $params = array (
      'conStart' => isset($conStart)? $conStart:'25',
    );
    
    function createNewResource( &$fields ){
        
        global $modx;
        $values = array();
    
        $values['pagetitle'] = $modx->db->escape($fields['title']);
        $values['longtitle'] = $modx->db->escape($fields['email']);
        $values['description'] = '';
        $values['alias'] = $modx->db->escape($fields['title']);
        $values['published'] = 0; //0 - if you want the document to be created unpublished
        $values['parent'] = $conStart; //id of the container for the the new document
        $values['introtext'] = '';
        $values['content'] = $modx->db->escape($fields['content']);
        $values['template'] = 4; //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') );
        
        //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();
    }
    ?>
    ?>


    The bits I have added are
    $params = array (
      'conStart' => isset($conStart)? $conStart:'25',
    );
    


    and
        $values['parent'] = $conStart; //id of the container for the the new document


    And when I call the snippet I use

    [!createNewResource? &conStart=`60` !] 


    the conStart variable will just let me select the container I want the content to post to when I call the snippet.

    Should be a simple solution im just new to coding snippets.

    Any assistance appreciated

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      $conStart is not a variable; it’s an array element in your code; you need to access it by
      $params['conStart']

      If you want it to be a variable, just do
      $conStart = isset($conStart) ? $conStart : '45';
        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
        • 27716
        • 126 Posts
        Hi Susan,

        Thanks for your support.

        I have changed the code so it is now like this

        <?php
        $conStart = isset($conStart) ? $conStart : '10';
        
        function createNewResource( &$fields ){
            
            global $modx;
            $values = array();
        
            $values['pagetitle'] = $modx->db->escape($fields['title']);
            $values['longtitle'] = $modx->db->escape($fields['email']);
            $values['description'] = '';
            $values['alias'] = $modx->db->escape($fields['title']);
            $values['published'] = 0; //0 - if you want the document to be created unpublished
            $values['parent'] = $conStart; //id of the container for the the new document




        My snippet call looks like

        [!createNewResource? &conStart=`60` !]


        However the snippet still isnt posting under the container im defining.

        Its posting in the root of the site.

        It seems like it is not using the parameter.

        Any thoughts?
          • 3749
          • 24,544 Posts
          Try putting this inside the function:

          global $conStart;


          The $conStart variable inside your function is local and is never set in the function.

          If that doesn’t work, pass $conStart to the function as an argument.
            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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Oops... I didn’t even see the function part!
              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
              • 27716
              • 126 Posts
              Thanks so much for helping guys,

              In the end I gave up on the variable and settled for this!

              $values['parent'] = $modx->documentIdentifier


              It worked perfectly for what i needed. Posting in to the document where the snippet call is executed!

              Will be handy to know the global bit though!

              Thanks again.