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.

Form Questions /

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

Display logic doesn't work with UploadField, or TreeDropdownField


Go to End


2 Posts   2511 Views

Avatar
Greengin

Community Member, 2 Posts

16 January 2015 at 2:54am

Hi,
I'm working on a SilverStripe 3.1.8 site. I have a DataExtension that defines some fields, and I'm trying to use Display Logic to modify the behavior of the CMS:

    private static $db = array(
        'Enabled' => 'Boolean',
        'Title' => 'Text'
    );
    private static $has_one = array(
        'Link' => 'SiteTree',
        'Image' => 'Image'
    );

    public function updateCMSFields(FieldList $fields) {
        $fields->addFieldsToTab('Root.Other', array(
            CheckboxField::create('Enabled'),
            TextField::create('Title', "Title")->displayIf('Enabled')->isChecked()->end(),
            TreeDropdownField::create("LinkID", "Linked page", 'SiteTree')->displayIf('Enabled')->isChecked()->end(),
            UploadField::create('Image', "Image")->displayIf('Enabled')->isChecked()->end()
        ));

    }

When I check or uncheck the "Enabled" checkbox, the other three fields should appear or disappear correspondingly. Unfortunately, only the TextField does, the TreeDropdownField and UploadField are always shown.

Any ideas why Display Logic fails with these two field types, and how to solve it?
Thank you!

Avatar
patricknelson

Community Member, 7 Posts

28 April 2015 at 8:06am

Edited: 28/04/2015 8:07am

I think i can help. I encountered this very unintuitive problem (I should say, was afflicted with it) myself as well and after much struggling, I gave in and decided to finally Google it (backwards, I know)... Basically all you have to do is use a "DisplayLogicWrapper" to wrap "nonstandard" fields and then on that object, apply your display logic methods. So you'd do this instead:

	public function updateCMSFields(FieldList $fields) {
		$fields->addFieldsToTab('Root.Other', array(
			CheckboxField::create('Enabled'),
			TextField::create('Title', "Title")->displayIf('Enabled')->isChecked()->end(),
			TreeDropdownField::create("LinkID", "Linked page", 'SiteTree')->displayIf('Enabled')->isChecked()->end(),
			DisplayLogicWrapper::create(UploadField::create('Image', "Image"))->displayIf('Enabled')->isChecked()->end()
		));
	}

Note to the author: It may make sense to update SS core to execute the "onBeforeRender" hook in UploadField and then incorporate the ability to set special attributes on the wrapping <div> that the upload field uses, even if it's not an actual <input> of any sort.

Hope this helps anyone else that this has plagued. This is buried in the documentation here: https://github.com/unclecheese/silverstripe-display-logic