Skip to content

Instantly share code, notes, and snippets.

@rvalitov
Created April 6, 2023 16:26
Show Gist options
  • Save rvalitov/497a72cb239106123e3afa6be7e21432 to your computer and use it in GitHub Desktop.
Save rvalitov/497a72cb239106123e3afa6be7e21432 to your computer and use it in GitHub Desktop.
Community Builder Conditional: how to show a field only if a user looks his own profile in Joomla?
/**
* @param FieldTable|TabTable $object
* @param string $reason
* @param int $userId
* @param null|bool $js
* @param null|bool $reset
* @return bool|int
*/
private static function parseConditions( $object, $reason, $userId, &$js = null )
//Function code goes here....
//Leave all the code and find a part starting with this line:
case 'customusers':
if ( $andCondition->getString( 'user_users', 'displayed' ) === 'viewer' ) {
$fieldValue = Application::MyUser()->getUserId();
} else {
$fieldValue = (int) $userId;
}
$operator = $andCondition->getInt( 'operator_users', 12 );
$delimiter = $andCondition->getString( 'delimiter_users' );
if ( ! $delimiter ) {
$delimiter = ',';
}
//You will see a line, that you need to comment out:
//$value = $cbUser->replaceUserVars( $andCondition->getString( 'field_users' ), true, false, array(), false );
//Below is the new 4 lines of code that we need to add:
$field_users = $andCondition->getString( 'field_users' );
if ($field_users === 'viewing_user')
$field_users = Application::MyUser()->getUserId();
$value = $cbUser->replaceUserVars( $field_users, true, false, array(), false );
//End of new added lines, the original code continues below.
break;
@rvalitov
Copy link
Author

rvalitov commented Apr 6, 2023

This modification was made for CB Conditional plugin version 6.0.2 (the latest version at the time of publishing this gist) for Community Builder (tested with version 2.8.0 and Joomla 4).

The task I had was to display some private fields (like address) on user's profile page conditionally, so that this field is visible only if a user looks into his own profile. It helps the user to see all his fields while some data remains private and hidden from other users.

To have such functionality you need to add just 4 lines of code from this gist to a file /components/com_comprofiler/plugin/user/plug_cbconditional/library/CBConditional.php in your Joomla directory.

Then you should add a conditional rule to your field and use a new keyword called viewing_user which substitutes viewing user id into this field:
image

You can't mix viewing_user with ids in the same field. If you need more conditions add them separately.
For example it's easy to allow moderators also to see the private fields, just add the related built-it moderation condition as a new OR row.

The plugin code goes under GPL-2 license.
Special thanks to Joomlapolis!, @krileon and @beat for developing Community Builder and providing a free version, too!

@krileon
Copy link

krileon commented Apr 6, 2023

Not necessary. Just use [cb:userdata field="user_id" user="#me" /] to substitute in the viewing users id. That parameter supports substitutions.

@rvalitov
Copy link
Author

rvalitov commented Apr 6, 2023

Thank you so much! That's a perfect solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment