We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17930
    • 81 Posts
    I understand Resource Groups, Users, and User Groups.

    What I want to do is show or hide additional content on specific pages according the the User Group the person is assigned to.

    So the top of the page (a video) would be the same for users from User Group A and User Group B, but the text below the video would be different according to the User Group the person is assigned to.

    Is this done through the Element Category Access settings of the User Group? Or is there a better way to do it?

    Thank you in advance. smiley

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 46886
      • 1,154 Posts
      I am not an expert and i am sure someone more knowledgeable will comment, but I think the easiest way to do this is with a custom php snippet. I am sure there is another way of course, but this way is pretty straightforward.

      I am not a php person, but its basically:

      GET -> current user -> current user group
      IF -> user group=group A then display chunk A,
      IF -> user group=group B then display chunk B,
      ELSE -> default chunk

      These would be chunks with static text (plus html if needed).
      • discuss.answer
        • 3749
        • 24,544 Posts
        You might see if the Personalize extra will do what you want. It lets you show different chunks to people inside and outside a group using the &allowedGroups property.

        If that doesn't work, a custom snippet, as nuan88 suggests, should be fairly simple -- possibly as simple as this:

        if ($modx->user->isMember('somegroup')) {
            $output =  $modx->getChunk('chunk1');
        } else {
            $output =  $modx->getChunk('chunk2');
        }
        return $output;
        
          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
          • 17930
          • 81 Posts
          Thank you! Now that I've thought it through I would really like, for this one, to have all the text in the same resource. So I created this with your guys' help.

          <?php
          /*
          [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &input=`Hello World`]]
          
          
          Must have &input and &mustBeUserGroup defined.
          
          
          If you want to show the &input to multiple User Groups, 
          seperate the User Groups with a comma (but don't have a 
          space following the comma).
          Example:
          User Group 1,User Group 2,User Group3
          
          
          I don't know how you would be able to have the ` in the &input.
          */
          
          $output = '';
          
          if($input != '' && $mustBeUserGroup != '')
          {
          	//uncomment for testing
          	//$holdx = $mustBeUserGroup;
          	
          	if(strpos($mustBeUserGroup, ',') !== false)
          		$mustBeUserGroup = explode( ',', $mustBeUserGroup );
          	
          	if ($modx->user->isMember($mustBeUserGroup) )
          		$output = $input;
          	else
          	{
          		//uncomment for testing
          		//$output = 'user not in '. $holdx;
          	}
          }
          else
          {
          	//uncomment for testing
          	//$output = '&input or &mustBeUserGroup were empty in showContentIfUserGroup snippet call';
          }
          
          return $output;



          I am wondering, could I do something like this, if I wanted to move the text to a chunk? And would I need to call the chunk uncached?

          [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &input=`[[$chunkName]]`]]
          
          [ed. note: mafokken last edited this post 8 years, 2 months ago.]
            • 3749
            • 24,544 Posts
            You could do, though I'd suggest a slight modification:

            [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &input=`chunkName`]]


            The way you had it, the entire chunk's contents would be parsed and sent to the snippet, even if the user is not a member of the group.

            The snippet would be something like this:

            $userGroup = $modx->getOption('mustBeUserGroup', $scriptProperties, 'nogroup', true);
            $chunk = $modx->getOption('chunkName', $scriptProperties, '', true);
            
            if ($modx->user->isMember($userGroup)) {
                return $modx->getChunk($chunk);
            }
            
            return '';


            Keep in mind that the snippet will execute once for each tag you use. If you want to test for multiple groups, something like this would be a little faster:

            $output = '';
            
            if ($modx->user->isMember('group1')) {
                $output = 'chunk1';
            } elseif ($modx->user->isMember('group2')) {
                $output = 'chunk2';
            } elseif ($modx->user->isMember('group3')) {
                $output = 'chunk3';
            }
            
            return $output;




            [ed. note: BobRay last edited this post 8 years, 2 months ago.]
              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
              • 17930
              • 81 Posts
              Thanks!!


              <?php
              /*
              [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &input=`Hello World`]]
              
              only one or the other, not both &input and &chunkName
              
              [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &chunkName=`chunk name`]]
              
              
              Must have either &input or &chunkName AND then &mustBeUserGroup defined.
              
              
              If you want to show the &input to multiple User Groups, seperate the User Groups with a comma (but don't have a space following the comma).
              Example:
              User Group 1,User Group 2,User Group3
              
              
              I don't know how you would be able to have the ` (back tick) in the &input.
              
              */
              
              $output = '';
              
              $input = $modx->getOption('input', $scriptProperties, '', true);
              $chunkName = $modx->getOption('chunkName', $scriptProperties, '', true);
              $mustBeUserGroup = $modx->getOption('mustBeUserGroup', $scriptProperties, '', true);
              
              if($chunkName != '')
              {
              	$chunk = $modx->getChunk($chunkName);
              	$input = $chunk;
              }
              
              if($input != '')
              {
              	if($mustBeUserGroup != '')
              	{
              		//uncomment for testing
              		//$holdx = $mustBeUserGroup;
              		
              		if(strpos($mustBeUserGroup, ',') !== false)
              			$mustBeUserGroup = explode( ',', $mustBeUserGroup );
              		
              		if ($modx->user->isMember($mustBeUserGroup) )
              			$output = $input;
              		else
              		{
              			//uncomment for testing
              			//$output = 'user not in '. $holdx;
              		}
              	}
              	else
              	{
              		//uncomment for testing
              		//$output = '&mustBeUserGroup was empty in showContentIfUserGroup snippet call';
              	}
              }
              else
              {
              	//uncomment for testing
              	//$output = '&input or &chunkName were empty in showContentIfUserGroup snippet call';
              }
              
              return $output;


              .
                • 3749
                • 24,544 Posts
                Looks good. smiley

                I would replace this line:

                $output = $input;


                With this

                $output = $modx->getChunk($chunkName);


                And remove the getChunk() line above. There's no point in taking the time to retrieve and parse the chunk unless it's actually going to be used.
                  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
                  • 17930
                  • 81 Posts
                  Good point. smiley


                  <?php
                  /*
                  Snippet: showContentIfUserGroup
                  
                  [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &input=`Hello World`]]
                  
                  only one or the other, not both &input and &chunkName
                  if snippet call has both &input and &chunkName declared, the &chunkName will show and not the &input
                  
                  [[!showContentIfUserGroup? &mustBeUserGroup=`some user group` &chunkName=`chunk_name`]]
                  
                  
                  Must have either &input or &chunkName AND ALSO &mustBeUserGroup defined.
                  
                  
                  If you want to show the &input to multiple User Groups, seperate the User Groups with a comma (but don't have a space following the comma).
                  Example:
                  User Group 1,User Group 2,User Group3
                  
                  
                  I don't know how you would be able to have the ` (back tick) in the &input.
                  
                  
                  */
                  
                  $output = '';
                  
                  $input = $modx->getOption('input', $scriptProperties, '', true);
                  $chunkName = $modx->getOption('chunkName', $scriptProperties, '', true);
                  $mustBeUserGroup = $modx->getOption('mustBeUserGroup', $scriptProperties, '', true);
                  
                  if($mustBeUserGroup != '')
                  {
                  	if($input != '' || $chunkName != '')
                  	{
                  		//uncomment for testing
                  		//$holdx = $mustBeUserGroup;
                  		
                  		if(strpos($mustBeUserGroup, ',') !== false)
                  			$mustBeUserGroup = explode( ',', $mustBeUserGroup );
                  		
                  		if($modx->user->isMember($mustBeUserGroup) )
                  		{
                  			if($chunkName != '')
                  			{
                  				$chunk = $modx->getChunk($chunkName);
                  				$input = $chunk;
                  			}
                  			
                  			if($input != '')
                  				$output = $input;
                  		}
                  		else
                  		{
                  			//uncomment for testing
                  			//$output = 'user not in '. $holdx;
                  		}
                  	}
                  	else
                  	{
                  		//uncomment for testing
                  		//$output = '&input or &chunkName were empty in showContentIfUserGroup snippet call';
                  	}
                  }
                  else
                  {
                  	//uncomment for testing
                  	//$output = '&mustBeUserGroup was empty in showContentIfUserGroup snippet call';
                  }
                  
                  return $output;
                  
                  [ed. note: mafokken last edited this post 8 years, 2 months ago.]