Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects.
Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects.
composer require unclecheese/silverstripe-serialised-dbfields
If you plan on editing the serialised data in the CMS, you'll probably want to install CodeEditorField.
composer require nathancox/codeeditorfield
The database fields are set up to automatically scaffold CodeEditorField
when available.
mysite/code/MyPageType.php
class MyPageType extends Page {
private static $db = [
'MyJSONData' => 'JSONField',
'MyYAMLData' => 'YAMLField'
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.JSONData', CodeEditorField::create('MyJSONData')
->setMode('json')
);
$fields->addFieldToTab('Root.YAMLData', CodeEditorField::create('MyYAMLData')
->setMode('yaml')
);
return $fields;
}
}
$MyJSONData
{
"Category": "Lollies",
"BannerImage": "/path/to/image.png",
"Products": [
{
"Title": "Snake",
"Colour": "Red"
},
{
"Title": "Jet plane",
"Colour": "Purple"
}
]
}
themes/mytheme/templates/Layout/MyPageType.ss
<% with $MyJSONData %>
Category: $Category<br>
<img src="$BannerImage"><br>
<ul>
$Products.count total products
<% loop $Products %>
<li>$Title ($Colour)</li>
<% end_loop %>
</ul>
<% end_with %>
By default, all scalar values fall back on the $default_cast
setting of ViewableData
(Text). To customise the casting, use the |
symbol in your field value.
Price: Currency|20.00
StartDate: Date|2016-01-01
framework/sake dev/tests/SerialisedDBFeildsTest
Ring Uncle Cheese.
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem