We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28000 ☆ A M B ☆
    • 397 Posts
    I have a project where I need to write a folder structure that is context_key dependent. I created a plugin that is triggered by the OnContextSave event and based on the new context_key name, it writes a specific folder structure.

    My question is how can I use the MODX API to write these folders. My prototype just uses the php mkdir command, but I am sure that it would be best to do this with maybe XPDO->writeTree or something from the API.

    I am having a hard time following the MODX documentation to come up with a solution. Anyone have an idea on the best practice here?

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

      Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 28000 ☆ A M B ☆
        • 397 Posts
        Yeah, I tried to implement that but with no success...

        $directory = $modx->fileHandler->make($path); Did nothing....
          Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You called the fileHandler service first?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I see. I tried it out and didn't get anywhere with it. Will try to find out what the story is with it and get back to you.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 28000 ☆ A M B ☆
              • 397 Posts
              Hi Susan,

              That would be fantastic!!
                Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
                • 3749
                • 24,544 Posts
                I can't think of any reason not to just use mkdir(). Maybe I'm missing something.

                Using the API to create and update MODX objects in the database makes sense. Files are just files, though, and really have nothing to do with MODX or the database.

                If you wanted to coordinate the folders with MODX Media Sources, you could do that with the API, but even then, it would still make sense use native PHP methods to create the files and folders (which is all that writeTree does).

                The writeTree() method would be handy if it took a nested array of directories as an argument and created them. Instead, though, it takes a single directory path string, which makes it fairly inefficient for creating a tree of directories. Using mkdir($path, $dirPermission, true) will do the same thing, and probably much faster.
                  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
                  • 28000 ☆ A M B ☆
                  • 397 Posts
                  Yeah, that is the question in general. I thought there may have been a recommended best practice when using MODX to write folders to the server. I was thinking that it would be safer or more server compatible to let MODX handle things such as read/write permissions when creating a directory.
                    Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    The main purpose of the core of MODX is to do database stuff. Most of the file management that it does is for internal use, not to use as an API.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 3749
                      • 24,544 Posts
                      There are references in the code to new_file_permissions and new_folder_permissions System Settings and there are lexicon strings for them, but in my installations, those settings don't exist and I've never seen them set.
                        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