Вопрос

I am using the TreeDropdownField for the SiteTree dropdown, although this is an optional field and would like to be able to clear/remove this value. How would I do this with Silverstripe?

    <?php 
class StaticSidebar extends Page {

    static $db = array(
        'ExternalLink' => 'Text',
        'ExternalText' => 'Varchar',
        'ImageLink' => 'Text'
    );

    static $has_one= array(
        "Image" => "Image",
        "InternalLink" => "SiteTree"
    );

    static $allowed_children = array("none");

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

    $fields->addFieldToTab("Root.Content.Main", new TextField("ExternalText", "External Text"), "Content");
    $fields->addFieldToTab("Root.Content.Main", new TextField("ExternalLink", "External Link"), "Content");
    $fields->addFieldToTab("Root.Content.Main", new TreeDropdownField("InternalLinkID", "Internal Link", "SiteTree"), "Content");
    $fields->addFieldToTab("Root.Content.Main", new ImageField("Image"), "Content");
    $fields->addFieldToTab("Root.Content.Main", new TextField("ImageLink", "Image Link"), "Content");

        return $fields;
    }       
} 

class StaticSidebar_Controller extends Page_Controller 
{


}
Это было полезно?

Решение

I'm afraid this isn't possible with the TreeDropdownField.

You could do something a little hacky like create a dummy page called "None" (with a specific page type e.g. NoPage) that a user can select and then you'd implement the onBeforeWrite method to check for this page type and then set the "InternalLinkID" field to 0 if the page selected is that page type. Not really that elegant though.

The other option would be to use a DropdownField instead. This gives you a simple select box. Not as nice in terms of having a site tree view but you can set an "Empty" value.

A third option might be to instead use the TreeMultiselectField. This field allows multiple selections (same as TreeDropdownField but uses checkboxes). At least this way you could deselect all items. The only issue would be which page to use if your CMS user selected more than one item.

Edit: In SilverStripe 3 it is possible to clear/remove the TreeDropdownField selection by choosing the currently selected item.

Другие советы

$fields->addFieldToTab('Root.TreeDropdown', new TreeDropdownField('PageID','Link','SiteTree'));
$fields->addFieldToTab('Root.TreeDropdown', new CheckboxField('UnselectTreeDropdown','remove Link'));

simple and easy solution for me was creating a CB-field and clear the the treedropdown if the CB is checked

function onBeforeWrite(){
    if($this->UnselectTreeDropdown)
        $this->PageID= 0;
    parent::onBeforeWrite();
}

EDIT: it is possible to clear the TreeDropdownField by just choosing the same item that is currently selected.

It is possible to clear the TreeDropdownField by just reselecting the currently selected item.

I have created a simple module that extends TreeDropdownField to allow clearing a selection in a TreeDropdownField. It's available at github: https://github.com/richardsjoqvist/silverstripe-optionaltreedropdownfield

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top