On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I have a site where users can select interests when they register. They can select more than one, but I tried in vain to find an example of using checkboxes to toggle an extended field container with multiple possible values (e.g. favouritefoods = cheese AND wine AND chocolate), so I created my own. Thought it might be useful to somebody.

    Please note: I’ve used this in conjunction with the Login Profile snippet - it uses the Login Profileupdate submit button. Use the
    [[!Profile? &useExtended=`1`]] [[!UpdateProfile]]
    calls in the page.

    //checkbox_extended
    //Snippet to generate checkboxes that add/remove extended field values in a user's profile. This is used in conjunction with the Login Profile snippet.
    <?php
    //get your checkbox values - defined in your snippet call as &boxes=`checkbox1,checkbox2,checkbox3...`
    $checkboxvalues = explode(",",$boxes,99);
    
    //get extended field values from the user profile - use &container=`` in your snippet call to define the container name of the extended value(s). You'll find this in Manager > Security > Manage Users
    $profile = $modx->user->getOne('Profile');
    $extended = $profile->get('extended');
    $extendedvalues = $extended[$container];
    
    //compare the two and identify any matches. e.g. if checkbox1 == extendedfieldvalue1 then return a 'checked' result
    for($x = 0; $x < count($checkboxvalues); $x++)
    {
    if(in_array($checkboxvalues[$x],$extendedvalues))
    {
    $selected = 'checked';
    }else{
    $selected = '';
    }
    //output the checkboxes - you can style it how you want here
    $output.= '<div class="option"><input name="'.$container.'[]" number="" type="checkbox" value="'.$checkboxvalues[$x].'"'.$selected.'> '.$checkboxvalues[$x].'</input></div> ';
    }
    
    return $output;


    Then in the page, use:
    [[!checkbox_extended? &container=`yourextendedfieldcontainername` &boxes=`checkbox1,checkbox2,checkbox3...etc`]]


    This works nicely for one container, I haven’t tried using it for multiple containers (e.g. favouritefoods, favouritecars etc.) but i don’t see why you can’t use the snippet more than once on the same page.
    • I realize this is over a year old but I am hoping someone other than myself is trying to do the same thing.

      I added
      $user = $modx->getObject('modUser',$userId);
      if (!$user) return '';
      $profile = $user->getOne('Profile');
      

      Adding this squelched the error associated with getOne('Profile'), but I am still unable to get the boxes to show on the page.

      I am trying to use this in a wish list to remove the TV but stay on the page.
      • This is an old thread but is till current information as far as I can see. I added the following to the page that I wanted the clients optional select box's showing so they could update when required.
        [[!Profile? &useExtended=`1`]] [[!UpdateProfile]]
        [[!checkbox_extended? &container=`tick` &boxes=`checkbox1,checkbox2,checkbox3`]]
        

        Then I created a snippet called "useExtended" and added the code from original post into it, [as below]. but for some reason it is throwing errors, and just showing a blank page.
        //checkbox_extended
        //Snippet to generate checkboxes that add/remove extended field values in a user's profile. This is used in conjunction with the Login Profile snippet.
        <?php
        //get your checkbox values - defined in your snippet call as &boxes=`checkbox1,checkbox2,checkbox3...`
        $checkboxvalues = explode(",",$boxes,99);
         
        //get extended field values from the user profile - use &container=`` in your snippet call to define the container name of the extended value(s). You'll find this in Manager > Security > Manage Users
        $profile = $modx->user->getOne('Profile');
        $extended = $profile->get('extended');
        $extendedvalues = $extended[$container];
         
        //compare the two and identify any matches. e.g. if checkbox1 == extendedfieldvalue1 then return a 'checked' result
        for($x = 0; $x < count($checkboxvalues); $x++)
        {
        if(in_array($checkboxvalues[$x],$extendedvalues))
        {
        $selected = 'checked';
        }else{
        $selected = '';
        }
        //output the checkboxes - you can style it how you want here
        $output.= '<div class="option"><input name="'.$container.'[]" number="" type="checkbox" value="'.$checkboxvalues[$x].'"'.$selected.'> '.$checkboxvalues[$x].'</input></div> ';
        }
         
        return $output;
        


        Have in misinterpreted the instructions?
        Using Platform: Modx Revo 2.3.2
        • Check your server's PHP error log to see why it's failing. A white-screen means a fatal PHP error that won't even allow PHP's internal error logging to function. All that MODX is, is a single index.php file that then includes everything else, so any fatal PHP error in any of the files (such as your snippet, which is loaded as a file) will abort the whole thing.

          Why do you have a snippet named useExtended, and where does it get called on the page? That property in the Profile snippet just indicates whether to display the extended fields or not.
          If true, will set as placeholders all extended fields as well.
          Or is that just your name for the original checkbox_extended snippet?

          You should gather those variables into an array, then use
          return $modx->getChunk('myChunk', $vars);
          


          http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet#HowtoWriteaGoodSnippet-DonotincludeHTML
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
          • sorry that was a typo "useExtended" was meant to be "checkbox_extended"

            php server log is showing the below
            14.2.189.105 - - [01/Dec/2014:16:53:06 +1100] "GET /tick.html HTTP/1.1" 500 - "http://my.cloudalarms.net.au/manager/?a=element/chunk/update&id=30" "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"

            id=30 is another page, for some reason it is calling this and I cant see why...
            • That's the web server access log, not the PHP error log. And that's a report of accessing the Manager to update a chunk, chunk #30. And the web server returned a 500 error.

              You can see where the error log is in the Manager, Manage -> Reports -> System Info, and the phpinfo() view link, in the Core section of the resulting display.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
              • I've set up a localhost installation just for dealing with user management testing and development. I'll see what I can do with this.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                • You can't have anything at all in the snippet above the <?php opening tag, or PHP will try to process it as a mixed HTML/PHP document with inline PHP. Needless to say, MODX won't be happy with that, although actually it can be done under certain conditions - definitely not recommended. So MODX automatically inserts an opening <?php tag for all snippets and plugins if it doesn't find one at the top of the field. [ed. note: sottwell last edited this post 5 years, 4 months ago.]
                  • I just need to work out how I can put custom Description next to each tick box, instead of just the name of the extended field.

                    e.g.
                    [] add personal detail to emails {snippet called "userDetail"}
                    [] show company ph number {snippet called "sACC"}
                    [] show voice code on alert emails
                      • Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org