Skip to content

Commit 7fc00f8

Browse files
committed
MAGEDOC-3432: Clarify image uploader topic
1 parent 25dbf33 commit 7fc00f8

File tree

2 files changed

+148
-64
lines changed

2 files changed

+148
-64
lines changed
Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
# How to use the Image Uploader
1+
# How to add the Image Uploader
22

3-
## What's in this topic
3+
This topic describes how to add the image Uploader component to your content type. Find the source code for the Uploader here: `app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/uploader.ts`).
44

5-
This topic describes how to add a reusable image uploader component to the PageBuilder stage for a content type.
5+
![How to add an image uploader](../images/how-to-add-image-uploader.svg)
66

7-
## Overview
7+
## Step 1: Add configuration data for the Uploader
88

9-
To add image uploader customization to PageBuilder:
10-
- [Add configuration for the uploader](#add-configuration-for-the-uploader)
11-
- [Update the `<YourModule>/view/adminhtml/web/js/content-type/<content_type_name>/preview.js` file {#js-file}](#js-file)
12-
- [Update the preview template to display the uploader component {#preview}](#preview)
13-
14-
## Add configuration for the uploader {#add-configuration-for-the-uploader}
15-
16-
Use `additional_data` in your `<YourModule>/view/base/pagebuilder/content_type/<content-type-name>.xml` XML config file to add the image uploader custom configuration to a content type:
9+
Use the `additional_data` element in your content type's config file to add a custom configuration for the image Uploader:
1710

1811
``` xml
12+
<?xml version="1.0"?>
1913
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_PageBuilder:etc/content_type.xsd">
20-
<type name="MyName" translate="label" label="MyName" icon="icon-modulename-simple" component="Vendor_ModuleName/js/content-type" form="modulename_simple_form" menu_section="layout">
21-
...
14+
<type name="example_quote"
15+
...
2216
<additional_data>
2317
<item name="uploaderConfig" xsi:type="array">
2418
<item name="isShowImageUploadInstructions" xsi:type="boolean">false</item>
@@ -49,64 +43,46 @@ Use `additional_data` in your `<YourModule>/view/base/pagebuilder/content_type/<
4943
</config>
5044
```
5145

52-
## Update the `<YourModule>/view/adminhtml/web/js/content-type/<content_type_name>/preview.js` file {#js-file}
53-
54-
To update the `<YourModule>/view/adminhtml/web/js/content-type/<content_type_name>/preview.js` file:
55-
56-
1. Import the 'Magento_PageBuilder/js/content-type/uploader' component as a dependency:
57-
58-
``` js
59-
define(['Magento_PageBuilder/js/content-type/uploader'], function (Uploader) {
60-
```
61-
62-
**Constructor arguments**
63-
64-
| Argument | Type | Description | Required | Default |
65-
| ------------------ | --------- | ----------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------- |
66-
| `name` | String | Used to locate the component in the UI registry once it is created. | yes | None |
67-
| `uploaderConfig` | Object | Initializes the image uploader UI component. | yes | None |
68-
| `contentTypeId` | String | The ID of the parent content type this will be used in. | yes | None |
69-
| `dataStore` | DataStore | The DataStore that the selected image should be stored in. | yes | None |
70-
| `initialValue` | Object[] | The value to be used for the initial state of the component. | yes | None |
71-
| `onChangeCallback` | Function | A callback that will be called when an image is selected. | no | The image will be saved to the provided `dataStore` using `uploaderConfig.dataScope` as the key. |
72-
| `onDeleteCallback` | Function | A callback that will be called when the current used image is deleted from storage. | no | The image will be removed from to the provided `dataStore` using `uploaderConfig.dataScope` as the key. |
46+
## Step 2: Create an instance of the Uploader
7347

74-
The following is an example extracted from the image content type:
48+
To create an instance of the image uploader in your preview component (`preview.js`), import the `Magento_PageBuilder/js/content-type/uploader` component as a dependency and call the Uploader constructor, passing in your content type's configuration options (added in step 1) and the other required arguments, as shown here :
7549

76-
```js
77-
var dataStore = this.parent.dataStore.getState();
78-
var initialImageValue = dataStore[this.config.additional_data.uploaderConfig.dataScope] || "";
79-
80-
this.uploader = new Uploader(
81-
'imageuploader_' + this.parent.id,
82-
this.config.additional_data.uploaderConfig,
83-
this.parent.id,
84-
this.parent.dataStore,
85-
initialImageValue,
86-
);
87-
```
50+
``` js
51+
define(['Magento_PageBuilder/js/content-type/uploader'], function (Uploader) {
52+
53+
Preview.prototype.getUploader = function () {
54+
var initialImageValue = this.contentType.dataStore
55+
.get(this.config.additional_data.uploaderConfig.dataScope, "");
56+
57+
return new Uploader(
58+
"imageuploader_" + this.contentType.id,
59+
this.config.additional_data.uploaderConfig,
60+
this.contentType.id,
61+
this.contentType.dataStore,
62+
initialImageValue,
63+
);
64+
};
65+
```
8866
89-
2. Add configuration for the uploader in the `<content-type-name>.xml` file to initialize the uploader.
67+
### Uploader constructor arguments
9068
91-
3. Register the listener to specify when the image is loaded from the uploader UI component:
9269
93-
``` js
94-
/**
95-
* Get registry callback reference to uploader UI component
96-
*
97-
* @returns {Uploader}
98-
*/
99-
public getUploader() {
100-
return this.uploader;
101-
}
102-
```
70+
| Argument | Type | Description | Required | Default |
71+
| ------------------ | --------- | ----------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------- |
72+
| `name` | String | Used to locate the component in the UI registry. | yes | None |
73+
| `uploaderConfig` | Object | Initializes the image uploader UI component with the configuration settings from the `additional_data` element. | yes | None |
74+
| `contentTypeId` | String | The ID of the content type you are adding the Uploader to. | yes | None |
75+
| `dataStore` | DataStore | The DataStore to store the selected image in. | yes | None |
76+
| `initialValue` | Object[] | The image value to set for the initial state of the Uploader component. | yes | None |
77+
| `onChangeCallback` | Function | The callback to execute when the end-user selects an image. | no | Magento saves the image to the provided `dataStore` using `uploaderConfig.dataScope` as the key. |
78+
| `onDeleteCallback` | Function | The callback to execute when the end-user deletes the current image from storage. | no | Magento removes the image from to the provided `dataStore` using `uploaderConfig.dataScope` as the key. |
10379
104-
## Update the preview template to display the uploader component {#preview}
80+
## Step 3: Add markup for the Uploader
10581
106-
Update the preview template file, `bluefoot/app/code/Magento/PageBuilder/view/adminhtml/web/template/content-type/<content-type-name>/<ppearance_name>/preview.html`, to display the uploader component:
82+
To add the image Uploader to your preview template (`preview.html`), use Knockout's `scope` binding element to render an instance of your configured Uploader component from the Magento registry, as shown here:
10783
10884
``` html
109-
<div>
85+
<div ...>
11086
...
11187
<scope args="getUploader().getUiComponent()">
11288
<render />
@@ -115,4 +91,6 @@ Update the preview template file, `bluefoot/app/code/Magento/PageBuilder/view/ad
11591
</div>
11692
```
11793
118-
**Note:** When a file is deleted from the media browser, the `fileDeleted` event is triggered on the window with the `mediabrowser` namespace. The passed argument is an object containing the `ids` property, which is an array of ID strings for each of the deleted files. The IDs of the selected files are provided in the objects dispatched by the `addFile` and `processFile` methods inside the image uploader UI Component.
94+
95+
{: .bs-callout .bs-callout-info }
96+
When an end-user deletes a file from the media browser, Magento triggers the `fileDeleted` event on the window with the `mediabrowser` namespace. The passed argument is an object containing the `ids` property, which is an array of ID strings for each of the deleted files. Magento adds the IDs of the selected files in the objects dispatched by the `addFile` and `processFile` methods inside the image Uploader component.

0 commit comments

Comments
 (0)