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.
-
☆ 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.
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
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:
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
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
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...
I could well be wrong about Wayfinder's behavior in Revolution, in fact now that I think about it, I'm sure I am.
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
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