We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38282
    • 12 Posts
    Hi,

    I've been experimenting with the 2.2 release and one feature that interested me a lot is the possibility to create custom resource classes.
    So I began creating my very own CRC, with the tutorial on http://rtfm.modx.com/display/revolution20/Creating+a+Resource+Class as my guide.

    At this point, I'm at the end of the tutorial's first page, getting nuts because I can't get the 'Create new CRC here' menu item to show up.
    Three things in particular are very unclear to me:

    • what files do i actually need to get it to work? (now I've just got my class.php-file and the xml schema)
    • how should I make the addExtensionPackage() call? (now I've created a small snippet that makes the call)
    • I've put my files in the core/components directory, is this the reason why it doesn't work (I agree that this isn't considered to be best practice, but can it actually cause the CRC not to work?)

    If anyone can help me, I'ld be grateful
      • 28215
      • 4,149 Posts
      1. As long as you've run the build on your xml schema (similarly to how you do it in normal Extras so it generates the xPDO classes and maps, that's all the files you'll need. Make sure your CRC extends the modResource class.

      2. You'll only need to run the addExtensionPackage call *once* - it's basically just adding your record to the setting. Make sure it's passing the proper path though.

      3. No, your files can be anywhere (Articles is in core/components/articles/model/articles/article.class.php), as long as you're loading it correctly.

      Can you post more of your directory structure, Extension Packages setting/path, and even your CRC class file?
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 38282
        • 12 Posts
        When you go bananas searching for a bug, it's usually a ridiculous bug. In this case it was a missing quote in my addExtensionPackage() call...

        So, now I got as far as the end of the tutorial's first page, and I'm faced with a whole new problem: can't get modX to work anymore...
        Basically every time modX has to load a resource, the call
        $modx->getObject('modResource',$_REQUEST['id']);

        returns NULL. This off course leads to a fatal error when modx tries to call a member function of that NULL-resource...

        Any ideas, anybody?
          • 38282
          • 12 Posts
          I dug into it a little deeper and found out why modX can't load resources from the database any more. Apparently, the fields declared by my CRC aren't added to the modx_site_content table's definition. Off course I can fix it by adding these fields manually, but that's less than ideal at best.

          I guess that somewhere, I skipped a little step, but I'm not sure where. And since there is no field added to modResource in the tutorial's example, I'm a bit in the dark here.
            • 28215
            • 4,149 Posts
            Generally you should not be adding fields to CRC's, but only repurposing existing fields. If you want to add fields, you can add a related object (another table that references the site_content row) and override the loadInstance method for modResource to join that table on load of the Resource.

            This is the preferred way to add new fields; Articles currently adds a new field or two, but wont as of the next revision for Revo 2.2.1.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 38282
              • 12 Posts
              I did think it wasn't really optimal to augment the modx_site_content-table with extra fields that will be populated for few records only.

              I guess that in stead of adding fields in my CRC, I should rather create some TVs... Then again, I still would have to turn to CRCs to make the use of these TVs more fool-proof:

              • Getting the fields for the TVs on the first tab of the resource in the manager
              • Making the TV fields required
              • Maybe even impose a certain template
              Keeping in mind the person who is to add these resources, I'm afraid that all of these 3 adaptations truly are necessary...