douyun1546 2016-06-05 10:12
浏览 67
已采纳

在设置表单上设置默认值在Octobercms中返回null

I create a little plugin for the OctoberCMS but now dealing with a problem of setting a default value for a settings form.

I used database storage for my plugin settings (Backend -> Settings tab -> My Plugin tab) which works , quite good, except I cannot set default values.

I have a fields.yaml with this content:

tabs:
    fields:
        reviews_border_color:
            label: 'Field1'
            span: left
            tab: 'review_sticker'
            required: 1
            type: text
            default: '#F2F2F2'
        rating_star_size:
            label: 'Field2'
            span: left
            tab: 'review_sticker'
            required: 1
            type: number
            default: 14

I retrieve the settings in my components like this:

<?php namespace Codedge\TestPlugin\Components;

use Cms\Classes\ComponentBase;
use Codedge\TestPlugin\Models\Settings;

class TestComponent extends ComponentBase
{
    public $starSize;
    public $borderColor;

    public function componentDetails()
    {
        return [
            'name'        => 'Test Component',
            'description' => '...'
        ];
    }

    public function defineProperties()
    {
        return [];
    }

    public function init()
    {
        $this->starSize = Settings::get('rating_star_size'); // not giving a default value when empty, default is 14
        $this->borderColor = Settings::get('reviews_border_color'); // not giving a default value when empty, default is '#F2F2F2'
    }

}

I also tried to set the default value, as second param in the get() method as suggested in the OctoberCMS docs:

public function init()
{
    $this->starSize = Settings::get('rating_star_size', 14); // returns null
    $this->borderColor = Settings::get('reviews_border_color', '#F2F2F2'); // returns null
}

So any suggestions how I can reliably set a default value if none is specified/entered in to the settings form field?

Update:

After some digging aroung I found the explanation in the method getSettingsValue() in system/behaviors/SettingsModel.php

/**
 * Get a single setting value, or return a default value
 */
public function getSettingsValue($key, $default = null)
{
    if (array_key_exists($key, $this->fieldValues)) {
        return $this->fieldValues[$key];
    }

    return $default;
}

As my field exists in $this->fieldValues it returns it empty to me instead of returning the $default value.

So what I thought, returning the default value if the field value is empty, is not covered.

Any suggestions?

  • 写回答

2条回答 默认 最新

  • doutao1171 2016-06-06 20:10
    关注

    After consulting with the OctoberCMS team on Github, the current behaviour is intented/not a bug - see https://github.com/octobercms/october/issues/2094 . There could be considerations to change this behaviour, but this is another story.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方