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’m trying to write a simple test plugin to do the following when a user clicks the ’Save’ button after editing a resource:

    • look for a certain substring in the pagetitle field
    • if found, set a different value for pagetitle

    My first-crack code looks like this:
    $pagetitle= $modx->resource->get('pagetitle');
      if (strstr($pagetitle, '__draft_')) {
           $modx->resource->set('pagetitle', 'changed');
         $modx->resource->save();
      }


    Questions:

    • Should this be an OnBeforeDocFormSave event or an OnDocFormSave event?
    • Is that the right code for updating the pagetitle field and saving the document?

      • 3749
      • 24,544 Posts
      You want OnBefore... The other event happens after the object is saved to the DB. The rest of the code looks right to me.
        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
        OK. A couple of follow-up questions.

        1. OnBefore was my first instinct... but When I activate the plugin set to OnBefore, my documents never save, I get the endless "Please Wait..." message.
        2. Thinking ahead, if I do manage to fire the code as written in an OnBefore event... wouldn’t the actual OnDocSave event overwrite the pagetitle field anyway? Maybe what I’m really trying to do does need to happen after the object is saved to the db... save what they input into the form, then test the value and modify if needed(?)

          • 14883 ☆ A M B ☆
          • 450 Posts
          When I activate the plugin set to OnBefore, my documents never save, I get the endless "Please Wait..." message

          Clarification: Actually, this is caused by the line
           $pagetitle = $modx->resource->get('pagetitle');


          The endless "Please Wait" happens regardless of which event I point at, if this line is present.

          I can write this:

          $resource = $modx->resource;
          


          But this gums things up:

          $pagetitle = $resource->get('pagetitle');
          
          -or-
          $id = $resource->get('id');
          


          Seems like I’m just not able access resource fields. Should I be able to, for the current resource? Or is there some other way I have to get at the resource from a plugin?
            • 14883 ☆ A M B ☆
            • 450 Posts
            Figured it out.

            I don’t want "$modx->resource".

            I just want "$resource"

            These events (OnBeforeDocFormSave & OnDocFormSave) have an Event Parameter called "resource" that points to the modResource object being handled by the form.

            This has been another episode of "James posting to the forums before reading all available documentation".

              • 3749
              • 24,544 Posts
              Sorry, I should have noticed that. tongue
                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
                • 39437
                • 13 Posts
                I know it is quite an old Discussion, but I just read it and want to add that the line:
                $modx->resource->save();

                is not necessary.
                The Resource will be saved in the next step anyways.
                  MODX Rocks I am so glad that I have found it.
                  • 3749
                  • 24,544 Posts
                  That line is necessary if the the plugin is connected to OnDocFormSave, which fires after the doc is saved. It's not necessary if you're connecting to OnBeforeDocFormSave.
                    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