We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36788
    • 75 Posts
    Hi I'm trying to get content-type settings for html type of files from inside the snippet. What is the best way or function to do this?
      • 3749
      • 24,544 Posts
      I'm not sure I understand your question, but this may help. For the current resource, you can use:

      $modx->resource->get('contentType');



      or if you have another resource object in a variable called $doc:

      $doc->get('contentType');


      The 'contentType' field gives the content type as text (e.g. 'text/html'). There is also a 'content_type' field which contains an integer code for the content type.
        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
        • 22303 MODX Staff
        • 10,725 Posts
        The contentType field is deprecated in Revolution (a leftover from Evolution). You want to get the mime_type from the modContentType object...
        $mimeType = $modx->resource->getOne('ContentType')->get('mime_type');
          • 3749
          • 24,544 Posts
          Does the content_type field contain the ID of a modContentType object then?

          I was confused by the fact that some MODX code and the code of a number of extras appear to still use (or at least set) the 'contentType' field.
            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
            • 36788
            • 75 Posts
            Quote from: opengeek at Nov 02, 2013, 07:20 PM

            $mimeType = $modx->resource->getOne('ContentType')->get('mime_type');

            Do I understand correctly that this will return the ContentType for particular resource?

            What I need is to get the system setting of file extension for text/html types of file. According to the settings mentioned in the file I've attached in the first post this will be the back slash (/).

            Need to get this setting to generate proper URLs for custom documents stored in migx db.
              • 3749
              • 24,544 Posts
              Have you tried this?

              $url = $modx->makeUrl($modx->resource->get('id'), "", "", "full");


              If that doesn't work, this may:

              $ext = $modx->resource->getOne('ContentType')->get('file_extensions');


              The default extension for text/html is .html, though some users leave it empty and some may use a '/' extension. It's not a System Setting (you may be thinking of the container_suffix System Setting, which is '/' by default. The extension is stored in the 'file_extensions' field of the modContentType object itself.
                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
                • 36788
                • 75 Posts
                Hi, BobRay!

                Thank you for helping me, and sorry for my English))

                I will try to explain in other words)) If you are in the admin panel, then in the top menu choose system->types of content (it is below dictionary setting and above the contexts setting, I'm translating from russian so the name could vary slightly).

                You will see mime types and file extensions. This extension is used as url suffix for child documents in modx. So if you put ".html for text/html types of files the url will be yoursite.com/mainpage.html and if you change it to "/" the url will be changed to "yoursite.com/mainpage/"

                I attached the screenshot with the setting I need to get access.


                I need to get the setting from this tab for text/html files.

                I'm not able to use the function you've mentioned as it requires the modResource, so it should be used as follows:
                $resource = $modx->getObject('modResource', 1);
                $output = $resource->getOne('ContentType')->get('file_extensions');
                echo $output;


                as soon as I have resources stored in the own table this will not work: $output = $resource->getOne('ContentType')->get('file_extensions');((

                  • 3749
                  • 24,544 Posts
                  It looks like some of your code at the end of your message got lost because it wasn't in code tags.

                  Are you storing the "resources" in some custom table and not in the modx_site_content table?

                    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
                    • 36788
                    • 75 Posts
                    Quote from: BobRay at Nov 04, 2013, 09:17 PM
                    It looks like some of your code at the end of your message got lost because it wasn't in code tags.

                    Are you storing the "resources" in some custom table and not in the modx_site_content table?


                    Yes, I have a custom table to store shop resources. And I just need to read the property set in " system -> types of content ->html/text ->file extentions", to generate the correct URL suffix. By default this will be .html, but user could change it for "/" for example.
                      • 3749
                      • 24,544 Posts
                      Did you create a schema for your table and generate class and map files for it so it can be used with xPDO? Does the class for your table extend modResource?

                      That would all be necessary in order for the getOne() and getObject() calls to work.

                      See if your getObject() and getOne() calls are working. The return value will be null if they're not working.

                      Also, this code:

                      $resource = $modx->getObject('modResource', 1);


                      will get the Resource with the ID of 1. That's very unlikely to be the ID of a resource in your custom table unless there are no other resources on the site or you're generating the IDs yourself (which you shouldn't do -- they should be autoincrement IDs so the regular resources combined with the resources in your table all have unique IDs).
                        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