We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38720
    • 23 Posts
    I'm trying to write a plugin. The code works as a snippet but not a plugin.
    For example, can I use "runSnippet" in a plugin?

    I'm using Revo 2.2.4pl

    Example for OnDocFormSave:

    $params['startId'] = '0';
    $params['level'] = '3';
    $output = $modx->runSnippet('Wayfinder', $params));

    When I save a resource, the save "loading" keeps going and going....seems like anytime I used $modx->somethinghere it does not work.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      What, exactly, do you want to accomplish? Your plugin code would need to be appropriate to the event.

      For example, the first sample at http://rtfm.modx.com/display/revolution20/Plugins works with onBeforeDocFormSave, but does nothing using onDocFormSave.
        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
        • 38720
        • 23 Posts
        I just want to save Wayfinder output to a file everytime a document is saved. The barebones:

        $output = $modx->runSnippet('Wayfinder',array(
        'startId' => '0',
        'rowTpl' => 'mainnav.rowTpl',
        'innerTpl' => 'mainnav.innerTpl',
        'innerRowTpl' => 'mainnav.innerRowTpl',
        'rowIdPrefix' => 'item',
        'excludeDocs' => '1,3,4,5,6',
        'level' => '3',

        ));

        $file = $_SERVER['DOCUMENT_ROOT']."/assets/file.html";
        file_put_contents($file,$output);

        How can I get that to work as a plugin? TIA
          • 3749
          • 24,544 Posts
          Wayfinder sends its output to the screen, which you can't to in a plugin.

          I think this would work:

          Add this property to the Wayfinder properties array:

          'ph' => 'w_output',


          That will tell WF to put the output in a placeholder named 'w_output' rather than echoing it.

          Then get it with this:

          $output = $modx->getPlaceholder('w_output');



          ------------------------------------------------------------------------------------------
          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
            • 38720
            • 23 Posts
            Really odd, does not work, keeps hanging at the Please Wait.. saving...

            $output = $modx->runSnippet('Wayfinder',array(
               'startId' => '0',
               'level' => '3'
               'ph' => 'w_output',
               
            ));
            
            $html = $modx->getPlaceholder('w_output');
            $file = $_SERVER['DOCUMENT_ROOT']."/occ/assets/cache/navigation.html";
            if(file_exists($file)){
            	file_put_contents($file, $html);
            }
            
            [ed. note: javster last edited this post 14 years ago.]
              • 3749
              • 24,544 Posts
              Does it still crash if you comment out the last two lines? If not, that would indicate that your path is wrong.

              Also, try removing the underscore in the placeholder name in both places.

              ------------------------------------------------------------------------------------------
              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
                • 39932
                • 483 Posts
                Fuzzical Logic Reply #7, 14 years ago
                It amazes me that it echoes the output instead of returning it. This goes directly against MODx "best practices" for Snippets... yet this is a crucial extra.

                I just want to save Wayfinder output to a file everytime a document is saved.

                Amazing! Thanks for the awesome idea! This could save a ton of server load. ... Unfortunately, it has the drawback of not changing all other documents when the menu might change. ... I might look at a way to bridge this...
                  Website: Extended Dialog Development Blog: on Extended Dialog
                  Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                  Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                  Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                  • 38720
                  • 23 Posts
                  Quote from: BobRay at Sep 20, 2012, 12:11 AM
                  Does it still crash if you comment out the last two lines? If not, that would indicate that your path is wrong.

                  Also, try removing the underscore in the placeholder name in both places.

                  It runs if I take this out
                  $output = $modx->runSnippet('Wayfinder',array(
                     'startId' => '0',
                     'level' => '3'
                     'ph' => 'woutput',
                  ));
                  

                    • 3749
                    • 24,544 Posts
                    I could well be wrong about Wayfinder's behavior in Revolution, in fact now that I think about it, I'm sure I am. wink


                    BTW, this is handy for debugging plugins:

                    function my_debug($message, $clear = false) {
                        /* @var $chunk modChunk */
                        global $modx;
                    
                        $chunk = $modx->getObject('modChunk', array('name'=>'debug'));
                        if ($clear) {
                            $content = '';
                        } else {
                            $content = $chunk->getContent();
                        }
                        $content .= $message;
                        $chunk->setContent($content);
                        $chunk->save();
                    }


                    You need to create a chunk called 'debug'

                    Just do something like this:

                    my_debug("\nHTML: " . $html . "\nFILE: " . $file, true);



                    ------------------------------------------------------------------------------------------
                    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
                      • 3749
                      • 24,544 Posts
                      OK, now I'm sure I was wrong:

                      Wayfinder code:

                      /* output results */
                      if ($wf->_config['ph']) {
                          $modx->setPlaceholder($wf->_config['ph'],$output);
                      } else {
                          return $output;
                      }


                      I can't see any reason why either of your methods wouldn't work.


                      ------------------------------------------------------------------------------------------
                      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