Jump to:

1036 Posts in 810 Topics by 315 members

Forum Module

SilverStripe Forums » Forum Module » default Nickname for users created in the CMS? [SOLVED]

Discuss the Forum Module.

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

Page: 1
Go to End
Author Topic: 1148 Views
  • Anatol
    Avatar
    126 Posts

    default Nickname for users created in the CMS? [SOLVED] Link to this post

    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

  • Anatol
    Avatar
    126 Posts

    Re: default Nickname for users created in the CMS? [SOLVED] Link to this post

    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

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: default Nickname for users created in the CMS? [SOLVED] Link to this post

    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)

    1148 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.