We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    Trying to figure out how to copy one resource’s TV values to another, to flesh out the functionality of the "publishDraftCopy" plugin I’m working on (http://ichosemodx.wordpress.com/2010/08/02/updating-a-published-resource-from-a-draft-copy/.

    My initial thought is that the soundest approach to updating TVs in the production resource is to

    • get all the TVs present in the draft resource AND the production resource
    • update all the production TVs with the values from the draft
    • remove/unset/delete any TVs that are present in the production resource but not in the draft resource

    Does that seem like a basically sound approach?

    Next question - I’m a little unclear on how to set the TV values of the production resource. This is what I came up with:
             $TVs = $resource->getMany('TemplateVars');
             foreach ($TVs as $TV) {
               $newval = $TV->getValue($resource->('id'));
               $TV->setValue($prodResource->('id'), $newval);
               $TV->save();
             }  


    That doesn’t seem to quite work right though... and it doesn’t feel quite right either. I’m not fully grasping the concept of what I am getting and setting with the collection of TVs. Any insight would be greatly helpful!

      • 14883 ☆ A M B ☆
      • 450 Posts
      I guess this code does work; I just needed to change $resource->(’id’) to $resource->get(’id’).
        • 8168
        • 1,118 Posts
        Hi James, What version of MODx did you get your "publishDraftCopy" plugin working on? I have just followed the instructions on https://ichosemodx.wordpress.com/2010/08/02/updating-a-published-resource-from-a-draft-copy and http://ichosemodx.wordpress.com/2010/08/03/publishing-a-resource-from-a-draft-copy-revised-now-with-tvs but it does not work on REVO 2.2.7?

        Did you get any further with developing this into a fully working solution? I'd love to chat about this as I am very keen on getting a proper CMS workflow into MODx ASAP.

        With the plugin code inputed - on save of any resource - the manager falls over and the "Saving" progress bar never disapears to show the resource has been updated...? Something not right somewhere... The Plugin code I am using is:

        <?php
        $pagetitle = $resource->get('pagetitle');
        //if the “draft” flag is in the title AND the user “published” the draft
        if (
        (preg_match('/\)draft-(?P<id>\d+)-\)/', $pagetitle, $results ))&&
        ($resource->get('published'))
        ) { 
        // the ID of the resource  to publish to
        $prodID = $results['id'];
        // if $prodID represents a real & published resource
        if ($prodResource = $modx->getObject(‘modResource’,
        array(‘id’=>$prodID,’published’=>1))) {
        $newVals = $resource->toArray();	
        
        $restructedFields = array('alias'=>",'parent'=>"); //fields not to update
        $filteredVals = array_diff_key($newVals,$restrictedFields); 
        $prodResource->fromArray($filteredVals);
        
        //take out the “draft” token before setting pagetitle
        $prodResource->set(‘pagetitle’, str_replace($results[0], ”, $pagetitle));
        $prodResource->save();
        $newTVs = $resource->getMany('TemplateVars');
        foreach ($newTVs as $TV) {
        $newVal = $TV->getValue($resource->get('id'));
        $TV->setValue($prodResource->get('id'), $newVal);
        $TV->save();
        }
        }
        
        //unpublish the draft resource
        $resource->set('published', 0);
        $resource->save();
        }
        return;
        ?>
        


        Let me know... [ed. note: dubbs last edited this post 13 years, 2 months ago.]