<![CDATA[ OnDocFormSave and a simple internal function - what's going wrong here ... - My Forums]]> https://forums.modx.com/thread/?thread=104326 <![CDATA[OnDocFormSave and a simple internal function - what's going wrong here ...]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561142
at the moment I don't understand modx, because the following minimal code example
did not work. On pressing the Save-Button in a Resource the system hangs:

<?php
$PlugName = "TestPlugIn";

// ****** Internal Functions ******

function MyInternalFn($id) {
  $test=$id;
}


// ****** Handle MODX Events ******

switch ($modx->event->name) {
    // ****** OnDocFormRender with $mode $resource $id *********************************************************************************************
    case 'OnDocFormRender':
        $modx->log(MODX_LOG_LEVEL_INFO, "[$PlugName -> ".$modx->event->name."] ID: ($id)   Mode:($mode)");
       break;
    // ****** OnBeforeDocFormSave with $mode $resource $id  ****************************************************************************************
    case 'OnBeforeDocFormSave':
        $modx->log(MODX_LOG_LEVEL_INFO, "[$PlugName -> ".$modx->event->name."] ID: ($id)   Mode:($mode)");
        break;
    // ****** OnDocFormSave with $mode $resource $id ***********************************************************************************************
    case 'OnDocFormSave':
        $modx->log(MODX_LOG_LEVEL_INFO, "[$PlugName -> ".$modx->event->name."] ID: ($id)   Mode:($mode)");
        break;
    // ****** OnBeforeEmptyTrash with $ids *********************************************************************************************************
    case 'OnBeforeEmptyTrash':
        $modx->log(MODX_LOG_LEVEL_INFO, "[$PlugName -> ".$modx->event->name."] Anzahl zu löschender IDs: (".count($ids).")");
        break;
    default: break;
}


There are to ways, the system doesn't hang anymore:
1) Remove the function MyInternalFn or
2) Remove the system event 'OnDocFormSave' for this PlugIn
Both variants I don't understand, because the code
should work ...

Thanks
Martin
]]>
spechto Aug 27, 2018, 02:43 AM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561142
<![CDATA[Re: OnDocFormSave and a simple internal function - what's going wrong here ...]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561159 ]]> BobRay Aug 27, 2018, 04:26 PM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561159 <![CDATA[Re: OnDocFormSave and a simple internal function - what's going wrong here ... (Best Answer)]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561150 and the system works WITHOUT a hanger smiley
Without 'OnDocFormSave' it has worked with functions because
the Plugin code was run only once, but with 'OnDocFormSave'
and 'OnBeforeDocFormSave' the code is called twice in the same
call ...

// ****** Internal Functions ******

if (!function_exists('MyInternalFn')) {
  function MyInternalFn($id) {
    $test=$id;
  }
}
]]>
spechto Aug 27, 2018, 10:34 AM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561150
<![CDATA[Re: OnDocFormSave and a simple internal function - what's going wrong here ...]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561149 calls the PlugIn code in conjunction with "OnDocFormSave" and "OnBeforeDocFormSave".
At first I splitted the Code in two PlugIns and set the correct event for the plugin:
function MyInternalFn() { $test=0; }
if ($modx->event->name == 'OnBeforeDocFormSave') { $test=1; }

function MyInternalFn() { $test=0; }
if ($modx->event->name == 'OnDocFormSave') { $test=1; }

This code will cause a system hanger. When changing the both functions names in two
different names, it works. I think the problem is the redeclaration of the function MyInternalFn().]]>
spechto Aug 27, 2018, 10:16 AM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561149
<![CDATA[Re: OnDocFormSave and a simple internal function - what's going wrong here ...]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561147 the 4 events and the problem is there (Revo 2.6.5, PHP 7.0 and 7.2).
The hanger can be removed by "removing the unused function" or
"remove the event handler for the OnDocFormSave".

This is a more reduced version, but the problem is still there ...

<?php
$PlugName = "TestPlugIn";

function MyInternalFn() {
  $test=0;
}

switch ($modx->event->name) {
    case 'OnDocFormRender':
       break;
    case 'OnBeforeDocFormSave':
        break;
    case 'OnDocFormSave':
        break;
    case 'OnBeforeEmptyTrash':
        break;
    default: break;
}


I can't explain, why this happens ...
]]>
spechto Aug 27, 2018, 09:18 AM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561147
<![CDATA[Re: OnDocFormSave and a simple internal function - what's going wrong here ...]]> https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561143
1. Wrap all the variables in curly braces: {$plugName} {$id} (also, using -> in the string may confuse the parser).

2. Make sure you're not calling your function in events that don't send $id in the invokeEvent() call, and don't display variables that are not sent.

See this: https://bobsguides.com/modx-revolution-events.html]]>
BobRay Aug 27, 2018, 02:57 AM https://forums.modx.com/thread/104326/ondocformsave-and-a-simple-internal-function---what-s-going-wrong-here#dis-post-561143