Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Forum Module /

Discuss the Forum Module.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

default Nickname for users created in the CMS? [SOLVED]


Go to End


7 Posts   3872 Views

Avatar
Anatol

126 Posts

15 August 2010 at 11:35pm

Edited: 16/08/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

Avatar
Anatol

126 Posts

16 August 2010 at 3:44pm

Edited: 16/08/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

Avatar
MarijnKampf

Community Member, 176 Posts

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)

Avatar
Nwa9ja

Community Member, 8 Posts

20 January 2015 at 9:08pm

I tried using your code MarijnKampf and got this error

Fatal error: Class 'DataObjectDecorator' not found in /home4/heoutre1/public_html/selahstudios/mysite/code/ForumRoleDefaultNickname.php on line 2

Avatar
MarijnKampf

Community Member, 176 Posts

20 January 2015 at 9:20pm

Edited: 20/01/2015 10:20pm

This was for SS 2.X, in SS 3.0+ DataObjectDecorators are now called DataExtensions, see http://doc.silverstripe.org/framework/en/reference/dataextension

<?php
class ForumRoleDefaultNickname extends DataExtension {
	// 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

Member
  extensions:
    -ForumRoleDefaultNickname

or add the following line to your mysite/_config.php

Member::add_extension('ForumRoleDefaultNickname');

Please note I've not tested the above code.

Avatar
Nwa9ja

Community Member, 8 Posts

20 January 2015 at 9:32pm

I have changed the code but now I have this error

Fatal error: Call to undefined method DataExtension::add_extension() in mysite/_config.php on line 20

Avatar
MarijnKampf

Community Member, 176 Posts

20 January 2015 at 10:21pm

The line in _config.php should have been

Member::add_extension('ForumRoleDefaultNickname');