We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I want to write a plugin that fires on onBeforeDocFormSave, parses the resource content, and changes certain tags if it finds them. I’m having a hard time finding documentation for how to do the parsing...
    $parser= $modx->getParser();
    //now what?
    


    I only need to parse the tags that are directly in the resource content - no need to parse recursively. I’m just not seeing how to do it. I tried using $parser->collectElementTags to no success.
      • 14883 ☆ A M B ☆
      • 450 Posts
      Nevermind. This works:
        $content = $resource->get('content');
        $parser = $modx->getParser();
        $count = $parser->collectElementTags($content, &$matches);
      

        • 3749
        • 24,544 Posts
        It’s not critical, but this would be the "official" way to get the content:

        $content = $resource->getContent();


        It’s a good habit, because the "content" field of chunks and snippets isn’t "content" and it will work even if the field name changes in the future.
          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
          • 14883 ☆ A M B ☆
          • 450 Posts
          So - let my try and re-state what I think I’m hearing you say: $resource->getContent() is the preferred / best practice way to get the actual content from a resource, because get(’content’) can mean different things depending on whether its used with a resource, chunk, snippet, etc.

          Is that right?

            • 3749
            • 24,544 Posts
            Quote from: jrotering at Jan 11, 2011, 08:56 AM

            So - let my try and re-state what I think I’m hearing you say: $resource->getContent() is the preferred / best practice way to get the actual content from a resource, because get(’content’) can mean different things depending on whether its used with a resource, chunk, snippet, etc.

            Is that right?

            Yes, in fact get(’content’) with anything but a resource will generally get you nothing at all. get() is a very low-level method -- a method of the xPDO object.

            The argument to get() is the name of a specific column in the DB. For legacy reasons, the "content" field is ’snippet’ for snippets and ’htmlsnippet’ for chunks (though it’s ’content’ for templates).

            getContent() is a higher-level method -- a method of the MODx object itself (resource, template, snippet, chunk, etc.). Each object knows how to return its own "content" fields when that method is called (by calling get() with the appropriate field name).

            That’s not to say you should never use get(’whatever’). If you know what you’re doing and speed is critical, calling get(’content’) is a tiny bit faster, but if the field name ever changes (and that could happen in future releases), your code could break.

              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