Skip to content

Commit d847a9c

Browse files
authored
Apply suggestions from code review
1 parent ea3aee8 commit d847a9c

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

7.x-dev/crud-fields.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ CRUD::field([
874874
875875
#### Uploading files with summernote
876876

877-
Summernote saves images as base64 encoded strings in the database. If you want to save them as files on the server, you can use the [Summernote File Upload](https://backpackforlaravel.com/docs/7.x/crud-uploaders) . Please note that the Summernote Uploader is part of the `backpack/pro` package.
877+
Summernote saves images as base64 encoded strings in the database. If you want to save them as files on the server, you can use the [Summernote Uploader](https://backpackforlaravel.com/docs/7.x/crud-uploaders). Please note that the Summernote Uploader is part of the `backpack/pro` package.
878878
Input preview:
879879

880880
![CRUD Field - summernote](https://backpackforlaravel.com/uploads/docs-4-2/fields/summernote.png)

7.x-dev/crud-uploaders.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ We've already created Uploaders for the most common scenarios:
7070

7171
Do you want to create your own Uploader class, for your custom field? Here's how you can do that, and how Uploader classes work behind the scenes.
7272

73-
First thing you need to decide if you need "Ajax" or "Non-Ajax" upload. The big difference is that the "non-ajax" uploaders process the file upload when you submit your form, while the ajax upload process the file using a javascript ajax request, so it can be uploaded before you submit the form.
73+
First thing you need to decide if you are creating a _non-ajax_ or _ajax_ uploader:
74+
- _non-ajax_ uploaders process the file upload when you submit your form;
75+
- _ajax_ uploaders process the file upload before the form is submitted, by submitting an AJAX request using Javascript;
76+
77+
<a name="how-to-create-a-custom-non-ajax-uploader"></a>
78+
## How to Create a Custom Non-Ajax Uploader
7479

7580
First let's see how to create a non-ajax uploader, for that we will create a `CustomUploader` class that extends the abstract class `Uploader`.
7681

@@ -107,7 +112,7 @@ CRUD::field('avatar')->type('upload')->withFiles([
107112
]);
108113
```
109114

110-
But most likely, you have a `custom_upload` field that you'd like to use this uploader without having to specify it every time. You can do that by adding it to the `UploadersRepository` in your Service Provider `boot()` method:
115+
If you custom uploader was created to work for a custom field (say it's called `custom_upload`), you can tell Backpack to always use this uploader for that field type - that way you don't have to specify it every time you use the field. You can do that in your Service Provider `boot()` method, by adding it to the `UploadersRepository`:
111116

112117
```php
113118
// in your App\Providers\AppServiceProvider.php
@@ -118,11 +123,11 @@ protected function boot()
118123
}
119124
```
120125

121-
You can now use `CRUD::field('avatar')->type('custom_upload')->withFiles();` and it will use your custom uploader. What happen behind the scenes is that Backpack will register your uploader to be ran in 3 different model events: `saving`, `retrieved` and `deleting`.
126+
You can now use `CRUD::field('avatar')->type('custom_upload')->withFiles();` and it will use your custom uploader. What happens behind the scenes is that Backpack will register your uploader to run on 3 different model events: `saving`, `retrieved` and `deleting`.
122127

123128
The `Uploader` class has 3 "entry points" for the mentioned events: **`storeUploadedFiles()`**, **`retrieveUploadedFiles()`** and **`deleteUploadedFiles()`**. You can overwrite these methods in your custom uploader to add your custom logic but for most uploaders you will not need to overwrite them as they are "setup" methods for the action that will be performed, and after setup they call the relevant methods that each uploader will implement, like ```uploadFiles()``` or ```uploadRepeatableFiles()```.
124129

125-
The base uploader class has most of the functionality implemented and use **"strategy methods"** to configure the underlying behavior.
130+
Notice this custom class you're creating is extending `Backpack\CRUD\app\Library\Uploaders\Uploader`. That base uploader class has most of the functionality implemented and uses **"strategy methods"** to configure the underlying behavior.
126131

127132
**`shouldUploadFiles`** - a method that returns a boolean to determine if the files should be uploaded. By default it returns true, but you can overwrite it to add your custom logic.
128133

@@ -152,9 +157,11 @@ protected function shouldUploadFiles($value): bool
152157
// when the value is an instance of UploadedFile, it means the file was uploaded and we should upload it
153158
return is_a($value, 'Illuminate\Http\UploadedFile', true);
154159
}
155-
```
156160

157-
For the ajax uploaders, the process is similar, but the `CustomUploader` class should extend `BackpackAjaxUploader` **(requires backpack/pro)** instead of `Uploader`.
161+
<a name="how-to-create-a-custom-ajax-uploader"></a>
162+
## How to Create a Custom Ajax Uploader
163+
164+
For the ajax uploaders, the process is similar, but your custom uploader class should extend `BackpackAjaxUploader` instead of `Uploader` (**note that this requires backpack/pro**).
158165

159166
```php
160167

@@ -186,15 +193,11 @@ In addition to the field configuration, ajax uploaders require that you use the
186193
Similar to model events, there are two "setup" methods for those endpoints: **`processAjaxEndpointUploads()`** and **`deleteAjaxEndpointUpload()`**. You can overwrite them to add your custom logic but most of the time you will not need to do that and just implement the `uploadFiles()` and `uploadRepeatableFiles()` methods.
187194

188195
The ajax uploader also has the same "strategy methods" as the non-ajax uploader (see above), but adds a few more:
189-
**`ajaxEndpointSuccessResponse($files = null)`** - this should return a `JsonResponse` with the needed information when the upload is successful. By default it returns a json response with the file path.
190-
191-
**`ajaxEndpointErrorResponse($message)`** - use this method to change the endpoint response in case the upload failed. Similar to the success it should return a `JsonResponse`.
192-
193-
**`getAjaxEndpointDisk()`** - by default a `temporaryDisk` is used to store the files before they are moved to the final disk. (when uploadFiles() is called). You can overwrite this method to change the disk used.
194-
195-
**`getAjaxEndpointPath()`** - by default the path is `/temp` but you can overwrite this method to change the path used.
196-
197-
**`getDefaultAjaxEndpointValidation()`** - Should return the default validation rules (in the format of `BackpackCustomRule`) for the ajax endpoint. By default it returns a `ValidGenericAjaxEndpoint` rule.
196+
- **`ajaxEndpointSuccessResponse($files = null)`** - This should return a `JsonResponse` with the needed information when the upload is successful. By default it returns a json response with the file path.
197+
- **`ajaxEndpointErrorResponse($message)`** - Use this method to change the endpoint response in case the upload failed. Similar to the success it should return a `JsonResponse`.
198+
- **`getAjaxEndpointDisk()`** - By default a `temporaryDisk` is used to store the files before they are moved to the final disk (when uploadFiles() is called). You can overwrite this method to change the disk used.
199+
- **`getAjaxEndpointPath()`** - By default the path is `/temp` but you can override this method to change the path used.
200+
- **`getDefaultAjaxEndpointValidation()`** - Should return the default validation rules (in the format of `BackpackCustomRule`) for the ajax endpoint. By default it returns a `ValidGenericAjaxEndpoint` rule.
198201

199202

200203
For any other customization you would like to perform, please check the source code of the `Uploader` and `BackpackAjaxUploader` classes.

0 commit comments

Comments
 (0)