在 Silverstripe CMS 3.1 中的 OptionsetField 中创建自定义字段

Create custom field inside OptionsetField in Silverstripe CMS 3.1

我想从一些预定义的值中选择我的日历事件的类型,但如果未列出,则创建一个新的(自定义)类型。

所以我在 $db 中创建了这样的字段: 'Type' => 'Varchar', 'EventCustomType' => 'Varchar'

然后,在 getCMSFields() 中我有:

$f->addFieldsToTab("Root.Main", $eventType = new OptionsetField(
            'Type',
            _t('CalendarEvent.EVENTTYPE','Type'),
            array (
                'music'  => _t('CalendarEvent.MUSIC','Music'),
                'sport'  => _t('CalendarEvent.SPORT','Sport'),
                'drama'  => _t('CalendarEvent.DRAMA','Drama'),
                'custom' => TextField::create('EventCustomType','Event type')
            )
        )
    );

问题是我不知道如何在 Textareafield 之前插入标签 "Custom" 并在同一行中设置它们的样式。

此外,我不确定自定义字段是否需要第二个字段。我可以在 "Type" 字段中插入自定义值或验证它吗?

感谢任何建议

这可以通过为 "EventCustomType" 设置一个单独的字段然后使用 Display Logic 来显示它来实现,例如...

$eventType = OptionsetField::create(
    'Type',
    _t('CalendarEvent.EVENTTYPE','Type'),
    array (
    'music'  => _t('CalendarEvent.MUSIC','Music'),
    'sport'  => _t('CalendarEvent.SPORT','Sport'),
    'drama'  => _t('CalendarEvent.DRAMA','Drama'),
    'custom' => _t('CalendarEvent.CUSTOM','Custom'),
    )
);

$fEventCustomType = TextField::create('EventCustomType','Event type')
    ->displayIf('Type')->isEqualTo('custom');

$f->addFieldsToTab("Root.Main", array($eventType,$fEventCustomType));

作为替代方案,如果您想拯救 This module,那么您可以创建它以将其保存到一个字段中,因为它的设计目的是按照您的要求...但它有一个错误(上次我试过了)所以现在仅供参考。

最后,我用单独的字段弄明白了:

    $eventType = OptionsetField::create(
        'Type',
        _t('CalendarEvent.EVENTTYPE','Type'),
        array (
        'music'  => _t('CalendarEvent.MUSIC','Music'),
        'sport'  => _t('CalendarEvent.SPORT','Sport'),
        'drama'  => _t('CalendarEvent.DRAMA','Drama'),
        'custom' => _t('CalendarEvent.CUSTOM','Custom'),
        )
    );

    $customEventType = TextField::create('EventCustomType','Custom type');

    $f->addFieldsToTab("Root.Main", array($eventType,$customEventType));

和jQuery:

$('#Root_Main').entwine({
    onmatch: function(){
        var $tab = this.closest('.tab');
        var $eventType = $tab.find('ul.eventType');
        var $customType = $tab.find('.customEventType').hide();

        if($eventType.find('input:checked').val() == 'custom'){
            $customType.show();
        }

        $eventType.find('input').change(function(){
            if($(this).val() == 'custom'){
                $customType.show();
            }else{
                $customType.hide();
            }
        });
    }
});