We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I had a pretty sweet plugin working in version 2.1.3 and earlier. It would pre-set the template for new resources based on certain conditions. It looked basically like this:

    if ($mode == 'new') {
      $parentID = (int) $_REQUEST['parent'];
      if ($parent = $modx->getObject('modResource', $parentID)) { 
          if ($tpl = $modx->runSnippet('someSnippetThatMightReturnATemplateID', array('parentID'=>$parentID))) {
    	 $_REQUEST['template'] = $tpl;  
          } 
       }
    }


    When the inner snippet returned a template number, the new resource would automagically load that template because "&template=28" (or whatever template ID number it happens to be) was automagically appended to the query string.

    Combined with template-based FC rules, this was an important part of my master plan for taking over the world.

    This worked in 2.1.3 either on the "OnDocFormPrerender" OR the "OnDocFormRender" events.

    It doesn't seem to work at all in 2.2.0-pl2, and it is RUINING MY LIFE.

    Have the behaviors of these system events changed? Or the way MODX deals with the REQUEST array? Or how it writes the query string?

    I will cry myself to sleep if I can't get this plugin working.
      • 3749
      • 24,544 Posts
      For security, that stuff is no longer in the $_GET array.

      I'm not sure what it is now, but try this:

      $scriptProperties['parent']


      or this

      $modx->getOption('parent', $scriptProperties)




      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      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
        • 14883 ☆ A M B ☆
        • 450 Posts
        I can still get the parent with my original code ($_REQUEST['parent']).

        The problem I'm having is that I cannot *set* the template value anymore with
        $_REQUEST['template'] = $tpl;

        I'll dig a little deeper into the code and see if I can figure it out.
          • 3749
          • 24,544 Posts
          Sorry, I missed that. Looking at the resource/update processor code, this should be it:

          $resource->set('template', $tpl);



          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          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
            • 14883 ☆ A M B ☆
            • 450 Posts
            Turns out it is a lot more complicated. From my experiments, the OnDocFormPrerender event is no longer in the right place within the chain of events to make this happen. I tried OnDocFormRender, OnBeforeManagerPageInit,and OnManagerPageInit, each with the several different approaches to setting the template, and none of them worked.

            I was ultimately able to recreate the plugin by making the following changes:


            • changing the event to OnHandleRequest
            • changing the "if $mode == 'new'" to "if ($_REQUEST['a'] == 47)"
            • changing "$_REQUEST['template'] = $tpl;" to "$_GET['template'] = $tpl;"

            I figured this out by working my way through several of the class files, looking at where the various events fire and where the properties are set (line 111 of modmanagerresponse.class.php).

            The new code works in 2.2, but not 2.1.3, and the old code works in 2.1.3 but not 2.2. As long as it works, I guess I don't care.

              • 3749
              • 24,544 Posts
              Glad you got it worked out. You can check the MODX version if you want to make the code more generic. wink


              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              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
                • 14883 ☆ A M B ☆
                • 450 Posts
                Quote from: BobRay at Mar 14, 2012, 06:04 PM
                You can check the MODX version if you want to make the code more generic. wink

                That's a great idea, thanks.

                FYI, I posted a blog today about what I am doing with this plugin.
                http://www.cmslesstraveled.com/index/archive/2012/setting-templates-automatically.html
                  • 3749
                  • 24,544 Posts
                  From the book:

                  getVersionData() -- Returns an array containing the version information
                  for the current MODX install.
                  
                  Array members:
                  
                  version - Current version (e.g., 2).
                  major_version - Current major version (e.g., 0).
                  minor_version - Current minor version (e.g., 1).
                  patch_level - Current patch level (e.g., beta-3).
                  code_name - Current codename (e.g., Revolution).
                  full_version - All of the above.
                  full_appname - 'MODX' . code_name | full_version (if code_name is empty).
                  



                  ---------------------------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                  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