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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

problem with Member::currentUser();


Go to End


5 Posts   2613 Views

Avatar
snaip

Community Member, 181 Posts

20 May 2009 at 3:33am

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

Community Member, 47 Posts

20 May 2009 at 7:29pm

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

Community Member, 181 Posts

23 May 2009 at 9:16am

Edited: 23/05/2009 9:22am

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

Forum Moderator, 922 Posts

23 May 2009 at 5:58pm

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

Community Member, 181 Posts

23 May 2009 at 9:41pm

thanks a lot Sean :]