|
1 |
| -# Storefront customization |
| 1 | +# How to add a storefront widget |
2 | 2 |
|
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. |
4 | 4 |
|
5 |
| -## Step 1: Create a JavaScript widget |
| 5 | +Adding a storefront widget to your content type is a simple two-step process: |
6 | 6 |
|
7 |
| -Create a JavaScript widget in your module's `/view/frontend/web/js/content-type/{content-type-name}/appearance/{appearance-name}/widget.js` file: |
8 | 7 |
|
9 |
| -``` javascript |
10 |
| -/** |
11 |
| - * Copyright © Magento, Inc. All rights reserved. |
12 |
| - * See COPYING.txt for license details. |
13 |
| - */ |
14 | 8 |
|
| 9 | + |
| 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 | +{: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 |
15 | 22 | define([
|
16 | 23 | 'jquery',
|
17 |
| - 'slick' |
18 | 24 | ], function ($) {
|
19 | 25 | 'use strict';
|
20 | 26 |
|
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')); |
40 | 30 | };
|
41 | 31 | });
|
42 |
| - |
43 | 32 | ```
|
44 | 33 |
|
45 |
| -## Step 2: Add XML configuration |
| 34 | +## Step 2: Configure the widget initializer |
46 | 35 |
|
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: |
50 | 37 |
|
51 | 38 | ``` 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> |
64 | 48 | </item>
|
65 |
| - <!--optional if you want load widget per appearance--> |
66 |
| - <item name="appearance" xsi:type="string">default</item> |
67 | 49 | </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 | + |
0 commit comments