Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » problem with Member::currentUser();

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

Page: 1
Go to End
Author Topic: 1776 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    problem with Member::currentUser(); Link to this post

    hi

    i have SS 2.3.1

    i want to do exactly the same as here http://www.silverstripe.org/archive/show/85210 but it doesnt work

    maby set default TextField value by $defaults but how to put member FirstName into Autor field ?

       public static $db = array(
       "Autor" => 'Text'
       );

    static $defaults = array(
    "Autor" =>
    );

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: problem with Member::currentUser(); Link to this post

    Hi snaip,

    you can it relate to another class, which give an return-value as the FirstName. So you have to create a function, which send a request to the the database. I don't have enough time to write an example. I'm sure, that my solution would run.

    Bets regards,

    Pascal

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: problem with Member::currentUser(); Link to this post

    hmm i tried this:

    public static $db = array(
    'Author' => 'Text'   
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $member = Member::currentUser();
    $options = array(''.$member->FirsName.'');
    $fields->addFieldToTab("Root.Content.Main", new DropdownField("Author", "Author:", $options), 'Content');
    return $fields;
    }

    but when i save i get "Javascript parse error" :/

    and hmm why i cant set value to the TextField ?
    this doesn't work, http://doc.silverstripe.com/doku.php?id=textfield :

    $fields->addFieldToTab("Root.Content.Main", new TextField('Author','Author:','Enter Your name'),'Content');

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: problem with Member::currentUser(); Link to this post

    You need to overload populateDefaults() instead of setting the item in the array.

    e.g.

    /**
    * Set up the defaults for this class.
    *
    * Overloaded from DataObject to support more complicated
    * defaults, such as calling a function to get a dynamic value.
    *
    * @return array
    */
    public function populateDefaults() {
       parent::populateDefaults();
       $memberName = Member::currentUser() ? Member::currentUser()->getName() : null;
       $this->setField('Author', $memberName);
    }

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: problem with Member::currentUser(); Link to this post

    thanks a lot Sean :]

    1776 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.