We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    How do I get the *unprocessed* value of a resources content field in a snippet?

    I have to retrieve several content fields in an unprocessed/unrendered state as they have several modx tags in them that I need to run some regex on.

    I have this, which works fine if I try to get the pagetitle, but errors if I try to get the content fields:

    $children = $modx->getChildIds(2726);
    
    $content = array();
    
    foreach($children as $child){
    
    	$resource = $modx->getObject('modResource', $child);
    
    	if($resource){
    		
    		$content[] =  $resource->get('content');
    	
    	}
    
    }
    
    echo '<pre>';
    print_r($content);
    echo '</pre>';
    

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

      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 36926
      • 701 Posts
      Quote from: sean69 at May 24, 2013, 11:17 AM

      I have to retrieve several content fields in an unprocessed/unrendered state as they have several modx tags in them that I need to run some regex on.

      I think getContent gets the raw unprocessed content of an element of a resources content field.

      EDIT:
      This should work:
      $resource->getContent();
      


      But don't forget in your example if you just output $content onto the page and view it via the front end the code will then still get processed.

      echo '<pre>';
      print_r($content);
      echo '</pre>';
      

      [ed. note: bennyb last edited this post 13 years, 5 months ago.]
      • discuss.answer
        • 26503
        • 620 Posts
        Quote from: bennyb at May 24, 2013, 11:53 AM
        Quote from: sean69 at May 24, 2013, 11:17 AM

        I have to retrieve several content fields in an unprocessed/unrendered state as they have several modx tags in them that I need to run some regex on.

        I think getContent gets the raw unprocessed content of an element of a resources content field.

        EDIT:
        This should work:
        $resource->getContent();
        


        sorry - no, it still returns the rendered content.
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 3749
          • 24,544 Posts
          I think MODX is still processing the tags before you see the page, even with print_r.

          Try this before the print_r():
          $content = str_replace('[[', '[ [', $content);
          [ed. note: BobRay last edited this post 13 years, 4 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
            • 36926
            • 701 Posts
            Quote from: sean69 at May 24, 2013, 12:28 PM

            sorry - no, it still returns the rendered content.

            It's working for me, as mentioned in your example when you then view the page it will then get processed . So make sure you do whatever regex on it before print_r.

            If you try what bob mentions you'll see you can amend the tag before it's then processed. [ed. note: bennyb last edited this post 13 years, 5 months ago.]
              • 3749
              • 24,544 Posts
              You can also get the unprocessed content in a plugin attached to OnParseDocument. It's in $content, which is a reference variable so if you change it, the changes get passed back before the page is rendered.

              [update] Looking at the code, that may not work. You may have to use $modx->documentOutput, which is deprecated, but should still work.

                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
                • 26503
                • 620 Posts
                Yes, getContent does work and yes, modx is just 'reprocessing' it on me smiley
                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525