We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11927
    • 105 Posts
    I’m trying to customize the form in the manager that is used to create new users. I want to create it so only certain fields appear, some of those fields have defaults, and the rest of the fields are hidden.

    I’ve gotten it to work, kind of.

    I have a plugin with the below code:
    <?php
    	
    	$eventName = $modx->event->name;
    	
    	switch($eventName) {
    		
    		case'OnUserFormPrerender':
    			/* nothing */
    			break;
    		case'OnUserFormRender':
    			$modx->regClientStartupScript('/manager/defaults-for-user.js');
    			break;
    	}
    	
    	return;


    The javascript file:
    function addLoadEvent(func) {
    	var oldonload = window.onload;
    	if (typeof window.onload != 'function')
    	{
    		window.onload = func;
    	}
    	else
    	{
    		window.onload =
    			function()
    			{
    				if (oldonload)
    				{
    					oldonload();
    				}
    				func();
    			}
    	}
    }
    
    addLoadEvent(
    	function()
    	{
    		var myForm = document.forms[0]; // getElementById('ext-gen75');   //ext-gen76
    		
    		
    		// set defaults
    		var mySpecifyPasswordRadio = document.getElementById('modx-user-password-genmethod-s');
    		mySpecifyPasswordRadio.checked = 'checked';
    		
    		var myCountryField = document.getElementById('modx-user-country');
    		myCountryField.value = 'United States';
    		var myCountryField = myForm.elements['country'];
    		myCountryField.value = 'United States';
    		
    		var myActiveBox = document.getElementById('modx-user-active');
    		myActiveBox.checked = true;
    		
    		
    		// hide other fields
    		
    	}
    );



    The reason I thought onLoad would work is because the actual form isn’t created until after the javascript file above is included. But the problem I have is the onLoad event doesn’t seem to fire unless I hit F5 (refresh the page) when I’m on the create New User form page. So I can’t really tell people using the form to hit refresh all the time.

    I’m sure I’m missing something or maybe missing an event that is happening. When I first load the New User form I get this error in firebug: mySpecifyPasswordRadio is null

    Unless there is an event like OnAfterUserFormRender because OnUserFormRender really fires right before the form loads.
      You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

      Carpet Cleaning
      • 11927
      • 105 Posts
      Why is it that most of the time when I post something, 30 minutes later I get it figured out?

      I just used a delay in the Javascript. - setTimeout

      So the Javascript looks like this now:
      function changeForm()
      {
      		var myForm = document.forms[0]; // getElementById('ext-gen75');   //ext-gen76
      		
      		
      		// set defaults
      		var mySpecifyPasswordRadio = document.getElementById('modx-user-password-genmethod-s');
      		mySpecifyPasswordRadio.checked = 'checked';
      		
      		var myCountryField = document.getElementById('modx-user-country');
      		myCountryField.value = 'United States';
      		var myCountryField = myForm.elements['country'];
      		myCountryField.value = 'United States';
      		
      		var myActiveBox = document.getElementById('modx-user-active');
      		myActiveBox.checked = true;
      		
      		
      		// hide other fields
      		
      }
      
      
      setTimeout("changeForm()",1250);
      



      I’ll report later on about hiding fields (if I get that working).
        You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

        Carpet Cleaning
        • 11927
        • 105 Posts
        So this is what I came up with. Does anyone know how to make a default Access Permission?

        Plugin:
        <?php
        	
        	$eventName = $modx->event->name;
        	
        	switch($eventName) {
        		
        		case'OnUserFormPrerender':
        			/* nothing */
        			break;
        		case'OnUserFormRender':
        			if($my_mode == modSystemEvent::MODE_NEW)
        			{
        				$modx->regClientStartupScript('/manager/defaults-for-user.js');
        				$modx->regClientStartupScript('/manager/hide-user-fields.js');
        			}
        			elseif($my_mode == modSystemEvent::MODE_UPD)
        			{
        				$modx->regClientStartupScript('/manager/hide-user-fields.js');
        			}
        	}
        	
        	return;



        defaults-for-user.js:
        function changeForm()
        {
        	var hide = 'none';
        	
        	// testing
        	//var myTest = document.getElementById('modx-user-phone');
        	//myTest.value = 'NEW User';
        	
        	// grabs the form, there is only one form to begin with
        	var myForm = document.forms[0]; // getElementById('ext-gen75');   //ext-gen76
        	
        	
        	// sets the default values on the form
        	var mySpecifyPasswordRadio = document.getElementById('modx-user-password-genmethod-s');
        	mySpecifyPasswordRadio.checked = 'checked';
        		// shows the password fields
        		var myPasswordBox = document.getElementById('modx-user-panel-newpassword');
        		myPasswordBox.style.display = '';
        	
        	var myCountryField = document.getElementById('modx-user-country');
        	myCountryField.value = 'United States';
        		// sets hidden field - THIS IS IMPORTANT!!! - if the field has a hidden field right next to it, then the hidden field needs to be set
        		var myCountryFieldHidden = myForm.elements['country'];
        		myCountryFieldHidden.value = 'United States';
        	
        	var myActiveBox = document.getElementById('modx-user-active');
        	myActiveBox.checked = true;
        		// sets hidden field - THIS IS IMPORTANT!!! - if the field has a hidden field right next to it, then the hidden field needs to be set
        		var myActiveFieldHidden = myForm.elements['active'];
        		myActiveFieldHidden.value = '1';
        	
        	
        	
        	// hide all tabs for new users only, except the first and last one (will attempt to set default Access Permissions later)
        	var myListItems = document.getElementById('modx-user-tabs').getElementsByTagName('li');
        	for( var x = 1; x < (myListItems.length - 1); x++ )
        	{
        		myListItems[x].style.display = hide;
        	}
        	
        	
        	// hide fields for new users only
        	// names of the fields to hide
        	var names = new Array();
        	names[0] = "modx-user-passwordnotifymethod-e";
        	names[1] = "modx-user-password-genmethod-g";
        	names[2] = "modx-user-active";
        	names[3] = "modx-user-fs-blocked";
        	// levels to climb up to hide the entire row that contains the field
        	var levels = new Array();
        	levels[0] = 8;
        	levels[1] = 8;
        	levels[2] = 3;
        	levels[3] = 0;
        	
        	// climbs up to the correct level and hides that row
        	for( var x = 0; x < names.length; x++ )
        	{
        		var tempField = document.getElementById( names[x] );
        		var i = 0;
        		while( i < levels[x] )
        		{
        			var tempField = tempField.parentNode;
        			
        			i++;
        		}
        		
        		tempField.style.display = hide;
        	}
        	
        }
        
        // delays call so that the form loads
        setTimeout("changeForm()", 1500);
        



        hide-user-fields.js:
        function hideFormFields()
        {
        	// hide other fields
        	var hide = 'none';
        	
        	// names of the fields to hide
        	var names = new Array();
        	names[0] = "modx-user-phone";
        	names[1] = "modx-user-mobilephone";
        	names[2] = "modx-user-address";
        	names[3] = "modx-user-city";
        	names[4] = "modx-user-fax";
        	names[5] = "modx-user-state";
        	names[6] = "modx-user-zip";
        	names[7] = "modx-user-website";
        	names[8] = "modx-user-dob";
        	names[9] = "modx-user-gender";
        	names[10] = "modx-user-class-key";
        	names[11] = "modx-user-comment";
        	// levels to climb up to hide the entire row that contains the field
        	var levels = new Array();
        	levels[0] = 2;
        	levels[1] = 2;
        	levels[2] = 2;
        	levels[3] = 2;
        	levels[4] = 2;
        	levels[5] = 2;
        	levels[6] = 2;
        	levels[7] = 2;
        	levels[8] = 3;
        	levels[9] = 3;
        	levels[10] = 2;
        	levels[11] = 2;
        	
        	// climbs up to the correct level and hides that row
        	for( var x = 0; x < names.length; x++ )
        	{
        		var tempField = document.getElementById( names[x] );
        		var i = 0;
        		while( i < levels[x] )
        		{
        			var tempField = tempField.parentNode;
        			
        			i++;
        		}
        		
        		tempField.style.display = hide;
        	}
        	
        }
        
        // delays call so that the form loads
        setTimeout("hideFormFields()",1500);
        
          You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

          Carpet Cleaning
          • 38494
          • 13 Posts
          this is getting at what i just realized i need

          i made a limited admin account, yet it has the capability to create a new super admin user! what a terrible security hole

          i cant disable the 'view roles' or 'view user group' capability, since i need this limited admin user to be able to add new users to a specific group+role

          so it either needs a default or a way to hide the super admin group/role individually


          EDIT: used bob's 'defaultusergroup' plugin, seems to work ok

          but that still poses a challenge... the limited admin account can disable any other account, i just want it to disable the ones it created, leaving the super admin safe [ed. note: kn00tcn last edited this post 11 years, 10 months ago.]
            • 19328
            • 433 Posts
            For anyone interested; I have solution to add a user to a user group automatically. It's a little addition to your code. See https://forums.modx.com/thread/100005/set-plugin-properties-through-manager-url-using-dahsboard-button#dis-post-540835