We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44580
    • 189 Posts
    I'm developing a plugin to add two additional fields to the user form. I've based the code on this tutorial: http://rtfm.modx.com/revolution/2.x/case-studies-and-tutorials/adding-custom-fields-to-manager-forms. I'm making progress, but have become stuck on getting the plugin to recognize an update. Here is a very simplified version of the code I can't get to work:

    <?php
    switch ($modx->event->name) {
        case 'OnUserFormPrerender':
            /* if you want to add custom scripts, css, etc, register them here */
            break;
        case 'OnUserFormRender':
            $v = '';
            if (isset($scriptProperties['resource'])) {
                /* on the update screen, so get current values */
                $v = $id;
            } else {
                /* on the create screen, so nothing to do */
            }
            /* now do the HTML */
            $fields = '
            <p>Id is: '.$v.'</p>
            ';
            $modx->event->output($fields);
            break;
        case 'OnUserFormSave':
            /* do processing logic here. */
            break;
    }
    return;


    When I go into Update User, the variable $v does not display. If I replace it with $id in the html, it does display. This leads me to conclude that the 'if' statement is not correct (it is a straight copy from the tutorial). I have also tried using 'if ($my_mode == modSystemEvent::MODE_UPD)' as well. This displays a blank page.

    Can anyone please point me in the right direction?
      • 3749
      • 24,544 Posts
      There is no resource associated with the user form and there is no variable called $my_mode.

      Try this:


      if ($mode == modSystemEvent::MODE_NEW) {
          return
      }
      
      /* Your code here */
      


      AFAIK, the only variables available there are $user (the user object), $id, and $mode -- all three should be available directly by name.


        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
        • 44580
        • 189 Posts
        That did the trick. Many thanks Bob.

        Should the tutorial be updated? I would myself but I don't have access.

        And BTW, $my_mode did have a value when I plugged it directly into the html.
          • 44580
          • 189 Posts
          Hi Bob, having fixed the previous problem, I hope you can help with the next one. This code:

          <?php
          switch ($modx->event->name) {
              case 'OnUserFormPrerender':
                  /* if you want to add custom scripts, css, etc, register them here */
                  break;
              case 'OnUserFormRender':
                  $v = '';
                  if ($mode == modSystemEvent::MODE_UPD) {
                      /* on the update screen, so get current values */
                      $modx->addPackage('extenduser', MODX_CORE_PATH . 'components/extenduser/model/', 'ext_');
                      $user = $modx->getObject('extUser', $id);
                      $data = $user->getOne('Data');
                  } else {
                      /* on the create screen, so nothing to do */
                  }
                  /* now do the HTML */
                  $fields = '
                  /* lots of html goodies in here */
                  ';
                  $modx->event->output($fields);
                  break;
              case 'OnUserFormSave':
                  /* do processing logic here. */
                  break;
          }
          return;


          will not execute if the line $data = $user->getOne('Data'); is present.

          What I am trying to do here is get the current values into my extended ModUser (extUser) fields for the record that I am editing. Again, I am following a closely as possible another tutorial (http://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/extending-moduser). I'm also unclear as what needs to come next once I can successfully retrieve the record. I have tested my extended fields and all appears to be ok, although I have not yet tried to write to them.

          Sorry to be a pain, but I am at a loss and have searched high and low looking for an answer.
            • 3749
            • 24,544 Posts
            I think you want this:

            $user->getOne('Profile');


            There's no 'Data' alias for the user object. You can see the possible aliases at the end of this section:

            http://bobsguides.com/modx-object-quick-reference.html#modUser
              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
              • 44580
              • 189 Posts
              Quote from: BobRay at Oct 20, 2013, 05:37 PM
              I think you want this:

              $user->getOne('Profile');


              There's no 'Data' alias for the user object. You can see the possible aliases at the end of this section:

              http://bobsguides.com/modx-object-quick-reference.html#modUser

              Sorry, I should have explained further. 'Data' is my alias for the extended ModUser (basically I just followed the naming conventions from the tutorial I linked to above). This is the schema I created:

              <?xml version="1.0" encoding="UTF-8"?>
              <model package="extenduser" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" tablePrefix="ext_">
                  <!-- extend the modUser class -->
                  <object class="extUser" extends="modUser">
                      <composite alias="Data" local="id" class="Userdata" foreign="userdata_id" cardinality="one" owner="local" />
                  </object>
               
                  <object class="Userdata" table="userdata" extends="xPDOSimpleObject">
                      <field key="userdata_id" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>
                      <field key="coach_id" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>
                      <field key="coach_desc" dbtype="varchar" precision="100" phptype="string" null="true" />
                      <field key="parent_id" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>
                      <field key="parent_desc" dbtype="varchar" precision="100" phptype="string" null="true" />
                      <index alias="userdata_id" name="userdata_id" primary="false" unique="true" type="BTREE">
                          <column key="userdata_id" length="" collation="A" null="false" />
                      </index>
                      <aggregate alias="extUser" local="userdata_id" foreign="id" cardinality="one" owner="foreign" />
                  </object>
               </model>
              


              It is the values from this table that I am trying to place in the extra fields on the Update User form (I have left most of the working plugin code out to try and reduce confusion - I don't think it worked smiley ).
                • 3749
                • 24,544 Posts
                Ah. Sorry for being so slow on the uptake. This isn't really my area of expertise, but here are some things that come to mind:

                Did you run parseSchema() to create the class and map files?

                Did you modify the extension_packages System Setting to add your package?

                Did you create the userdata table in the DB?
                  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
                  • 44580
                  • 189 Posts
                  Yes to all of the above. Plus tested successfully with:

                  $componentPath = $modx->getOption('extenduser.core_path', null, $modx->getOption('core_path') . 'components/extenduser/') . 'model/';
                  $modx->addPackage('extenduser', $componentPath, 'ext_');
                  $obj = $modx->newObject('extUser');
                  print_r($obj->toArray());


                  Also manually entered a row in the DB table (so I had something to test the Update User form with).

                  I greatly appreciate your help with this Bob. I still believe this is the correct approach, but I'm struggling with the paucity of documentation and my inexperience. Hopefully there is someone out there who has been here before.