├── .github ├── FUNDING.yml └── mergify.yml ├── .gitignore ├── changelog.md ├── composer.json ├── license.md ├── readme.md └── src ├── AddonServiceProvider.php └── resources └── views └── fields └── json.blade.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [ziming] 4 | -------------------------------------------------------------------------------- /.github/mergify.yml: -------------------------------------------------------------------------------- 1 | pull_request_rules: 2 | - name: automatic approval for Dependabot pull requests 3 | conditions: 4 | - author~=^dependabot(|-preview)\[bot\]$ 5 | actions: 6 | review: 7 | type: APPROVE 8 | message: Automatically approving dependabot 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | composer.lock 3 | docs 4 | vendor 5 | coverage 6 | .idea 7 | .phpunit.result.cache 8 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to the package will be documented in this file. 4 | 5 | ## 0.1.0 - 2020-10-29 6 | 7 | ### Added 8 | - Initial Commit 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ziming/json-field-for-backpack", 3 | "description": "A Json Editor Field for Backpack", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "ziming", 8 | "email": "ziming.opensource@gmail.com" 9 | }, 10 | { 11 | "name": "vesper8" 12 | }, 13 | { 14 | "name": "stephanus-stuff" 15 | } 16 | ], 17 | "homepage": "https://github.com/ziming/json-field-for-backpack", 18 | "keywords": ["Laravel", "Backpack", "Backpack for Laravel", "Addon", "Admin Panel", "Json", "Field"], 19 | "require": { 20 | "ext-json": "*", 21 | "backpack/crud": "^4.1.0|^5.0|^6.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Ziming\\JsonFieldForBackpack\\": "src/" 26 | } 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "Ziming\\JsonFieldForBackpack\\AddonServiceProvider" 32 | ] 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # JSON Field for Backpack 5 and 6 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Total Downloads][ico-downloads]][link-downloads] 5 | 6 | This package provides a ```json``` field type for the [Backpack for Laravel](https://backpackforlaravel.com/) administration panel. The field allows the admin to manually edit the contents of a column where a JSON is stored, using the [`josdejong/jsoneditor`](https://github.com/josdejong/jsoneditor) JS plugin. That means they'll have: 7 | - multiple modes of viewing the JSON (code, tree, form) 8 | - syntax highlighting 9 | - indented code 10 | - color picker 11 | - searching 12 | - undo & redo 13 | - etc. 14 | 15 | Of course, this field should only be used when the admin is savvy enough to know what JSON is. Otherwise they can completely mess up the structure/format of the JSON stored in the database column. 16 | 17 | ## Screenshots 18 | 19 | ![https://user-images.githubusercontent.com/1032474/97699650-e2e92b80-1a80-11eb-8320-3ac35e8a59a1.gif](https://user-images.githubusercontent.com/1032474/97699650-e2e92b80-1a80-11eb-8320-3ac35e8a59a1.gif) 20 | 21 | Backpack v6 with Tabler, light and dark modes 22 | ![https://github.com/ziming/json-field-for-backpack/assets/82019078/bdd36562-d46c-4966-ad02-4e765675eae4](https://github.com/ziming/json-field-for-backpack/assets/82019078/bdd36562-d46c-4966-ad02-4e765675eae4) 23 | 24 | ## Installation 25 | 26 | Via Composer 27 | 28 | ``` bash 29 | composer require ziming/json-field-for-backpack 30 | ``` 31 | 32 | ## Usage 33 | 34 | Inside your custom CrudController: 35 | 36 | ```php 37 | $this->crud->addField([ 38 | 'name' => 'column_name', 39 | 'type' => 'json', 40 | 'view_namespace' => 'json-field-for-backpack::fields', 41 | 42 | // OPTIONAL 43 | 44 | // Which modes should the JsonEditor JS plugin allow? 45 | // Please note that the first mode in the array will be used as the default mode. 46 | 'modes' => ['form', 'tree', 'code'], 47 | 48 | // Default value, if needed. If there is an actual value in the json column, 49 | // it will do an array_merge_recursive(), with the json column values 50 | // replacing the ones with the same keys. 51 | 'default' => [], 52 | ]); 53 | ``` 54 | 55 | Notice the ```view_namespace``` attribute - make sure that is exactly as above, to tell Backpack to load the field from this _addon package_, instead of assuming it's inside the _Backpack\CRUD package_. 56 | 57 | 58 | ## Overwriting 59 | 60 | If you need to change the field in any way, you can easily publish the file to your app, and modify that file any way you want. But please keep in mind that you will not be getting any updates. 61 | 62 | **Step 1.** Copy-paste the blade file to your directory: 63 | ```bash 64 | # create the fields directory if it's not already there 65 | mkdir -p resources/views/vendor/backpack/crud/fields 66 | 67 | # copy the blade file inside the folder we created above 68 | cp -i vendor/ziming/json-field-for-backpack/src/resources/views/fields/json.blade.php resources/views/vendor/backpack/crud/fields/json.blade.php 69 | ``` 70 | 71 | **Step 2.** Remove the vendor namespace wherever you've used the field: 72 | ```diff 73 | $this->crud->addField([ 74 | - 'view_namespace' => 'json-field-for-backpack::fields' 75 | ]); 76 | ``` 77 | 78 | **Step 3.** Uninstall this package. Since it only provides one file - ```json.blade.php```, and you're no longer using that file, it makes no sense to have the package installed: 79 | 80 | ```bash 81 | composer remove ziming/json-field-for-backpack 82 | ``` 83 | 84 | ## Change log 85 | 86 | Please see the [changelog](changelog.md) for more information on what has changed recently. 87 | 88 | 89 | ## Security 90 | 91 | If you discover any security related issues, please email [the author](composer.json) instead of using the issue tracker. 92 | 93 | ## Credits 94 | 95 | - [ziming](https://github.com/ziming) - Created the initial field type 96 | - [vesper8](https://github.com/vesper8) - Polished & fixed bugs in the field type to allow multiple instances of the field 97 | - [stephanus-stuff](https://github.com/stephanus-stuff) - For updating the field type to be compatible with Backpack 4.0 and 4.1 98 | - [Cristian Tabacitu](https://github.com/tabacitu) - For creating Backpack. 99 | - [josdejong](https://github.com/josdejong) - For creating [jsoneditor](https://github.com/josdejong/jsoneditor) 100 | 101 | - [All Contributors][link-contributors] 102 | 103 | ## License 104 | 105 | MIT. Please see the [license file](license.md) for more information. 106 | 107 | [ico-version]: https://img.shields.io/packagist/v/ziming/json-field-for-backpack.svg?style=flat-square 108 | [ico-downloads]: https://img.shields.io/packagist/dt/ziming/json-field-for-backpack.svg?style=flat-square 109 | 110 | [link-packagist]: https://packagist.org/packages/ziming/json-field-for-backpack 111 | [link-downloads]: https://packagist.org/packages/ziming/json-field-for-backpack 112 | [link-author]: https://github.com/ziming 113 | [link-contributors]: ../../contributors 114 | -------------------------------------------------------------------------------- /src/AddonServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(realpath(__DIR__.'/resources/views'), 'json-field-for-backpack'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/resources/views/fields/json.blade.php: -------------------------------------------------------------------------------- 1 | {{-- json field based on: https://github.com/josdejong/jsoneditor --}} 2 | @php 3 | $value = new stdClass(); 4 | 5 | if (old($field['name'])) { 6 | $value = old($field['name']); 7 | } elseif (isset($field['value']) && isset($field['default'])) { 8 | $value = array_merge_recursive($field['default'], $field['value']); 9 | } elseif (isset($field['value'])) { 10 | $value = $field['value']; 11 | } elseif (isset($field['default'])) { 12 | $value = $field['default']; 13 | } 14 | 15 | // if attribute casting is used, convert to JSON 16 | if (is_array($value) || is_object($value) ) { 17 | $value = json_encode($value); 18 | } 19 | 20 | // bug in laravel @json blade directive, it cannot accept an array of more than 3 items directly. 21 | // hence the need to declare a variable here 1st then pass to @json later. 22 | $defaultModes = ['form', 'tree', 'code', 'preview', 'text']; 23 | @endphp 24 | 25 | @if ($crud->checkIfFieldIsFirstOfItsType($field, $fields)) 26 | @push('crud_fields_styles') 27 | 28 | @endpush 29 | 30 | @push('crud_fields_scripts') 31 | 32 | 35 | @endpush 36 | @endif 37 | 38 | @include('crud::fields.inc.wrapper_start') 39 | 40 | 41 |
42 | 43 | 46 | 47 | @if (isset($field['hint'])) 48 |

{!! $field['hint'] !!}

49 | @endif 50 | @include('crud::fields.inc.wrapper_end') 51 | 52 | @push('crud_fields_scripts') 53 | 68 | 74 | @endpush 75 | --------------------------------------------------------------------------------