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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Creating Hidden field with auto filled values in the admin


Go to End


2 Posts   1860 Views

Avatar
Adesso

Community Member, 10 Posts

17 September 2014 at 10:29pm

Edited: 18/09/2014 9:27am

For the setup code, please reffer to Articles that only have relations to Other Pages

When creating a Holding Page and then a Article page I would like to set a hidden field with the current user in it. This way I can have at least a fast way to see who changed the Article last. As far as I can see SilverStripe has a history function of Objects, but I just need the last user for now, and not the complete history.

How would one hide a text Field in the CMS, and then auto-fill it with the current user ID or Name or whatever is needed to link the current user to the edit.

I hope this question has not been asked before.

Greetings from Germany

Avatar
apiening

Community Member, 60 Posts

18 September 2014 at 5:38am

Edited: 18/09/2014 9:28am

Hi Adesso,

here are two options for solving your problem:

class MyPage extends Page
{
    private static $has_one = array(
        'Creator' => 'Member',
    );

    // Option A: Your approach with a hiddenfield:
    public function getCMSFields()
    {
        $fields = parent::getCMSFields()
        if (!$this->exists()) $fields->replaceField('CreatorID', HiddenField::create('CreatorID', null, Member::currentUserID());
        return $fields;
    }

    // Option B: A little more elegantly than the hidden field, using a hook:
    public function onBeforeWrite()
        parent::onBeforeWrite();
        if (!$this->CreatorID) $this->CreatorID = Member::currentUserID();
    }
}

I hope this helps.

Cheers

Andy