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

    Here's my contribution. I only just noticed that someone did this with a random alias, but here is my code to take the title and clean it before putting in the alias.

    I couldn't get the modx system regexp to work so cobbled my own together (my brain still hurts - but regular expressions do that) - I am certain someone will find fault with this and am happy for improvements

    It also has a redirect to a Thank You page at the end.

    $doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
     
    if (empty($doc)){
        $doc = $modx->newObject('modResource');
        $doc->set('createdby', $modx->user->get('id'));
    }
    else{
        $doc->set('editedby', $modx->user->get('id'));
    }
     
    $allFormFields = $hook->getValues(); 
    foreach ($allFormFields as $field=>$value)
    {
     
       if ($field !== 'spam' && $field !== 'resource_id'){
             $doc->set($field, $value);
        }
    
       //we need to parse the title into the alias
       if($field == 'pagetitle'){
          //replace spaces with -
          $alias = preg_replace('/[\' \']/' , '-' , $value);
    
          //remove non alpha and a common injection string 
          $alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
    
                                       //this is the standard revo regexp
                                       // \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
       }
    }
     // now set the alias
     $doc->set('alias', $alias);
     
    $doc->set('template', '1');
    $doc->save();
     
    foreach ($allFormFields as $field=>$value)
    {
        if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
        {
            /* handles checkboxes & multiple selects elements */
            if (is_array($value)) {
                $featureInsert = array();
                while (list($featureValue, $featureItem) = each($value)) {
                    $featureInsert[count($featureInsert)] = $featureItem;
                }
                $value = implode('||',$featureInsert);
            }   
            $tv->setValue($doc->get('id'), $value);
            $tv->save();
        }
    }
     
    $url = $modx->makeUrl(84);
    $modx->cacheManager->refresh(); // suggested by gallenkamp
    $modx->sendRedirect($url);
    [ed. note: xxxmicrobexxx last edited this post 11 years, 4 months ago.]
    • befpore the redirect, I call this to have my getResources fetch the correct url, including the alias:

      $modx->cacheManager->refresh();
        • 38669
        • 51 Posts
        Quote from: gallenkamp at Jan 05, 2013, 08:53 AM
        befpore the redirect, I call this to have my getResources fetch the correct url, including the alias:

        $modx->cacheManager->refresh();

        Nice, I have added it to the code. Thanks
          • 16430
          • 217 Posts
          Hi, this snippet is working pretty well, except one thing.
          I have multiple select box in the form which populate values into TV. But it always return only one value, no matter how many options I have selected.
          TV input and output is set to text.
          Any idea whats wrong?
          In form I am using this syntax:
          <option value="blue" [[!+fi.color:FormItIsSelected=`blue`]] >Blue</option>
            • 4172
            • 5,888 Posts
            did you define your multiple select box as an array?

            name="color[]"
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 16430
              • 217 Posts
              Quote from: Bruno17 at Feb 28, 2013, 09:26 PM
              did you define your multiple select box as an array?

              name="color[]"

              ..ehm..no...

              It works! Thanks...
                • 16430
                • 217 Posts
                Is it possible to add page ID to newly generated alias? (posted by xxxmicrobexxx)
                  • 4172
                  • 5,888 Posts
                  try something like that:

                  $doc->set('template', '1');
                  $isnew = $doc->isNew();
                  
                  if ($doc->save()) {
                      if ($isnew) {
                          $doc->set('alias', $doc->get('alias' . '-' . $doc->get('id')));
                          $doc->save();
                      }
                  }

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

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 16430
                    • 217 Posts
                    I replaced this code:
                    $doc->set('template', '1');
                    $doc->save();


                    with your code, but it saves resource without ID in URL
                      • 16430
                      • 217 Posts
                      Quote from: nick2687 at Jan 25, 2012, 05:02 PM
                      I'm curious to know if there is there a way to get the new resource id returned to the page after it has been created.

                      For example: Have it create the new resource and then return the id of the new resource back to the page into a placeholder so that you could for example redirect to the newly created resource upon creation.

                      Have you found out how to do that?