Dynamic override of Settings Model fields
0
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);
}
}
There are no comments yet
Be the first one to comment