https://github.com/arillo/silverstripe-arbitrarysettings
Add multiple settings to a DataObject using a single field.
https://github.com/arillo/silverstripe-arbitrarysettings
php silverstripe
Last synced: about 1 year ago
JSON representation
Add multiple settings to a DataObject using a single field.
- Host: GitHub
- URL: https://github.com/arillo/silverstripe-arbitrarysettings
- Owner: arillo
- License: mit
- Created: 2017-01-09T09:34:53.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-12-09T14:37:49.000Z (over 1 year ago)
- Last Synced: 2025-03-23T22:25:14.917Z (over 1 year ago)
- Topics: php, silverstripe
- Language: PHP
- Homepage: https://arillo.github.io/silverstripe-arbitrarysettings/
- Size: 69.3 KB
- Stars: 4
- Watchers: 8
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
README
# arillo/silverstripe-arbitrarysettings
Extends a DataObject with a mutil value field to store arbitrary settings in it.
### Requirements
SilverStripe CMS ^5.0
- For a SilverStripe 4.x compatible version of this module, please see the [2 branch](https://github.com/arillo/silverstripe-arbitrarysettings/tree/2.x).
- For a SilverStripe 3.x compatible version of this module, please see the [1 branch, or 0.x release line](https://github.com/arillo/silverstripe-arbitrarysettings/tree/1.0).
### Usage
Add and setup the extension on your DataObject
e.g. in config.yml:
```yml
MyDataObject:
extensions:
# adds a field called ArbitrarySettings
- Arillo\ArbitrarySettings\SettingsExtension
# define your settings
settings:
show_title:
options:
0: 'No'
1: 'Yes'
default: 0
label: 'Show title as image caption?'
description: 'Additional description goes here'
image_alignment:
options:
'left': 'Left'
'right': 'Right'
default: 'left'
label: 'Image alignment'
```
**Note:** All keys should be alphanumeric (including underscores, haven't tested other special characters yet) and should not contain whitespace.
To add the field in CMS you can use a helper method to show the field:
```php
use Arillo\ArbitrarySettings\SettingsExtension;
public function getCMSFields()
{
$fields = parent::getCMSFields();
if ($settingsField = SettingsExtension::field_for($this))
{
$fields->addFieldToTab('Root.Main', $settingsField);
}
return $fields;
}
```
Values can be accessed like this:
```php
$this->SettingByName('image_alignment') // returns 'left' or 'right'
```
in templates:
```html
...
```
`SettingsField` has functions available to manipulate the source of the field:
For including or excluding certain setting you can use:
```php
// will show all settings but show_title
$settingsField->exclude(['show_title']);
// will show show_title setting only
$settingsField->include(['show_title']);
```
It is also possible to update the default value for a setting (for sure only if its present as an option):
```php
$settingsField->updateDefaultForKey('show_title', 1);
```
### Settings presets
It is possible to define a list of setting presets like this:
```yml
Arillo\ArbitrarySettings\SettingsExtension:
presets:
bg:
options:
transparent: 'Transparent'
light: 'Light blue'
default: transparent
label: 'Background color'
imgType:
options:
Default: 'Default image'
Hero: 'Hero image'
default: Default
label: 'Image type'
```
With these presets defined it is possible to reference these keys in your DataObject's settings config, e.g.:
```yml
MyDataObject:
extensions:
- Arillo\ArbitrarySettings\SettingsExtension
# define your settings
settings:
- bg
- imgType
```
### Translations
To translate the form field label used by `SettingsExtension::field_for` can be changed like this:
```yml
en:
Arillo\ArbitrarySettings\SettingsExtension:
Label: 'Options'
```
To translate options follow the following convention:
```yml
# for a config like this:
MyObject:
settings:
show_title:
options:
0: 'No'
1: 'Yes'
default: 0
label: 'Show title as image caption?'
# the following translation keys can be used:
en:
MyObject:
setting_show_title_option_0: 'Nope'
setting_show_title_option_1: 'Yep'
setting_show_title_label: 'Use title as image caption'
```