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

    I would like to know if it’s possible to have a list of extended fields available each time I create a new user, like an event who create this extended fields for me, or any solution.

    Thanks.
      Low
      • 3749
      • 24,544 Posts
      Yes, you can write a plugin that adds them to the Create/Update User panel and saves them during the events OnUserFormRender and OnUserFormSave, but it’s not a trivial job.

      Here’s a plugin that does something similar. It adds a "tree_root_id" input field on the user form and then saves it as a user setting, you’d have to replace that later code with code that sets the extended field:

      <?php
      /* MemberPages Plugin */
      switch ($modx->event->name) {
          case 'OnUserFormRender':
              /* set field to the current value (if any) */
              /* The user ID is always available as $id */
              $v = "";
              $treeRoot = $modx->getObject('modUserSetting',
                  array('user'=>$id,'key'=>'tree_root_id'));
              if ($treeRoot) { /* the user already has the setting */
                  $v = $treeRoot->get('value');
              }
              /* now do the HTML */
              $fields = '<div class="x-form-item x-tab-item">
                  <label class="x-form-item-label" style="width:152px;">
                      <b>Tree Root</b></label>
                  <div class="x-form-element">
                      <input type="text" name="TreeRoot" value="'. $v .
                          '" class="x-form-text x-form-field" />
                  </div>
              </div>';
              $modx->event->output($fields);
              break;
           case 'OnUserFormSave':
              if (isset($_POST['TreeRoot'])){  /* field is filled in */
                 $setting = $modx->getObject('modUserSetting',
                    array('user'=>$id,'key'=>'tree_root_id'));
      
                    if ($setting) { /* user setting already exists - update it */
                       if ($setting->get('value') != $_POST['TreeRoot']){
                          /* user changed it */
                          $setting->set('value',$_POST['TreeRoot']);
                          $setting->save();
                       }
                    } else { /* create a new one  */
                        $setting =& $modx->newObject('modUserSetting');
                        $setting->set('user',$id);
                        $setting->set('key','tree_root_id');
                        $setting->set('value',$_POST['TreeRoot']);
                        $setting->set('namespace','core');
                        $setting->save();
                    }
              }
              break;
      }
      return;
      ?>
      


      Here’s an example of code that sets an existing extended user field:

      <?php
      $user = $modx->getObjectGraph('modUser',
          array('Profile' => array()),
          array('id'=>'$id));
      $extFields = $user->Profile->get('extended');
      $extFields['sign'] = 'Virgo';
      $user->Profile->set('extended',$extFields);
      $user->save();
      
        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
        • 36426
        • 197 Posts
        Hi Bob, sorry to hijack an old thread but I'm looking to do something similar.

        I'm already using the Login plugin so users can register. But I'm hoping that there is a way that if a user registers or if I add a user manually from the backend, both methods of user creation add a default set of extra extended fields - these aren't on the user registration form but rather fields I want to then manually populate so I can pull them back out and the user can see them from their "My Profile / My Account" type page...

        Is there a simple way to set this up? And where would it then be called so that both manual user creation and user registration both trigger these extra extended fields to be added?

        Thanks for any help smiley
          Yorkshire UK based hosting provider: https://www.simulant.uk
          • 17301
          • 932 Posts
          Take a look at Bobs ClassExtender extra:
          https://bobsguides.com/classextender-class.html
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
            • 46886
            • 1,154 Posts
            ClassExtender is definitely the way to go, the core of Modx doesn't handle much user data.

            I could be totally wrong but I just checked and it looks like all the user data fields are available from inside the manager, I thought that might be tricky but perhaps not
              • 36426
              • 197 Posts
              Hi thanks. Well I can already add the fields to a user profile I need and pull them back out to show the user, as that all already works from Login and Register snippets. I just hoped there was a way to set those fields up so they are always available in the backend on user creation or editing a user (even if the user didn't register themselves or add such data on registration)...

              Any help to just add them in would be great, or can ClassExtender do this anyway?
                Yorkshire UK based hosting provider: https://www.simulant.uk
                • 3749
                • 24,544 Posts
                ClassExtender will put the fields on the Create/Edit User form in the Manager, and the user can also see them in the front end with CE's UpdateProfile helper.

                They will also be available with a snippet that works like getResources to display collections of users.

                FWIW, it will make your life easier if you have the *all* your extra fields planned before running ClassExtender.
                  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
                  • 36426
                  • 197 Posts
                  Quote from: BobRay at Jun 11, 2018, 03:18 AM
                  ClassExtender will put the fields on the Create/Edit User form in the Manager, and the user can also see them in the front end with CE's UpdateProfile helper.

                  They will also be available with a snippet that works like getResources to display collections of users.

                  FWIW, it will make your life easier if you have the *all* your extra fields planned before running ClassExtender.

                  Hi thanks Bob.

                  I've added in two extra fields which are "checkin date" and "checkout date" so I can then show these to a user on certain pages.

                  However for some reason despite them being added and showing in the backend, the fields don't populate on my user profile page:

                  [[!Profile]]
                  [[!SetResourcePlaceholders]]
                  <h4>Details for user [[+username]]</h4>
                  <p>Email: [[+email]]</p>
                  <p>Full Name: [[+fullname]]</p>
                  <p>Phone: [[+phone]]</p>
                  <p>Mobile: [[+mobilephone]]</p>
                  <p>Website: [[+website]]</p>
                  <p>Address: [[+address]]</p>
                  <p>Address: [[+address2]]</p>
                  <p>City: [[+city]]</p>
                  <p>Postal Code: [[+zip]]</p>
                  <p>Country: [[+country]]</p>
                  <p>Arrival Date: [[+checkin]]</p>
                  <p>Departure Date: [[+checkout]]</p>
                  <p> </p>
                  <hr />
                  <p><small>Please visit <a data-ajax="false" href="[[~60]]" title="Customer Portal" >Update Profile</a> area if you wish to amend your details.</small></p>


                  The profile page resource when viewed shows all the other fields but the Arrival/Departure are blank - they have been populated with data in the backend but it isn't showing on the frontend!

                  Does this not work with [[!Profile]] snippet? Or should I be pulling them in some other way?

                  Thanks again for the help smiley
                    Yorkshire UK based hosting provider: https://www.simulant.uk
                    • 3749
                    • 24,544 Posts
                    Aren't they user placeholders, not resource placeholders. 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
                      • 36426
                      • 197 Posts
                      Quote from: BobRay at Jun 28, 2018, 05:27 PM
                      Aren't they user placeholders, not resource placeholders. wink

                      Hi thanks, yes that is my current profile page for the user which pulls in all the fields and displays them. The only ones it currently doesn't show though are the ClassExtender (which are just the arrival and departure dates), how can I add those ClassExtenders ones to the profile page to display or be updated on the "Manage profile" page too. The idea is I have a good working login user system with all the fields and profile update functionality in place, I just need to add those extra two ClassExtender fields to show that little bit of extra data.

                      Thanks again for any help.
                        Yorkshire UK based hosting provider: https://www.simulant.uk