We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18913
    • 654 Posts
    Not sure where to place this question, so I’ll try here...

    I’d like to write a simple plugin that would add a line of javascript to the "head" section of each page offered up when one is logged in as a manager via the backend.

    I’ve looked at existing code and think it ought to be something like this :
    global $e;
    $e = &$modx->Event;
    
    switch ($e->name) { 
    
      case 'OnDocFormRender':
    		echo '		
    			<script type="text/javascript">
    			var $j = "This is a test";
    			</script>	
    		';
        break;
      case 'orsomeotherevent' :
        break;
    
      default :
        break;
    } // end switch    
    


    But I can’t seem to get anything to show up in the frame source when enabling this (yes, ’OnDocFormRender’ is checked on the Events tab). And I’m pretty sure that the above code has problems anyway.

    So - can anyone give me a few pointers as to how I’d get a bit of code to work? The idea is to try to enable the NicEditor mentioned in another thread.

    EDIT : Note, this is for Evo 1.0.4 and not Revolution ...

    Thanks in advance,
    Matt
      • 5340
      • 1,624 Posts
      works for 1.0.2
      I will try it for 1.0.4 tomorrow.

      I changed the code to var $j = "This is a test";alert($j); and I get the alert.
      Make sure the plugin is not disable.

      BTW it only works when you click to edit a document in the tree. It doesn’t work if you want to see the resource overview or if you want edit a template, etc
        • 3749
        • 24,544 Posts
        Have you tried $modx->regClientStartupScript() ?
          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
          • 18913
          • 654 Posts
          @cipa : thanks for that test. I hadn’t put in the alert and was expecting to see the code inserted near the top. Instead, it shows up further along in the page source.

          What ends up working (as a first pass, PoC sorta thing) is this :
          // Handle event
          global $modx;
          $e = &$modx->Event; 
          switch ($e->name)
          {
            //Event triggers :
            //OnBeforeManagerPageInit,OnChunkFormRender,OnDocFormRender,
            //OnSnipFormRender,OnTVFormRender,OnTempFormRender
          
              default :
                  echo '<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
                        <script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>';
                  break;
           
          }


          I used that structure to allow for some possible customization based on context. But looking into it a bit further, it’s unclear to me if the options I really want are available (e.g. table editing, "raw" text mode). Also, I’d need to figure out the correct way to pass customization parameters. And if the ones I want aren’t available to begin with, then this is a deadend.

          @BobRay - yes, I did try that. But it only gets inserted if you are not in the manager. If this little editor could do what I wanted, then I think you are right, that would be the approach to try to use on the front-end.

          Thanks to both of you for your help,
          Matt

            • 3749
            • 24,544 Posts
            You’re right about regClientStartupScript().

            Have you tried putting a placeholder in the template for the Manager page and setting it in your code?

            Also, I think you’d want to connect it to "OnxxxFormPrerender instead of the render events.

            BTW, if you’re going to respond the same way for all the events the plugin is connected to, you don’t need to identify the event or have a switch statement.

            Also,
              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
              • 18913
              • 654 Posts
              @BobRay - No, I didn’t play around with the manager template and placeholders. Maybe I’ll look into that.
              And that’s a good thought re: the "Prerender" events. Thanks...
              Matt