{"id":18296542,"url":"https://github.com/fiedsch/contao-jsonwidget","last_synced_at":"2025-08-10T22:48:06.751Z","repository":{"id":47203747,"uuid":"119847473","full_name":"fiedsch/contao-jsonwidget","owner":"fiedsch","description":"Contao Widget for JSON Data","archived":false,"fork":false,"pushed_at":"2024-01-02T14:15:49.000Z","size":44,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-29T03:49:16.205Z","etag":null,"topics":["contao","dca","json-data"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fiedsch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-01T14:35:06.000Z","updated_at":"2025-02-22T08:17:30.000Z","dependencies_parsed_at":"2023-11-09T21:42:15.758Z","dependency_job_id":"c3f87c4c-ba4f-4fc5-83f4-61c0cfcfa43b","html_url":"https://github.com/fiedsch/contao-jsonwidget","commit_stats":{"total_commits":35,"total_committers":1,"mean_commits":35.0,"dds":0.0,"last_synced_commit":"cdca679f5fa19c9fd46f0d4a9e7bdbce4acd09ac"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/fiedsch/contao-jsonwidget","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fiedsch%2Fcontao-jsonwidget","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fiedsch%2Fcontao-jsonwidget/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fiedsch%2Fcontao-jsonwidget/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fiedsch%2Fcontao-jsonwidget/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fiedsch","download_url":"https://codeload.github.com/fiedsch/contao-jsonwidget/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fiedsch%2Fcontao-jsonwidget/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269803680,"owners_count":24477647,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contao","dca","json-data"],"created_at":"2024-11-05T14:41:37.028Z","updated_at":"2025-08-10T22:48:06.679Z","avatar_url":"https://github.com/fiedsch.png","language":"PHP","readme":"# JSON and YAML Widgets for Contao\n\nThe `jsonWidget` can be used in DCA files to create a text field that contains a JSON string.\nWhile saving it will be checked if that the string is valid JSON. \nThe widget displays the JSON string with `JSON_PRETTY_PRINT` so that checking/finding errors \nis easier for the user.\n\nThe `yamlWidget` is mostly the same, except that it uses the YAML format.\n  \n\n## Example: extending Members\n\n### DCA\n\n```php\n$GLOBALS['TL_DCA']['tl_member']['fields']['json_data'] = [\n   'inputType' =\u003e 'jsonWidget',\n   'label'     =\u003e \u0026$GLOBALS['TL_LANG']['tl_member']['json_data'],\n   'eval'      =\u003e ['tl_class' =\u003e 'long', 'decodeEntities' =\u003e true], \n   'sql'       =\u003e \"blob NULL\",\n ];\n \n // Add json_data to $GLOBALS['TL_DCA']['tl_member']['palettes']['default'] \n // where ever you like\n ```\nOther valid options in `eval` are the same as for `textarea`s (as `WidgetJSON` extends `TextArea`), \nexcept that setting `rte` will be ignored because the editors provided do not make sense here. \n\n\n### How to use the JSON data?\n\nExtend `tl_member` as in the above example. Then create an `ExtendedMemberModel` that \nextends Contao's `MemberModel`. In the magic methodd `__set()` and `_get` you can intercept\nthe \"fields\" stored in `json_data`. The `Fiedsch\\JsonWidgetBundle\\JsonGetterSetterTrait` takes \ncare of that:\n\n```php\n// models/ExtendedMemberModel.php\nnamespace Contao;\n\nuse Fiedsch\\JsonWidgetBundle\\Traits\\JsonGetterSetterTrait;\n\nclass ExtendedMemberModel extends MemberModel\n{\n    // let __set() and __get() take care of the JSON or YAML data (both at the same time will not work!)\n    use JsonGetterSetterTrait;\n    // or (see above!)\n    use YamlGetterSetterTrait;\n\n  /**\n    * The column name we selected for the `jsonWidget` in the example above\n    * @var string\n    */\n    protected static $strJsonColumn = 'my_json_data_column';\n    \n    /**\n      * Same thing for the `yamlWidget`\n      * @var string\n      */\n    protected static $strYamlColumn = 'my_yaml_data_column';\n\n}\n```\n\n```php\n// config/config.php\n$GLOBALS['TL_MODELS']['tl_member'] = 'Contao\\ExtendedMemberModel';\n```\n\n\n### Using the Model\n\n```php\n$member = \\ExtendedMemberModel::findById(42);\n\n// access fields columns created by contao's default DCA\nprintf(\"read member %s %s\\n\", $member-\u003efirstname, $member-\u003elastname);\n\n// access a field stored in our JSON data column\nprintf(\"transparently accessing a field from the JSON data ... '%s'\\n\", $member-\u003ewhatever);\n\n// set values and store in database\n$member-\u003ea_key_for_a_scalar_value = \"fourtytwo\";\n$member-\u003ekey_for_an_array = ['an','array','containing','some','strings'];\n$member-\u003esave(); // Note that saving will lose comments in your YAML-data \n                 // as Symfony\\Component\\Yaml\\Yaml will not save them \n```\n\n\n### YAML-Syntax Highlighting with ACE\n\nSet \n```php\n'eval' =\u003e [ /* ... , */ 'rte'=\u003e'ace|yaml'],\n```\nin your field's DCA definitions.\n\nQuick and dirty way: add desired CSS-Rules like e.g. \n```css\n.ace_comment {\n  color: red !important;\n}\n```\nto your `be_ace.html5` (which you create it if you do not yet have a custom version).\nOR: use a custom backend style and add the CSS rules there.\n\n\n### Set JSON (or JAML) Data \n\nIf you want to set all the JSON (or YAML) data at once you cannot use\n```php\n# In this example we assume $strJsonColumn = 'json_data';\n$data = [ 'foo' =\u003e 1, 'bar' =\u003e 'baz ];\n$model-\u003ejson_data = $data; # will throw an exception\n```\nTo do this you have to use\n```php\n$data = [ 'foo' =\u003e 1, 'bar' =\u003e 'baz ];\n$model-\u003esetJsonColumnData(array $data);\n# or\n# $model-\u003esetYamlColumnData(array $data);\n```\nNote that this way previously set JSON (or YAML) data is overwritten.\n\nAlso note that the behaviour of `setJsonColumnData([])` (when setting an empty data array) changed in version `0.5.0`. Previously  it created `[]` where it \nnow creates `{}` in the respective database column. \n\nIn version `0.7.0` we slightly changed this again to achieve the original goal: The storage of empty arrays as object (`{}`) is only enforced on the top level \nof the data. In lower levels, empty arrays will be stored as arrays (`[]`) not as \"empty\" objects (`{}`): compare\n`{ \"foo\": [] }` (versions \u003e= 0.7.0) to `{ \"foo\": {} }` (versions \u003e=0.5.0 and \u003c0.7.0). \n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffiedsch%2Fcontao-jsonwidget","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffiedsch%2Fcontao-jsonwidget","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffiedsch%2Fcontao-jsonwidget/lists"}