Skip to content

Commit fc4d48d

Browse files
committed
MAGEDOC-3430: Clarify Storefront customization
1 parent 9c58907 commit fc4d48d

File tree

3 files changed

+120
-53
lines changed

3 files changed

+120
-53
lines changed
Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,61 @@
1-
# Storefront customization
1+
# How to add a storefront widget
22

3-
You can customize Page Builder content types by adding your own logic on the frontend, as described here.
3+
A storefront widget is a JavaScript component that handles the behavior of a content type after being rendered on the storefront. For example, the Tabs and Sliders use their own storefront widgets to handle the end-user's tapping of tabs and the swiping of slides.
44

5-
## Step 1: Create a JavaScript widget
5+
Adding a storefront widget to your content type is a simple two-step process:
66

7-
Create a JavaScript widget in your module's `/view/frontend/web/js/content-type/{content-type-name}/appearance/{appearance-name}/widget.js` file:
87

9-
``` javascript
10-
/**
11-
* Copyright © Magento, Inc. All rights reserved.
12-
* See COPYING.txt for license details.
13-
*/
148

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
1522
define([
1623
'jquery',
17-
'slick'
1824
], function ($) {
1925
'use strict';
2026

21-
return function (config, sliderElement) {
22-
23-
var $element = $(sliderElement);
24-
25-
/**
26-
* Prevent each slick slider from being initialized more than once which could throw an error.
27-
*/
28-
if ($element.hasClass('slick-initialized')) {
29-
$element.slick('unslick');
30-
}
31-
32-
$element.slick({
33-
autoplay: $element.data('autoplay') === 1,
34-
autoplaySpeed: $element.data('autoplay-speed') || 0,
35-
fade: $element.data('fade') === 1,
36-
infinite: $element.data('is-infinite') === 1,
37-
arrows: $element.data('show-arrows') === 1,
38-
dots: $element.data('show-dots') === 1
39-
});
27+
return function (config, element) {
28+
var element = $(element);
29+
console.log("ELEMENT: " + element.data('element'));
4030
};
4131
});
42-
4332
```
4433

45-
## Step 2: Add XML configuration
34+
## Step 2: Configure the widget initializer
4635

47-
The XML configuration loads the widget on the frontend, and on the stage, so that you can preview content inside both the block and dynamic block content types.
48-
49-
Add the following configuration to the `etc/di.xml` file in your custom module directory:
36+
To configure your `widget.js` to so that Page Builder can initialize and load it on the storefront, create a new `WidgetInitializerConfig` type in your module's `etc/di.xml` file. An example of the initializer from the PageBuilderQuote module follows:
5037

5138
``` xml
52-
<type name="Magento\PageBuilder\Model\WidgetInitializerConfig">
53-
<arguments>
54-
<argument name="config" xsi:type="array">
55-
<item name="%content-type-name%" xsi:type="array">
56-
<!-- Name is the appearance name -->
57-
<item name="default" xsi:type="array">
58-
<!--required argument-->
59-
<item name="component" xsi:type="string">%{vendor-path}/js/content-type/{content-type-name}/appearance/{appearance-name}/widget%</item>
60-
<!--optional if you need provide some config for your widget-->
61-
<item name="config" xsi:type="array">
62-
<item name="buttonSelector" xsi:type="string">.pagebuilder-slide-button</item>
63-
<item name="showOverlay" xsi:type="string">hover</item>
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>
6448
</item>
65-
<!--optional if you want load widget per appearance-->
66-
<item name="appearance" xsi:type="string">default</item>
6749
</item>
68-
</item>
69-
</argument>
70-
</arguments>
71-
</type>
72-
```
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+
**[Can you briefly explain how Page Builder loads the widget on the frontend using this `di.xml` file? Thanks.]**
61+
Lines changed: 78 additions & 0 deletions
Loading

docs/images/where-to-add-widget.png

57.9 KB
Loading

0 commit comments

Comments
 (0)