Skip to content

Commit 33ed9d4

Browse files
committed
Merge branch 'MAGEDOC-3432' into develop
# Conflicts: # docs/configurations/storefront-configuration.md # docs/images/how-to-add-storefront-widget.svg
2 parents bfed3f8 + ed5a6b1 commit 33ed9d4

File tree

5 files changed

+212
-69
lines changed

5 files changed

+212
-69
lines changed

docs/getting-started/introduction.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ See [Install Page Builder](install-pagebuilder.md)
4444
* [Block chooser configuration](../configurations/block-chooser-configuration.md)
4545
* [Full-width page layout configuration](../configurations/full-width-page-layout-configuration.md)
4646
* [Responsive layout configuration](../configurations/responsive-layout-configuration.md)
47-
* [Storefront configuration](../configurations/storefront-configuration.md)
4847
* [Selector configuration](../configurations/selector-configuration.md)
4948
* [Product conditions configuration](../configurations/product-conditions-configuration.md)
5049
* [Server-side rendered previews](../configurations/server-side-rendered-previews.md)
@@ -53,11 +52,10 @@ See [Install Page Builder](install-pagebuilder.md)
5352

5453
* [How to develop a container content type](../how-to/how-to-develop-container-content-type.md)
5554
* [How to use the Image Uploader](../how-to/how-to-use-image-uploader.md)
56-
* [How to use the Image Uploader](../how-to/how-to-use-image-uploader.md)
5755
* [How to add inline text editing](../how-to/how-to-add-inline-text-editing.md)
5856
* [How to add a custom toolbar](../how-to/how-to-add-custom-toolbar.md)
57+
* [How to add a storefront widget](../how-to/how-to-add-storefront-widget.md)
5958
* [How to convert product attribute fields to use Page Builder](../how-to/how-to-convert-product-attributes-to-use-pagebuilder.md)
60-
* [How to store a component master format as a widget-directive](../how-to/how-to-store-master-format-as-widget-directive.md)
6159

6260
### Reference
6361

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Storefront customization
2+
3+
A storefront widget is JavaScript code that handles the behavior of a content type after Page Builder renders it on the storefront. For example, the Tabs and Sliders have their own storefront widgets to handle the end-user's tapping of tabs and the swiping of slides.
4+
5+
Adding a storefront widget to your content type is a simple two-step process:
6+
7+
8+
9+
![How to add storefront widget](../images/how-to-add-storefront-widget.svg)
10+
11+
12+
13+
## Step 1: Create the JavaScript
14+
15+
Name your JavaScript file `widget.js` and put it in the following directory structure: `/view/base/web/js/content-type/{content-type-name}/appearance/{appearance-name}/widget.js`. An example from the PageBuilderQuote content type follows:
16+
17+
![Where to add storefront widget](../images/where-to-add-widget.png){:width="477px" height="auto"}
18+
19+
The JavaScript for the widget can handle events, initialize third-party controls, or do whatever else you need in order to control your content type's behavior _after_ Page Builder renders the master format template on the frontend.
20+
21+
``` javascript
22+
define([
23+
'jquery',
24+
], function ($) {
25+
'use strict';
26+
27+
return function (config, element) {
28+
var element = $(element);
29+
console.log("ELEMENT: " + element.data('element'));
30+
};
31+
});
32+
```
33+
34+
## Step 2: Configure the widget initializer
35+
36+
To configure your `widget.js` so that Page Builder can initialize and load it on the storefront, create a new `WidgetInitializerConfig` type in your module's global `di.xml` file (`etc/di.xml`). Then add your configuration (that includes your widget) as a replacement argument. The following example is from the PageBuilderQuote module:
37+
38+
``` xml
39+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
40+
<type name="Magento\PageBuilder\Model\WidgetInitializerConfig">
41+
<arguments>
42+
<argument name="config" xsi:type="array">
43+
<item name="example_quote" xsi:type="array">
44+
<!-- Name is the appearance name -->
45+
<item name="default" xsi:type="array">
46+
<!--required argument-->
47+
<item name="component" xsi:type="string">Example_PageBuilderQuote/js/content-type/example-quote/appearance/default/widget</item>
48+
</item>
49+
</item>
50+
</argument>
51+
</arguments>
52+
</type>
53+
</config>
54+
```
55+
56+
The XML configuration loads the widget on the frontend, and on the stage, so you can preview content inside both the block and dynamic block content types.
57+
58+
**^^^ I don't understand the reference to blocks and dynamic blocks in the previous sentence.**
59+
60+
**^^^ Is the `WidgetInitializerConfig` class just automatically instantiated for the frontend? When is it instantiated? During the rendering of the storefront/frontend?**
61+
62+
63+
Lines changed: 41 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 an image uploader
22

3-
## What's in this topic
3+
This topic describes how to add the image uploader component to your content type so that end-users can add images from the Admin stage as needed.
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+
The first step is to customize the image uploader to suit your needs. To do this, you must add the `additional_data` element to your content type's config file to create the data types and values needed to initialize the image uploader for your specific needs.
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,5 @@ 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+
{: .bs-callout .bs-callout-info }
95+
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)