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.

Forum Module /

Discuss the Forum Module.

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

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


Reply


3 Posts   1188 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 [url=http://doc.silverstripe.org/datamodel#onbeforewrite]onBeforeWrite()[/url] before and it worked on the first attempt :)

Cheers!
Anatol

Avatar
MarijnKampf

Community Member, 164 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)