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.

Customising the CMS

problem with Member::currentUser();


Reply

5 Posts   1884 Views

Avatar
snaip

20 May 2009 at 3:33am Community Member, 180 Posts

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" =>
);

Avatar
PGiessler

20 May 2009 at 7:29pm Community Member, 47 Posts

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

Avatar
snaip

23 May 2009 at 9:16am (Last edited: 23 May 2009 9:22am), Community Member, 180 Posts

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');

Avatar
Sean

23 May 2009 at 5:58pm Forum Moderator, 921 Posts

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);
}

Avatar
snaip

23 May 2009 at 9:41pm Community Member, 180 Posts

thanks a lot Sean :]