We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44793
    • 19 Posts
    Within a snippet I am attempting to look up MIME type values for a specific file extension (as specified via System > Content Types). I discovered the $modx->contentTypes property, but it seems to be null. Is this by design or is it supposed to contain values?

    Since it is null, I am using the following code to retrieve the content types:
    if (is_null($modx->contentTypes))
        $modx->contentTypes = $modx->getCollection('modContentType');
    


    Is this the best way to access content types, or is there a better or more efficient way?

    Thanks,
    Mike
      • 3749
      • 24,544 Posts
      Apparently it is. I looked in the code and it appears that the $contentTypes variable is never set. Also, the content type getlist processor does just what you're doing.

        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
        • 44793
        • 19 Posts
        Cool, thank you. I looked in the code and that appears to be the case.

        Another option for my scenario might be something like what the getFileContentType method of the modImport class does. [ed. note: last edited this post 13 years, 1 month ago.]
          • 44793
          • 19 Posts
          Of course, one would need to make sure extensions passed to the query are sanitized if using or adapting getFileContentType.
            • 44793
            • 19 Posts
            Here's an example of getting a specific content type with a sanitized extension for MySQL:
            $extEscaped = preg_replace('/([%_])/', '\\\\$1', $extension);
            $contentType = $modx->getObject('modContentType', array('file_extensions:LIKE' => '%'.$extEscaped.'%'));
            
            [ed. note: last edited this post 13 years, 1 month ago.]