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

Can I access data from getCMSFields?


Go to End


3 Posts   828 Views

Avatar
Raver0124

Community Member, 8 Posts

18 September 2013 at 3:03pm

Edited: 18/09/2013 3:04pm

Hi guys,

I'm new in SilverStripe and i'm not quite sure i can do this.
Can i access the data from getCMSField?

class ImageDemo extends Page 
{
    static $db = array(
        'Title'             => 'Varchar'
    );

    static $has_one = array(
        'Graphic'           => 'Image'
    );

    public function getCMSFields($params = null){
        $fields = parent::getCMSFields($params);

        if ($this->Graphic.exists()) {  <--------------- I wanna check if the data exist in Graphic


            $fields->addFieldToTab("Root.Content.Main", new UploadField('Graphic', "A Feature Image"), "Content");
            
        }
        else
        {
            $fields->addFieldToTab("Root.Content.Main", new TextField('Graphic', "A Feature Image"), "Content");
        }
        return $fields;
    }
}

Avatar
Willr

Forum Moderator, 5523 Posts

18 September 2013 at 8:59pm

The answer is sometimes. You can for edit forms but on creation the form values will not be set. You could do something like if($this->Graphic() && $this->Graphic()->exists()) { // ...

Avatar
Raver0124

Community Member, 8 Posts

19 September 2013 at 12:37pm

Thank you very much, that worked perfectly :)