We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17594
    • 14 Posts
    I need to add a "related pages" TV on the page so I wrote a custom snippet which lists all sibling resources (excluding the current one). I get the parent of the current resource via $modx->resource->get('parent'); and job done.

    This works well when editing a resource, but when creating a new one it doesn't work, as I suspect the resource object is not yet available, even if all the fields in the form are populated. I have tried many different methods in my snippet including:

    $resourceId = $modx->resource->get('id');
    $resourcetObj = $modx->getObject('modResource',$resourceId);
    $resourcetObj->getOne('Parent');

    It seems like none of the resource fields (id, pagetitle, parent, etc) are available to get via a snippet before the resource is saved. Possibly what's happening is that the snippet is fired before the form fields are populated.

    Does anybody know of a way I can achieve populating a list TV with an @EVAL call to a snippet with the siblings or the resource being created (not edited)? Maybe a plugin?

    Thanks!
      • 3749
      • 24,544 Posts
      If you're creating the resource in your code, you can get the ID from the return value from the resource/create processor.

      If you're creating the resource in the Manager, it should be fairly easy to do what you want in a plugin attached to the OnDocFormSave event.

      The resource object will be available there as $resource. The $id might be populated (I can't remember), but if not you can do this:

      $id = $resource->get('id');
        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
        • 17594
        • 14 Posts
        Thanks you for your answer Bob.

        Quote from: BobRay at Sep 13, 2016, 09:08 PM

        If you're creating the resource in the Manager, it should be fairly easy to do what you want in a plugin attached to the OnDocFormSave event.

        What I am doing is using the "New Document" icon at the top of the tree or the right-click contextual menu to Create > Document. When the form loads for the new resource the TV should be populated, but since the resource isn't saved yet it is not able to do so.
          • 3749
          • 24,544 Posts
          It sounds like you're trying to populate the TV before the resource even exists. The resource won't ever have an ID until it's saved to the DB.

          The parent field may be populated in a plugin attached to OnDocFormPrerender. You might be able to fill in the TV there. It might take some JavaScript to fill in the TV value, though.

          Can I ask why you need a TV at all? It seems like you could use the code in your snippet to do the job on the fly whenever the page is visited. That way the results would always be current, as opposed to the TV, which might be out of date if there's a new sibling created.
            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
            • 17594
            • 14 Posts
            The TV needs to display a list of the siblings so that the user can manually select the related pages. There is a tutorial on how to do this here:

            https://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/creating-a-multi-select-box-for-related-pages-in-your-template

            That is pretty much exactly what I need, but the only problem with that snippet is that you have to input the parent ID manually, while I need the parent to be inputted dynamically since it will change depending on the parent folder.
              • 3749
              • 24,544 Posts
              When you say "user" do you mean the Manager user who is creating the resource?
                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
                • 17594
                • 14 Posts
                Yes, the manager user manually creating the resource. Sorry for the lack of clarity.
                  • 3749
                  • 24,544 Posts
                  Have you tried just using this:

                  $parentId = $modx->resource->get('parent');


                  You don't really need the resource object or its ID to get the siblings, just the parent's ID. The parent field should be filled in since it's known before the form is displayed.


                    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
                    • 17594
                    • 14 Posts

                    Quote from: BobRay at Sep 13, 2016, 09:55 PM
                    Have you tried just using this:

                    $parentId = $modx->resource->get('parent');


                    You don't really need the resource object or its ID to get the siblings, just the parent's ID. The parent field should be filled in since it's known before the form is displayed.


                    I did try that, and I definitely checked that the parent ID field is filled, but for some reason it won't get it.
                      • 3749
                      • 24,544 Posts
                      Erm ... how can you tell that the parent field is filled if you can't get it?
                        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