We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    Here is a simple plugin to automatically redirect top-level parents with children to the first child (it is the same as adding FirstChildRedirect to the top-level parent). If top-level has no ’active’ children than it will do nothing.

    The most accurate name would have been AutoFirstChildRedirectIfTopLevel...but that was too long so autoFolders it is grin

    Create a new plugin, paste in this code and select OnParseDocument system event...and save.

    // <?php
    // automatically redirect to first child from top-level document.
    // based on FirstChildRedirect snippet
    
    $id = $modx->documentIdentifier;
    $parent = $modx->documentObject['parent'];
    
    // check if top-level
    if($parent === '0'){
        // check if has active children
        $children= $modx->getActiveChildren($id, 'menuindex', 'ASC');
        if((!$children === false) && ($children['0'] != '')){
            $firstChildUrl = $modx->makeUrl($children['0']['id']);
            $modx->sendRedirect($firstChildUrl);
        } 
    }
    
    return;


    Can improve this in many ways...for example can have it check if there is content in the parent and only redirect if not, or can go deeper in the menu, or can have a TV do override the action. wink
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don&#39;t know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 16183
      • 1,390 Posts
      Shane,

      Thx for sharing. This will be useful.

      I got thrown off by the title too....thought it would auto create folders...but it’s a plugin so it won’t create nothing. It’s Evo only, I presume...Maybe important to add since I anticipate that question coming up..

      Would an override work in a situation whereby, say, I want the redirect to happen on the main menu but not on the side menu?

      cheers/k
        • 7231
        • 4,205 Posts
        I renamed the post to help keep it clear wink

        Yes, we could do all sorts of thins with this...it is a starting point. Not sure about your example though...the override would be global (as far as I could tell). This is in the ’in development’ forum so that we can build it up into something more useful. I made it only do top-level since if the first child was also a container it would jump straight in to that document as well...which is not ideal.

        The idea of an override was to allow some docs to be containers with content while others be folder like. I think that simply checking for content in the content field may be a nice ’automated’ method for bypassing.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 16183
          • 1,390 Posts
          Thx for the clarification....That’s the kinda thought I had about the override....Gonna test this sometime. This plugin approach seems to be easier to implement compared to the snippet...do it once and that’s it.

          cheers/k
            • 7231
            • 4,205 Posts
            another idea is to approach this the other way around...add a tv that would indicate the it is a folder rather than to say it isn’t one. It is just an idea. For smaller sites it would be fine the way it is...but for larger sites you may need the extra control. Yes, it is much easier than the snippet...specially since it is not obvious that that snippet exists for new users wink
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 26931
              • 2,314 Posts
              cool smiley thanks for sharing!

              i did use the TV Conditional Snippet so far to give FirstChildRedirect a bit more logic, for every page i want to have a redirect, i just tick-off a radio button to redirect that page