简体   繁体   English

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

[英]Setting a default value on settings form return null in Octobercms

I create a little plugin for the OctoberCMS but now dealing with a problem of setting a default value for a settings form. 我为OctoberCMS创建了一个小插件,但现在遇到了为设置表单设置默认值的问题。

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: 我有一个具有以下内容的fields.yaml

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 : 我还尝试设置默认值,作为OctoberCMS文档中建议的get()方法中的第二个参数:

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 经过一番挖掘后,我在system/behaviors/SettingsModel.php中的getSettingsValue()方法中找到了解释。

/**
 * 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. 由于我的字段存在于$this->fieldValues因此它对我返回空值,而不是返回$default值。

So what I thought, returning the default value if the field value is empty, is not covered. 因此,我认为,如果字段值为空,则返回默认值的问题并未涵盖。

Any suggestions? 有什么建议么?

Why don't you use initSettingsData function, you can use it like this: 为什么不使用initSettingsData函数,您可以像这样使用它:

class Settings extends Model
{
    public $implement = ['System.Behaviors.SettingsModel'];

    // A unique code
    public $settingsCode = 'my-settings';

    // Reference to field configuration
    public $settingsFields = 'fields.yaml';

    public function initSettingsData()
    {
        $this->admin_email = 'some@email.com';
        $this->admin_name = 'Walid Ammar';
        $this->other_settings = 'other value';
    }
}

After consulting with the OctoberCMS team on Github, the current behaviour is intented/not a bug - see https://github.com/octobercms/october/issues/2094 . 与Github上的OctoberCMS团队协商后,当前的行为是有意的/不是错误-请参阅https://github.com/octobercms/october/issues/2094 There could be considerations to change this behaviour, but this is another story. 可能会考虑更改此行为,但这是另一回事了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM