Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Dynamic override of Settings Model fields

0
by jaxwilko, last modified on June 15th, 2021

Overwriting the Settings Model's getFieldConfig() method allows you to dynamically modify field configuration used for storing settings.

The following allows for overriding the configuration specified in fields.yaml only when the config key app.acme_plugin_settings is populated.

<?php

namespace Acme\Plugin\Models;

use Model;

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

    public $settingsCode = 'acme_plugin_settings';

    public $settingsFields = 'fields.yaml';

    protected $cache = [];

    protected $fieldConfig = null;

    /**
     * Returns the field configuration used by this model.
     */
    public function getFieldConfig()
    {
        if ($this->fieldConfig !== null) {
            return $this->fieldConfig;
        }

        if ($config = config('app.acme_plugin_settings', [])) {
            return $this->fieldConfig = (object) $config;
        }

        return $this->fieldConfig = $this->makeConfig($this->settingsFields);
    }
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?