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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题