We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44051
    • 6 Posts
    Hello together,

    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

    This question has been answered by spechto. See the first response.

      • 3749
      • 24,544 Posts
      A couple of thoughts:

      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
        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
        • 44051
        • 6 Posts
        I'll try so, but the version above pasted in a new "PlugIn", activate
        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 ...
          • 44051
          • 6 Posts
          So, I tested a little bit and I think that the problem is how modx
          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().
          • discuss.answer
            • 44051
            • 6 Posts
            Here is the (or a) solution. So the function can't redeclared
            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;
              }
            }
            
              • 3749
              • 24,544 Posts
              Duh. I should have thought of that.
                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