We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53616
    • 6 Posts
    This will be my first post, I'm new to the MODx world \o/.


    I am trying to build my first plugin. From within the plugin... this works:
    $resource->set('alias', 'Something here');


    This does not:
    $resource->set('pagetitle', 'Some other stuff here');
    $resource->set('longtitle', 'Some other stuff here');
    $resource->set('description', 'Some other stuff here');
    


    Why? Is there any difference?

    I tried attaching to "OnDocFormSave" and "OnBeforeDocFormSave".

    Thank you all

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

      • 4172
      • 5,888 Posts
      did you

      $resource->save();


      after setting the fields?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 53616
        • 6 Posts
        Thanks Bruno for your help on this one (and many others in this Forum).

        The problem is not the "$resource->save()", this is not needed.

        This line
        $resource->set('alias', ...

        gets applied instantly, with no need to refresh the page.

        But in this one
        $resource->set('pagetitle', ...

        there is a need to refresh the page :S

        With the save it behaves the same way.
        Is this normal? Can I apply immediately without the need to refresh?

        Thanks,
        António
          • 52064
          • 120 Posts
          Try OnDocFormSave end add ->save() to the end.
            FerX - Developer at Eracom s.r.l.
            • 3749
            • 24,544 Posts
            Maybe I'm being dense, but your question is not clear to me. Are you trying to update the display on the Manager page, the data in the database, or both?

            MODX used two refresh the Manager page after the user saved the resource, but it you want to do that now, it's a little tricky. You have to hijack the save button with some JS code (credit to Gary Nutting):

             $modx->regClientStartupHTMLBlock('
                    <script type="text/javascript">
                        Ext.override(MODx.panel.Resource, {
                            originalSuccess: MODx.panel.Resource.prototype.success
                            , success: function (o) {
                                this.originalSuccess(o);
            
                                var stageDateTv = document.getElementById("tv' . $stageDateTvId . '").value;
                                var stagedResourceTv = document.getElementById("tv' . $stagedResourceTvId . '").value;
            
                                if (!!stageDateTv && (!stagedResourceTv || !stagedResourceTv.length)) {
                                    var url = location.href, i = url.indexOf("?") + 3;
                                    MODx.loadPage(url.substr(i));
                                }
            
                            }
                        });
                    </script>');


              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
              • 53616
              • 6 Posts
              Quote from: ferx77 at Jul 21, 2017, 08:49 PM
              Try OnDocFormSave end add ->save() to the end.

              Thanks "FerX" but it's the same result: it updates the "Resource Alias" field instantly, but it does not update "Title" field.

              It does update the "Title" in the database, because if I refresh the page the field changes.
                • 53616
                • 6 Posts
                Quote from: BobRay at Jul 21, 2017, 10:33 PM
                Maybe I'm being dense, but your question is not clear to me. Are you trying to update the display on the Manager page, the data in the database, or both?
                MODX used two refresh the Manager page after the user saved the resource, but it you want to do that now, it's a little tricky. You have to hijack the save button with some JS code (credit to Gary Nutting):


                Thanks a lot Bob, this code will be helpful in the future... good way to learn the "inside guts" of Modx.

                But what I want is precisely to avoid the need of refresh: updating the form field "Title" instantly on plugin code execution.
                The form field "Resource Alias" is updated instantly (with some JS somewhere), but "Title", "Long Title"... are only updated on the database. To view the field changes, I need to refresh the page... and I want to avoid that.

                Is it a bug or a feature?
                I'm using 2.5.7
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  I think that will be pretty difficult. Your plugin is executing on the server, but the fields you want to update are on the client's browser. You update the fields in the DB, but the browser isn't aware of that unless the page is reloaded.

                  To communicate with the browser, you'd have to use JavaScript, but the JS would have to be created and injected when the Manager page is loaded and rendered, not when the save button is clicked. At that point, you probably don't know the new values for the pagetitle and longtitle (if you did, you'd do everything in a earlier event like OnDocFormPrerender).

                  The only thing I can think of that might do it is a custom processor that you call with AJAX by registering an onClick() function for the Save button with addEventListener(). The processor would somehow get the new values (from a file written by the plugin or the DB), then return them. You'd need the processor to wait until the data was available. The AJAX call's success() function could update the fields on the screen.

                  It works for the alias, because the new alias is returned via the connector in the AJAX response from the resource/update processor. The pagetitle and longtitle are not returned.

                  I would just reload the page. wink






                    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
                    • 53616
                    • 6 Posts
                    Quote from: BobRay at Jul 23, 2017, 04:29 AM

                    ...
                    It works for the alias, because the new alias is returned via the connector in the AJAX response from the resource/update processor. The pagetitle and longtitle are not returned.

                    Thanks for the clear explanation. It makes sense now.

                    Yes, I will happily keep reloading, because this CMS has so many good things wink
                      • 3749
                      • 24,544 Posts
                      I'm glad I could help. smiley

                      BTW, you might be interested in this series of articles (there are a number more to come in the series).

                        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