2017 © Pedro Peláez
 

silverstripe-module silverstripe-serialised-dbfields

Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects.

image

unclecheese/silverstripe-serialised-dbfields

Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects.

  • Saturday, October 7, 2017
  • by unclecheese
  • Repository
  • 1 Watchers
  • 9 Stars
  • 4,748 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 2 Open issues
  • 1 Versions
  • 5 % Grown

The README.md

Serialised DB Fields for SilverStripe

Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects., (*1)

Installation

composer require unclecheese/silverstripe-serialised-dbfields, (*2)

If you plan on editing the serialised data in the CMS, you'll probably want to install CodeEditorField., (*3)

composer require nathancox/codeeditorfield, (*4)

The database fields are set up to automatically scaffold CodeEditorField when available., (*5)

Usage

mysite/code/MyPageType.php, (*6)

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, (*7)

{
  "Category": "Lollies",
  "BannerImage": "/path/to/image.png",
  "Products": [
    {
      "Title": "Snake",
      "Colour": "Red"
    },
    {
      "Title": "Jet plane",
      "Colour": "Purple"
    }
  ]
}

themes/mytheme/templates/Layout/MyPageType.ss, (*8)

<% 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 %>

Casting

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., (*9)

Price: Currency|20.00
StartDate: Date|2016-01-01

Tests

framework/sake dev/tests/SerialisedDBFeildsTest, (*10)

Troubleshooting

Ring Uncle Cheese., (*11)

The Versions

07/10 2017

dev-master

9999999-dev https://github.com/unclecheese/silverstripe-serialised-dbfields

Provides serialised data (YAML, JSON) in text fields that are traverseable as nested ViewableData objects.

  Sources   Download

BSD-3-Clause

The Requires

 

json yaml silverstripe dbfield