939 Posts in 271 Topics by 291 members
Forum Module
SilverStripe Forums » Forum Module » default Nickname for users created in the CMS? [SOLVED]
Discuss the Forum Module.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 946 Views |
-
default Nickname for users created in the CMS? [SOLVED]

15 August 2010 at 11:35pm Last edited: 16 August 2010 3:44pm
Hi,
I use a forum in a 'members only' secure part of a Silverstripe site that requires a member login. That means in my setup all members get added manually in the CMS security tab.
I would like to automatically create a default nickname from 'FirstName Surname'. At the moment when I create a new member in the CMS the nickname field is empty and the user appears as 'anonymous' in the frontend.
OK, the main question is: Does anyone have an idea how to create a default nickname on user creation in the CMS? Any hints are highly appreciated.
Alternatively I thought I could set the defaults for FirstNamePublic and SurnamePublic to true in ForumRole.php (added lines in green):
'defaults' => array(
'ForumRank' => _t('ForumRole.COMMEMBER','Community Member'),
'FirstNamePublic' => 1, // added
'SurnamePublic' => 1, // added
),I think if a member doesn't have a Nickname and FirstNamePublic is set to true it should then show the FirstName instead of 'anonymous' (and 'FirstName Surname' after a little tweak in the same file:
function Nickname() {
if($this->owner->Nickname) return $this->owner->Nickname;
elseif($this->owner->FirstNamePublic && $this->owner->FirstName) return $this->owner->FirstName.' '.$this->owner->Surname;
else return _t('ForumRole.ANONYMOUS','Anonymous user');
}This only works for the author name next to a post. In all other cases it still says 'Anonymous' or 'Anon'. Does anyone have any ideas? I still think that the first approach to create a default nickname from first and last name on user creation in the CMS would work best...
Cheers!
Anatol -
Re: default Nickname for users created in the CMS? [SOLVED]

16 August 2010 at 3:44pm Last edited: 16 August 2010 3:59pm
I found a solution for my problem described above. I added the following method to /forum/code/ForumRole.php to the ForumRole class:
// on the initial user creation add a Nickname generated from '$FirstName $Surname'
function onBeforeWrite() {
if(!$this->ID) {
$user = $this->owner;
if($user->Nickname == '') {
$user->Nickname = $user->FirstName.' '.$user->Surname;
}
}
parent::onBeforeWrite();
}When I now create a user through the CMS Security area it automatically creates a Nickname generated from '$FirstName $Surname'. This could also be useful also for other generated default fields such as the signature etc.
Nice. I have never used onBeforeWrite() before and it worked on the first attempt
Cheers!
Anatol -
Re: default Nickname for users created in the CMS? [SOLVED]

26 October 2010 at 10:15pm
Adding the code to directly to ForumRole.php has one drawback, you will need to make the change every time you update the forum code.
By extending the Member object you can keep the forum code and your changes separate - this isn't much more work to do.
mysite/code/ForumRoleDefaultNickname.php
<?php
class ForumRoleDefaultNickname extends DataObjectDecorator {
// on the initial user creation add a Nickname generated from '$FirstName $Surname'
function onBeforeWrite() {
if(strlen($this->owner->Nickname) <= 0) {
if($this->owner->Nickname == '') {
$this->owner->Nickname = trim($this->owner->FirstName.' '.$this->owner->Surname);
}
}
parent::onBeforeWrite();
}
}Add the following line to your mysite/_config.php
DataObject::add_extension('Member', 'ForumRoleDefaultNickname');
Please note I slightly changed the workings of the onBeforeWrite, I only set the Nickname if there is no nickname set. This should in theory ensure that all users will have a nick (unless they didn't enter there first / lastname)
| 946 Views | ||
|
Page:
1
|
Go to Top |

