We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22656
    • 121 Posts
    hmm, when deleting it doesnt seem to delete the cloned pages. i did uncomment the line from code above, ie

    $docAlias = $resource->get('alias');
        case OnResourceDelete:
      
            $docs = $modx->getCollection('modResource', array('alias' => $docAlias));
      
            foreach ($docs as $doc) {
                $doc->delete();
            }
            break;


    i've also tried getting the alias from following, no joy

     $e = $modx->Event;
            $docAlias = $e->params['alias'];
      • 3749
      • 24,544 Posts
      Hmm... try this:

      $foreach($docs as $doc) {
          $c = $doc->get('context_key');
          $doc->delete();
          $modx->reloadContext($c);
      }
      


      You may also have to refresh the tree.


      If that doesn't work, the next step would be writing debugging messages to the log or to a chunk and checking to see if $dosAlias is getting set correctly and if getCollection() is finding the objects.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 22656
        • 121 Posts
        Mkay, i've managed to get it to work with "remove", but it doesnt want to work with "delete" for some reason...

        I've also changed from getcollection to getobject - the clone will always contain one page. Getcollection has a problem searching across contexts it seems.... I had to specify the context to be searched for and then that seems to work.

        also, potential can of worms as I need to take all actions into consideration as when someone deletes a resource and then undeletes, it should undelete the clones...lol

        also, if someone gives an alias to something which already has an alias in web context (duplicate), by clicking delete with "getcollection" it is likely it would delete that duplicate which its not meant to do, correct? can of worms! lolz

        /*CreateSymlinks Plugin */
        //$docId = $resource->get('id');
        //$docAlias = $resource->get('alias');
         
        /* The numbers on the right here are the respective template IDs
         * The left members should be the names of your contexts 
         */
        //$modx->log($modx->LOG_LEVEL_ERROR,'some string');
                
        $contexts = array(
            'context1' => 13,
            'context2' => 13,
        );
         
        $fields = array(
           'docAlias' => $docAlias,
           'pageTitle' => $resource->get('pagetitle'), /* not sure if this is necessary */
            );
        
        
         
        switch ($modx->event->name) {
            case OnResourceDelete:
                    
                $docAlias = $resource->get('alias');
                $resource =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context1'));
                $resource_context = $resource->get('context_key');
                $resource1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context2'));
                $resource1_context = $resource1->get('context_key');
                
               
                $resource->remove();
                $modx->reloadContext($resource_context);
                $resource1->remove();
                $modx->reloadContext($resource1_context);
                
        
         
            case OnDocFormSave:
                $e = $modx->Event;
                $docId = $e->params['id'];
                $docAlias = $e->params['alias'];
                $pageTitle =  $resource->get('pagetitle');
                $pageAlias =  $resource->get('alias');
                
        
                if ($mode === modSystemEvent::MODE_NEW) {
                    foreach($contexts as $contextName => $templateId) {
                        $fields['context_key'] = $contextName;
                        $fields['template'] = $templateId;
                        $symlink = $modx->newObject('modSymlink', $fields);
                        $symlink->setContent($docId); 
                        $symlink->set('pagetitle', $pageTitle);
                        $symlink->set('alias', $pageAlias);
                        $symlink->set('published',true);
                        //$symlink->set('parent',$parentid);
                        $symlink->save();
                    }
                        
                }
                break;
        }
        [ed. note: papachumba last edited this post 14 years, 4 months ago.]
          • 3749
          • 24,544 Posts
          Responding to the behavior of actual users is always a can of worms. wink


          BTW, it occurred to me that if delete() isn't working, you could do this:


          $resource->set('deleted', '1');
          $resource->save();


          Some of your trouble may be the result of using $resource as a variable name and assigning a value to it.There's already a $resource available in both events (the current resource), it's set automatically by MODX, and it shouldn't be reassigned.

          One more point: In your second case statement, you may also need to do something like this when creating the Symlinks in the other Contexts in order for each Symlink to be properly attached to the Context:

          $cr = $modx->newObject('modContextResource');
          $cr->set('context_key', $contextName);
          $cr->set('resource', $resourceId);
          $cr->save();
          



          You could probably avoid that if you called the Symlink/Create processor with $modx->runProcessor() instead of using newObject(), but I'm not sure of the details.



          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 3 months ago.]
            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
            • 22656
            • 121 Posts
            Lol, I can only imagine whats it like with a forum full of kittens screaming for help!

            I came across $resource->set('deleted', '1') yesterday, I was scouring the forums for example code and finally stumbled across it somewhere. I have also realised i shouldnt be using the same $resource variable and changed this, however I've ran into different problems.

            If I add OnUndelete switch statement, OnDelete stops working. If I put one above the other the issue gets reversed and the other stops working?
            UPDATE - i've managed to get this to work by putting the "undelete" code into another plugin and attaching that to undelete action, this seems to fix it and all pages get deleted/undeleted nicely.

            Also, I attempted to add another if statement for when the page gets updated (modSystemEvent::MODE_UPD??), not saved for the first time, to correct aliases and pagetitles in clones in case they changed. When I hit save with this deployed, the loading bar loads forever:

            case OnResourceUndelete:   
                    $docAlias = $resource->get('alias');         
                    $clone1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'saga'));
                    $clone1->set('deleted',false);
                    $clone1->save();
                    $clone2 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'aa'));
                    $clone2->set('deleted',false);
                    $clone2->save();
            
                case OnResourceDelete:            
                    $docAlias = $resource->get('alias');
                    $clone1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'saga'));
                    $clone1->set('deleted',true);
                    $clone1->save();
                    $clone2 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'aa'));
                    $clone2->set('deleted',true);
                    $clone2->save();
             
                case OnDocFormSave:
                    $e = $modx->Event;
                    $docId = $e->params['id'];
                    $docAlias = $e->params['alias'];
                    $pageTitle =  $resource->get('pagetitle');
                    $pageAlias =  $resource->get('alias');
            
                    if ($mode === modSystemEvent::MODE_NEW) {
                        foreach($contexts as $contextName => $templateId) {
                            $fields['context_key'] = $contextName;
                            $fields['template'] = $templateId;
                            $symlink = $modx->newObject('modSymlink', $fields);
                            $symlink->setContent($docId); 
                            $symlink->set('pagetitle', $pageTitle);
                            $symlink->set('alias', $pageAlias);
                            $symlink->set('published',true);
                            //$symlink->set('parent',$parentid);
                            $symlink->save();
                        }
                            
                    }
                    if ($mode === modSystemEvent::MODE_UPD) {
                    $clone1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'saga'));
                    $clone1->set('pagetitle', $pageTitle);
                    $clone1->set('alias', $pageAlias);
                    $clone1->save();
                    $clone2 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'aa'));
                    $clone2->set('pagetitle', $pageTitle);
                    $clone2->set('alias', $pageAlias);
                    $clone2->save();
                    }
                    break;
            [ed. note: papachumba last edited this post 14 years, 3 months ago.]
              • 22656
              • 121 Posts
              also, im not quite understanding this

              $cr = $modx->newObject('modContextResource');
              $cr->set('context_key', $contextName);
              $cr->set('resource', $resourceId);
              $cr->save();


              vs this

               $fields['context_key'] = $contextName;
                              $fields['template'] = $templateId;
                              $symlink = $modx->newObject('modSymlink', $fields);
                              $symlink->setContent($docId); 
                              $symlink->set('pagetitle', $pageTitle);
                              $symlink->set('alias', $pageAlias);
                              $symlink->set('published',true);
                              $symlink->save();


              the second option which is in my code saves a clone in the required context beautifully?
                • 22656
                • 121 Posts
                One more update, all works.
                Creating a page will create the same page with title & alias in secondary contexts.
                Deleteing a page will delete clones from secondary contexts.
                Renaming a page will rename clones from secondary contexts.

                The only issue left - renaming the alias field in original is bound to produce unexpected results. After you change the alias and hit save - there is no way to retrieve the "old" alias name so that clones can be matched from secondary contexts. Is there a way to get both old field and new field when saving a resource?

                Anyway, this is what I have:

                On resourceDelete, ondocformsave:
                <?php
                /*CreateSymlinks Plugin */
                //$docId = $resource->get('id');
                //$docAlias = $resource->get('alias');
                 
                /* The numbers on the right here are the respective template IDs
                 * The left members should be the names of your contexts 
                 */
                //$modx->log($modx->LOG_LEVEL_ERROR,'some string');
                        
                $contexts = array(
                    'context1' => 13,
                    'context2' => 13,
                );
                 
                $fields = array(
                   'docAlias' => $docAlias,
                   'pageTitle' => $resource->get('pagetitle'), /* not sure if this is necessary */
                    );
                
                
                 
                switch ($modx->event->name) {
                
                    case OnResourceDelete:            
                        $docAlias = $resource->get('alias');
                        $clone1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context1'));
                        $clone1->set('deleted',true);
                        $clone1->save();
                        $clone2 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context2'));
                        $clone2->set('deleted',true);
                        $clone2->save();
                 
                    case OnDocFormSave:
                        $e = $modx->Event;
                        $docId = $e->params['id'];
                        $docAlias = $e->params['alias'];
                        $pageTitle =  $resource->get('pagetitle');
                        $pageAlias =  $resource->get('alias');
                
                        if ($mode === modSystemEvent::MODE_NEW) {
                            foreach($contexts as $contextName => $templateId) {
                                $fields['context_key'] = $contextName;
                                $fields['template'] = $templateId;
                                $symlink = $modx->newObject('modSymlink', $fields);
                                $symlink->setContent($docId); 
                                $symlink->set('pagetitle', $pageTitle);
                                $symlink->set('alias', $pageAlias);
                                $symlink->set('published',true);
                
                                //$symlink->set('parent',$parentid);
                                $symlink->save();
                            }
                                
                        }
                
                        if ($mode === modSystemEvent::MODE_UPD) {
                        $clone1 = $modx->getObject('modResource',array('alias' => $pageAlias,'context_key' => 'context1'));
                        $clone1->set('pagetitle', $pageTitle);
                        $clone1->save();
                        $clone2 =  $modx->getObject('modResource',array('alias' => $pageAlias,'context_key' => 'context2'));
                        $clone2->set('pagetitle', $pageTitle);
                        $clone2->save();
                        }
                        break;
                }


                onresourceUndelete:
                <?php
                
                switch ($modx->event->name) {
                
                    case OnResourceUndelete:
                        $docAlias = $resource->get('alias');
                        $clone1 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context1'));
                        $clone1->set('deleted',false);
                        $clone1->save();
                        $clone2 =  $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context2'));
                        $clone2->set('deleted',false);
                        $clone2->save();
                        break;
                }
                [ed. note: papachumba last edited this post 14 years, 3 months ago.]
                  • 3749
                  • 24,544 Posts
                  On changing the alias, I've done something similar, but I can't remember exactly how. Take a look at the ActivationEmail extra. It responds when the user's active status changes. I think you could do something similar with the alias.

                  You might also be able to save a $_SESSION variable with the old alias in OnDocFormPrerender and compare it to the alias in OnDocFormSave.


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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
                    • 22656
                    • 121 Posts
                    tx for the pointer bobray, that sounds doable.
                    Any idea why the above code had to be split up into 2 plugins instead of 1? OnResourceDelete and OnResourceUndelete do not seem to like each other?
                      • 3749
                      • 24,544 Posts
                      I don't know why you'd need to split those into separate plugins, but you shouldn't need the switch statement in the second one since it's only attached to one event (unless there's a bug that makes it get called for both events).


                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        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