-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: Allow CMS feld-data to be converted to JSON and stored to a spec…
…ific DB field.
- Loading branch information
Showing
4 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* 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 | ||
* model(s) as follows: | ||
* | ||
* <code> | ||
* private static $jsontext_field_map = [ | ||
* 'MyDBField1' => [ | ||
* 'MyInputField1', | ||
* 'MyInputField2' | ||
* ], | ||
* 'MyDBField2' => [ | ||
* 'MyInputField3', | ||
'MyInputField4' | ||
* ] | ||
* ]; | ||
* <code> | ||
* | ||
* @package silverstripe-jsontext | ||
* @subpackage extensions | ||
* @author Russell Michell <[email protected]> | ||
*/ | ||
|
||
class JSONTextExtension extends \DataExtension | ||
{ | ||
/** | ||
* Manipulate POSTed data from pre-specified CMS fields and write their data | ||
* as JSON. | ||
* | ||
* @return void | ||
*/ | ||
public function onBeforeWrite() | ||
{ | ||
// Fields are declared in a config static on decorated models | ||
$fieldMap = $this->getOwner()->config()->get('jsontext_field_map'); | ||
$postVars = Controller::curr()->getRequest()->postVars(); | ||
$data = []; | ||
|
||
foreach ($fieldMap as $dbFieldName => $inputFields) { | ||
foreach ($inputFields as $inputField) { | ||
$data[$dbFieldName][] = [$inputField => $postVars[$inputField]]; | ||
} | ||
|
||
$jsonTextField = $this->getOwner()->dbObject($dbFieldName); | ||
$this->getOwner()->setField($dbFieldName, $jsonTextField->toJSON($data[$dbFieldName])); | ||
} | ||
|
||
parent::onBeforeWrite(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters