We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38850
    • 110 Posts
    Quick question: A resource has "content_type" but this only returns an integer like "1" for text/html MIME type. My question is, how do I access the various sub-data that is in the content type?

    I have looked high and low and dug deep into the API documentation to try and find out how to access the values within the content type but I can't find it.

    So given a content_type of "1", how do I now retrieve the name, file extension or MIME type? Are there getters and setters for these? I want to access it from a plugin for a currently-opened resource.

    Thanks

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      This will get you the modContentType object and its name:

      $contentType = $resource->getOne('ContentType');
      $name = $contentType->get('name');
      

      The other field names are listed here: http://bobsguides.com/modx-object-quick-reference.html#modContentType

      If you want the content type for the current resource in the front end, use:

      $contentType = $modx->resource->getOne('ContentType');



        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
        • 38850
        • 110 Posts
        Quote from: BobRay at Oct 10, 2014, 05:52 PM
        This will get you the modContentType object and its name:

        $contentType = $resource->getOne('ContentType');
        $name = $contentType->get('name');
        

        The other field names are listed here: http://bobsguides.com/modx-object-quick-reference.html#modContentType

        If you want the content type for the current resource in the front end, use:

        $contentType = $modx->resource->getOne('ContentType');





        Just a followup on this. While your answer solved the question, I have a few notes for future readers.

        I didn't need the "name" as in Bob's example, I needed the file extension, but found it funny that it is called "file_extensions" with a plural "s" on it. Nowhere else in MODX docs, API, or manager pages does it even hint that this is a plural field (I'm sure I missed it tho). The file_extentions" field is listed as a "string" and it returns a string (not an array) if you access it.
        After a couple tests, I found that it is indeed a CSV field and you can have multiple extensions listed. I opened the "core/model/modx/modcontenttype.class.php" file and noticed a single public method "getExtension()" which returns the "first extension of this Content Type".

        All that to say this, I didn't want to have to test and parse a CSV, so I was able to use this function like so:

        $contenttype = $resource->getOne("ContentType");
        $ext = $contenttype->getExtension();
        


        This returns the first extension and worked perfectly.

        Bob, I thank you for the object reference you created, but can you link to where I find this information in the official docs? Does MODX not have its own "object reference"? I was trying to find it here (http://api.modx.com/revolution/2.2/) but it is a little difficult to use.
          • 3749
          • 24,544 Posts
          Thanks for the kind words. smiley

          I found the MODX reference difficult to use also. I used to just look at the schema file, but I created the object reference (which is created on-the-fly from the MODX schema) to provide an easier (and indexed) reference.
            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