We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5160
    • 118 Posts
    chris.dempsey78 Reply #1, 12 years ago
    Can anyone advise how to get the ID of the default Media Source in a snippet?

    I have tried:
    $modx->loadClass('sources.modMediaSource');
    $source = modMediaSource::getDefaultSource($modx);
    $basePath = $properties['basePath']['value'];


    $source is an array but $basePath has no value.

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

      • 4172
      • 5,888 Posts
      does this work?

      $modx->loadClass('sources.modMediaSource');
      $source = modMediaSource::getDefaultSource($modx);
      $source->initialize();
      $basePath = $source->getBasePath();
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 5160
        • 118 Posts
        chris.dempsey78 Reply #3, 12 years ago
        That works Bruno, thank you, however I failed to explain what I need correctly.

        My current code is:
        $mediasource = $modx->getObject('modMediaSource', array('id' => 1)); // <---- media source ID is hard coded
        $properties = $mediasource->get('properties');
        $basePath = $properties['basePath']['value'];

        It's actually the ID of the default Media Source I want, in this case 1 so that it doesn't need to be hard coded in the snippet.

        Based on your example: $basePath = $source->getBasePath();

        I am looking for something like: $id= $source->getID();

        Is there a list of properties that are available? If there is I can't find it.
          • 5160
          • 118 Posts
          chris.dempsey78 Reply #4, 12 years ago
          Is there a list of properties that are available? If there is I can't find it.
          Don't know how I missed it: http://fossies.org/dox/modx-2.3.1-pl/classmodMediaSource.html

          On 2.3.1 getBasePath() doesn't return the value of the basePath Property though, it returns the full filesystem path:


          • Expect: assets/uploads/
          • Get: C:/inetpub/vhosts/domain.com/assets/uploads/

          Seems it might be a bug though, found this on Google:
          when I manually prepend the modx base path + media source path it works and ... On the first request with empty cache $source->getBasePath() returns the full
          http://bugs.modx.com/issues/7392

          But the website isn't responding right now so I can't see the rest but it seems like someone else has experienced the same issue.
            • 4172
            • 5,888 Posts
            to get the id, I think this should work:

            $source->get('id');


            modFileMediaSource did allways return the absolute - path with

            $source->getBasePath;


            when basePathRelative was set to true, is it set to true in your mediasource - setting?

            you can also try:

            $properties = $source->getProperties();
            $basePath = isset($properties['basePath']['value'])?$properties['basePath']['value']:'';
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 5160
              • 118 Posts
              chris.dempsey78 Reply #6, 12 years ago
              modFileMediaSource did allways return the absolute - path with $source->getBasePath; when basePathRelative was set to true, is it set to true in your mediasource - setting?
              Yes, basePathRelative is true in the mediasource settings, so not a bug just lack of understanding on my part.
              $source->get('id');

              This works perfectly.

              My Plugin is now complete. It fires OnFileManagerUpload and uses Resizer to automatically resize images uploaded through the File Manager. I'll check it over and post the full code later for scrutiny and for others to use.

              Bruno - your help is invaluable, thank you!
              • discuss.answer
                • 40045
                • 534 Posts
                As the default media source id is specified in the system settings, you could just do

                $defaultsource = $modx->getOption('default_media_source');
                


                but this will just give you the id and no source object, you would have to get that with some code like posted in the other comments here!
                  • 5160
                  • 118 Posts
                  chris.dempsey78 Reply #8, 12 years ago
                  Even better, it is just the ID I need.

                  For some reason my brain was telling me it was separate to the system settings since the setting is edited through the Media Sources view in the Manager.

                  Still, I learned how to get it as an object which may be useful in future.

                  Thanks again both, very helpful. Full copy of plugin code to follow.