【问题标题】:Create custom field inside OptionsetField in Silverstripe CMS 3.1在 Silverstripe CMS 3.1 的 OptionsetField 中创建自定义字段
【发布时间】:2016-07-13 15:01:18
【问题描述】:

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

所以我在 $db 中创建了这个字段,如下所示: '类型' => '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 之前插入标签“自定义”并将它们设置在同一行中。

另外,我不确定是否需要为自定义字段设置第二个字段。我可以在“类型”字段中插入自定义值或验证它吗?

感谢您的任何建议

【问题讨论】:

    标签: php content-management-system silverstripe


    【解决方案1】:

    这可以通过为“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 作为替代方案,那么您可以创建它以保存到一个字段中,因为它旨在按照您的要求执行...但它有一个错误(我上次尝试)所以仅供参考。

    【讨论】:

      【解决方案2】:

      最后,我用单独的字段弄清楚了:

          $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();
                  }
              });
          }
      });
      

      【讨论】:

      • 出于兴趣,为什么显示逻辑没有用?
      • 我无法通过 composer 安装它:问题 1 - webbuilders-group/silverstripe-display-logic-extras ^0.1.0 的安装请求 -> webbuilders-group/silverstripe-display- 可满足逻辑附加[0.1.0]。 - 结论:删除 silverstripe/framework 3.1.18 - 结论:不要安装 silverstripe/framework 3.1.18 - webbuilders-group/silverstripe-display-logic-extras 0.1.0 需要 silverstripe/framework ^3.2 -> 可满足 silverstripe/框架[3.2.x-dev, 3.3.x-dev, 3.4.x-dev, ...]。 ... 4 多行。另一个插件给了我一些错误(自 3 年以来没有更新)
      • 哦,天哪,我发现它更容易,我在github.com/unclecheese/silverstripe-display-logic/blob/master/… 中没有看到对 webbuilders-group 的引用,我认为您可能选择了错误的内容。 webbuilders-group 是 unclecheese 的扩展
      • 是的。你说的对。但是,我认为它需要来自 composer.json 的 ss 3.2: "require": { "silverstripe/framework": "^3.2" },所以我使用的是 3.1.18 :(
      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多