Skip to content

Commit e525b54

Browse files
committed
MAGEDOC-3430: Clarify Storefront customization
Updated content after reading more about DI.
1 parent 79ff6a0 commit e525b54

File tree

3 files changed

+112
-43
lines changed

3 files changed

+112
-43
lines changed
Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,63 @@
11
# Storefront customization
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 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.
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
46-
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.
34+
## Step 2: Configure the widget initializer
4835

49-
Add the following configuration to the `etc/di.xml` file in your custom module directory:
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:
5037

5138
``` xml
39+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
5240
<type name="Magento\PageBuilder\Model\WidgetInitializerConfig">
5341
<arguments>
5442
<argument name="config" xsi:type="array">
55-
<item name="%content-type-name%" xsi:type="array">
43+
<item name="example_quote" xsi:type="array">
5644
<!-- Name is the appearance name -->
5745
<item name="default" xsi:type="array">
5846
<!--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>
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>
6950
</argument>
7051
</arguments>
7152
</type>
72-
```
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: 78 additions & 0 deletions
Loading

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

57.9 KB
Loading

0 commit comments

Comments
 (0)