Skip to content

Commit

Permalink
NEW: Compliments 39204a9 via displaying JSON data in the selected CMS…
Browse files Browse the repository at this point in the history
… input fields.
  • Loading branch information
phptek committed Sep 27, 2016
1 parent 32cc9c8 commit 0d7657c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion code/extensions/JSONTextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Add this DataExtension to your models if you wish to have specific DB fields
* receive JSON data POSTed from particular input fields in the CMS.
*
* You'll need to declare declare a `$jsontext_field_map` config static on the desired
* You'll need to declare declare a `$jsontext_field_map` config static on the decorated
* model(s) as follows:
*
* <code>
Expand Down Expand Up @@ -52,4 +52,31 @@ public function onBeforeWrite()
parent::onBeforeWrite();
}

/**
* Ensure any CMS input fields used with a {@link JSONText} field, show the
* appropriate value taken from the stored JSON data once data is saved.
*
* Note: UNSTABLE API, logic assumes a specific JSON structure.
*
* @param FieldList $fields
* @return void
*/
public function updateCMSFields(\FieldList $fields)
{
$fieldMap = $this->getOwner()->config()->get('jsontext_field_map');

foreach ($fieldMap as $dbFieldName => $inputFields) {
$dbObject = $this->getOwner()->dbObject($dbFieldName);
$jsonData = $dbObject->getStoreAsArray();

foreach ($inputFields as $inputField) {
foreach ($jsonData as $i => $array) {
if (isset($jsonData[$i][$inputField])) {
$this->getOwner()->setField($inputField, $array[$inputField]);
}
}
}
}
}

}
4 changes: 2 additions & 2 deletions docs/en/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ Example:
```

You can also take input from standard CMS input fields, convert those fields' data into JSON
and save it to a specific DB field. You need to declare a special `$jsontext_field_map` config
static on each model who's fields you wish to behave in this way.
and save to a specific DB field. You need to extend your desired models with the `JSONTextExtension`
and declare a special `$jsontext_field_map` config static on each decorated model who's fields you wish to behave in this way.

In the example below, `MyDBField1` will receive JSON data comprising the combined data of
`MyInputField1` and `MyInputField2` CMS input fields, but not `MyDBField2` which will be inserted into
Expand Down

0 comments on commit 0d7657c

Please sign in to comment.