I'd like for users and their posts to be labeled with a display name (e.g. "Jon Smith") that's separate from the username they use to login (which is how our SSO works). I've searched around a bit, as I could've sworn I saw someone else ask as similar question, but I couldn't find anything, so I was wondering if anyone else had any ideas as to how best to go about doing this.
I'll be delving into myself in a couple days, but first I wanted to see what the community had to say. Also I'd really like to keep it confined to a plugin and thereby avoid modifying any of the core files, but I'm new to Vanilla development and am not sure if that will be possible for this particular feature.
0 • •
Answers
Developer at Vanilla Forums, Inc. [GitHub, Twitter]
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •For the rest, I made a plugin that did the following:
1) Added another column to the User table called DisplayName
2) Overrode the UserBuilder and UserAnchor functions to include DisplayName.
3) Setup event handlers for AfterDiscussionSummaryQuery, BeforeGetID (discussion model), AfterCommentQuery, and AfterActivityQuery (which I had to add into the core code myself) to add select statements that mirrored the Name statements in each function, except replaced with DisplayName instead.
This setup allowed me to change the link text to use display names, but left most of the links themselves intact (linking to usernames not display names). Mind you, even if the links to point to display names it doesn't seem to affect anything, seeing as the links really use User IDs.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •I also don't have a way to set the display name, that's done separately in the code for my SSO login.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •We're using SSO too, so that's not a problem.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •Step 1: Make the following modifications... See next comment for the plugin code...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •Note that this plugin overrides two methods: UserAnchor and UserBuilder. If you're using another plugin that also overrides these methods (like Gravatar), I'm pretty sure you'll need to modify them to include DisplayName as well.
<?php if (!defined('APPLICATION')) exit();<br />// Define the plugin:
$PluginInfo['DisplayNameplugin'] = array(
'Description' => 'Plugin to implement support for display names',
'Version' => '1.0',
'Author' => "Jonathan Pautsch",
'AuthorEmail' => 'jspautsch@gmail.com',
'AuthorUrl' => 'http://vanillaforums.org/profile/32921/jspautsch'
);
class DisplayNameplugin extends Gdn_Plugin
{
public function Setup()
{
Gdn::Structure()
->Table('User')
->Column('DisplayName', 'text', NULL)
->Set();
}
public function CommentModel_AfterCommentQuery_Handler(&$Sender, $Args)
{
$Join = GetValue('Join', $Args);
if ( $Join )
{
$Sender->SQL->Select('iu.DisplayName', '', 'InsertDisplayName')
->Select('uu.DisplayName', '', 'UpdateDisplayName');
}
}
public function ActivityModel_AfterActivityQuery_Handler(&$Sender)
{
$Sender->SQL->Select('au.DisplayName', '', 'ActivityName')
->Select('ru.DisplayName', '', 'RegardingName')
->Select('au.DisplayName', '', 'ActivityDisplayName')
->Select('ru.DisplayName', '', 'RegardingDisplayName');
}
public function ConversationMessageModel_BeforeGet_Handler(&$Sender)
{
$Sender->SQL->Select('iu.DisplayName', '', 'InsertDisplayName');
}
}
if (!function_exists('UserBuilder'))
{
//Override the default UserBuilder function
function UserBuilder($Object, $UserPrefix = '')
{
$Object = (object)$Object;
$User = new stdClass();
$UserID = $UserPrefix.'UserID';
$Name = $UserPrefix.'Name';
$DisplayName = $UserPrefix.'DisplayName';
$Photo = $UserPrefix.'Photo';
$Gender = $UserPrefix.'Gender';
$User->UserID = $Object->$UserID;
$User->Name = $Object->$Name;
$User->DisplayName = $Object->$DisplayName;
$User->Photo = property_exists($Object, $Photo) ? $Object->$Photo : '';
$User->Email = GetValue($UserPrefix.'Email', $Object, NULL);
$User->Gender = property_exists($Object, $Gender) ? $Object->$Gender : NULL;
return $User;
}
}
if (!function_exists('UserAnchor'))
{
//Override the default UserAnchor function
function UserAnchor($User, $CssClass = '', $Options = NULL)
{
static $NameUnique = NULL;
if ($NameUnique === NULL)
$NameUnique = C('Garden.Registration.NameUnique');
$Px = $Options;
$Name = GetValue($Px.'Name', $User, T('Unknown'));
$DisplayName = GetValue($Px.'DisplayName', $User, T('Unknown'));
$UserID = GetValue($Px.'UserID', $User, 0);
if ($CssClass != '')
$CssClass = ' class="'.$CssClass.'"';
return ''.htmlspecialchars($DisplayName).'';
}
}
?>
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •Gdn::SQL()->Update('User')->Set('DisplayName', $newdisplayname)
->Where('UserID', $userid)
->Put();
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •