On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • How can I get the path of a Media Source? The object has a getBasePath method, but how can I get a modMediaSource object?
    $source = $modx->getObject('modMediaSource', 2);
    $path = $source->getBasePath();
    

    produces a PHP error:
    PHP Fatal error: Call to a member function sanitizePath() on a non-object in /Applications/MAMP/htdocs/revo228/core/model/modx/sources/modfilemediasource.class.php on line 43
    It wants a fileHandler, but why do I need a fileHandler object if all I want to do is get the base path of the Media Source?

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

    [ed. note: sottwell last edited this post 6 years, 10 months ago.]
      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
    • discuss.answer
      Finally figured it out. Can't use the getBasePath method on the object. This works. Taken from core/model/modx/sources/modfilemediasource.class.php around lines 41 and 44.
      $source = $modx->getObject('modMediaSource', 2);
      $properties = $source->getProperties();
      $path = $properties['basePath']['value'];
      return $path;
        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
      • I think this would have worked:


        $source = $modx->getObject('modMediaSource', 2);
        $path = $source->getBasePath($source);


        It looks like getBasePath() requires an object argument:

         public function getBasePath($object = '') {
                $bases = $this->getBases($object);
                return $bases['pathAbsolute'];
            }
        [ed. note: BobRay last edited this post 6 years, 6 months ago.]
          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
        • I shall certainly try that.

          Although it turns out that getting this into the content of a Static Resource during parsing is going to be tricky, if it's possible at all.
            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
          • I've tried everything I can find that uses getBasePath, and nothing at all works. It just produces a blank page. No MODX errors, no PHP errors, nothing. The getProperties way works fine, though.
              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
            • @sottwell Very neat code.

              For elFinder I am now doing something like:
              $source = $modx->getObject('modMediaSource', $id);
              $properties = $source->getProperties();
              $prop = $properties[$property]['value'];
              return $prop;


              How do you check if user has permission to view Media Source?

              UPDATE I am lookig at https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.haspermission
                TinymceWrapper: Complete back/frontend content solution.
                Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
              • I believe it would be somethings like this (if it's the current user, you can omit the second and third arguments):

                $allowed = $source->checkPolicy('view', null, $userObject);
                


                This might speed it up slightly (if it works -- I'm not sure):

                $allowed = $source->checkPolicy('view', array('sources.modAccessMediaSource'), $userObject);
                

                From the core\model\modx\sources\modmediasource.class.php file:
                /**
                     * Allow overriding of checkPolicy to always allow media sources to be loaded
                     *
                     * @param string|array $criteria
                     * @param array $targets
                     * @param modUser $user
                     * @return bool
                     */
                    public function checkPolicy($criteria, $targets = null, modUser $user = null) {
                        if ($criteria == 'load') {
                            $success = true;
                        } else {
                            $success = parent::checkPolicy($criteria,$targets,$user);
                        }
                        return $success;
                    }
                
                  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