We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48496
    • 2 Posts
    I am using a slightly modified version of Bruno17 formit2resource snippet to allow frontend users to create article resource and would like them to be redirected to the newly created page on submitting. The problem I'm having is it not redirecting with the newly created article resource id variable.

    Instead I'm getting this error "Attempted to redirect to an empty URL."

    		
    
            $tv->setValue($doc->get('id'), $value);
            $tv->save();
        }
    }
    
    $id = $doc->get('id');;
    $modx->log(modX::LOG_LEVEL_ERROR, $id);
    
    $url = $modx->makeUrl($id);
    $modx->cacheManager->refresh();
    $modx->sendRedirect($url);


    What I don't understand is it prints the correct ID in the error log but some how somehow does not generate the url with makeUrl.

    If I change it to
    $modx->makeUrl(10);
    It redirects to that page fine. Is it that the page has not been created on submission. Here is the full snippet.

    <?php
    $doc = $modx->getObject('Article',array('id'=>$hook->getValue('resource_id')));
    $published = 1;
    $date = date('Y-m-d H:i:s');
    
    if (empty($doc)){
        $doc = $modx->newObject('modDocument');
    	$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'){
             $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&=+%#<>"~:`@\?\[\]\{\}\|\^'\
       }
       
    }
    
    
    $doc->set('parent', '2119');
    $doc->set('alias', $alias);
    $doc->set('template', '36');
    $doc->set('published',$published);
    $doc->set('publishedon', $date);
    
    $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();
        }
    }
    
    $id = $doc->get('id');
    $modx->log(modX::LOG_LEVEL_ERROR, $id);
    
    
    $url = $modx->makeUrl($id);
    $modx->cacheManager->refresh();
    $modx->sendRedirect($url);




    Site is on modxcloud - MODX Revolution 2.2.15-pl (traditional)
    PHP Version 5.4.30

    Any help or advice would be much appreciated.

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Try adding this after the save() if it's a new doc:


      $modx->reloadContext();
        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
      • discuss.answer
        • 48496
        • 2 Posts
        Quote from: BobRay at Aug 03, 2014, 04:20 PM
        Try adding this after the save() if it's a new doc:


        $modx->reloadContext();


        Thanks BobRay, that worked. It was bugging me for days!!
          • 3749
          • 24,544 Posts
          I'm glad that worked for you. It bugged me for days too before someone gave me the solution. 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