|
1 | 1 | # How to add an appearance
|
2 | 2 |
|
3 |
| -This topic describes how to add a new appearance to a content type so that you can give end-users the custom features they require when creating content. |
| 3 | +Appearances provide different layout and styling options for a content type. For example, the Banner content type has four appearances. Each appearance shows a different layout for the Banner's text and button. |
| 4 | + |
| 5 | +This topic shows how to add appearances to existing content types. An overview of the steps and the files you need to add are illustrated here: |
4 | 6 |
|
5 | 7 | 
|
| 8 | +_Steps for adding appearances_ |
| 9 | + |
| 10 | +Even though this topic describes how to add appearances to Page Builder's native content types, the steps can be applied to adding more appearances to your custom content types as well. |
| 11 | + |
| 12 | +## Step 1: Add VisualSelect class |
| 13 | + |
| 14 | +First, we need a way select an appearance within the Admin UI. In Page Builder, we use the `VisualSelect` class. This class provides the UI for selecting different appearances. For example, the `VisualSelect` class for the Banner provides four appearances to choose from: |
| 15 | + |
| 16 | + |
| 17 | +_Visual selector for Banner_ |
| 18 | + |
| 19 | +To add a new appearance option for one of Page Builder's native content types, like the Banner, create a new `di.xml` file in your module, as shown here: |
| 20 | + |
| 21 | + |
| 22 | +_File and location for the VisualSelect class_ |
| 23 | + |
| 24 | +Within the `di.xml` file, add a `VisualSelect` class using the Banner's `VisualSelect` class name (`<virtualType name="AppearanceSourceBanner"`), as shown here: |
| 25 | + |
| 26 | +```xml |
| 27 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> |
| 28 | + <virtualType name="AppearanceSourceBanner" type="Magento\PageBuilder\Model\Source\VisualSelect"> |
| 29 | + <arguments> |
| 30 | + <argument name="optionsSize" xsi:type="string">large</argument> |
| 31 | + <argument name="optionsData" xsi:type="array"> |
| 32 | + <item name="4" xsi:type="array"> |
| 33 | + <item name="value" xsi:type="string">simple-poster</item> |
| 34 | + <item name="title" xsi:type="string" translate="true">Simple Poster</item> |
| 35 | + <item name="icon" xsi:type="string">Example_PageBuilderBannerAppearance/css/images/content-type/banner/appearance/simple-poster.svg</item> |
| 36 | + </item> |
| 37 | + </argument> |
| 38 | + </arguments> |
| 39 | + </virtualType> |
| 40 | +</config> |
| 41 | +``` |
| 42 | +_VisualSelect class for new Banner appearance_ |
| 43 | + |
| 44 | +Using the same class name as the Banner ensures that Magento's XML merging will add your new visual appearance option to the existing Banner appearance options. |
| 45 | + |
| 46 | +The following table describes the configuration arguments for each appearance option. |
| 47 | + |
| 48 | +| Arguments | Description | |
| 49 | +| --------------- | ------------------------------------------------------------ | |
| 50 | +| `optionsSize` | The size of the icons that display each option. Use `large` for appearance options. | |
| 51 | +| `optionsData` | Grouping array for all the appearance options of a content type. | |
| 52 | +| `item array` | Grouping array of properties that define an appearance option. The `name="4"` signifies the index order for displaying the option. The Banner's existing options stop with an index of 3 (`name="3"`), ensuring that our new appearance option is positioned after the last Banner option. | |
| 53 | +| `value` | Assigns the unique key used in the component dataSource. | |
| 54 | +| `title` | Display name for the appearance option. Banner example: Poster. | |
| 55 | +| `icon` | Path to the `.svg` icon for the appearance: `view/adminthtml/web/css/images/content-type/[content-type-name]/appearance/*.svg`. See Creating an icon for your appearance | |
| 56 | +| `noticeMessage` | (Not shown in example.) The `noticeMessage` displays a message below the appearance options when the appearance is selected. For example, two of the Row appearances (`full-width` and `full-bleed`) define `noticeMessage` strings that display when selected. | |
| 57 | + |
| 58 | +To add more appearance options, simply create more ` item` arrays, as shown here: |
| 59 | + |
| 60 | +```xml |
| 61 | +<item name="5" xsi:type="array"> |
| 62 | + <item name="value" xsi:type="string">tall</item> |
| 63 | + <item name="title" xsi:type="string" translate="true">Tall</item> |
| 64 | + <item name="icon" xsi:type="string">Example_PageBuilderExtensionBanner/css/images/content-type/banner/appearance/tall.svg</item> |
| 65 | +</item> |
| 66 | +<item name="6" xsi:type="array"> |
| 67 | + <item name="value" xsi:type="string">short</item> |
| 68 | + <item name="title" xsi:type="string" translate="true">Short</item> |
| 69 | + <item name="icon" xsi:type="string">Example_PageBuilderBannerExtensionBanner/css/images/content-type/banner/appearance/short.svg</item> |
| 70 | +</item> |
| 71 | +``` |
| 72 | +_Example of additional appearance options_ |
| 73 | + |
| 74 | +### Creating an appearance icon |
| 75 | + |
| 76 | +Appearance icons are `.svg` files that graphically depict the layout of an appearance. Use the following SVG template as a starting place for creating your own appearance icon: |
| 77 | + |
| 78 | +```xml |
| 79 | +<svg xmlns="http://www.w3.org/2000/svg" width="218" height="110" viewBox="0 0 218 110"> |
| 80 | + <g fill="none" fill-rule="evenodd"> |
| 81 | + <rect width="149" height="69" x="35" y="21" fill="#524D49"/> |
| 82 | + <rect width="45" height="16" x="87" y="60" fill="#FFF"/> |
| 83 | + <rect width="72" height="4" x="73" y="34" fill="#FFF"/> |
| 84 | + <rect width="60" height="4" x="79" y="43" fill="#FFF"/> |
| 85 | + </g> |
| 86 | +</svg> |
| 87 | +``` |
| 88 | +_SVG appearance template_ |
| 89 | + |
| 90 | +You can use whatever tool you want to create your SVG. Just make sure you follow these specific dimensions to ensure your icon fits in with the existing appearance icons: |
| 91 | + |
| 92 | +| Property | Value | |
| 93 | +| ---------------------------- | ----------- | |
| 94 | +| `svg width` | 218 | |
| 95 | +| `svg height` | 110 | |
| 96 | +| `svg viewbox` | 0 0 218 110 | |
| 97 | +| `g fill` | none | |
| 98 | +| background `rect width` | 149 | |
| 99 | +| background `rect height` | 69 | |
| 100 | +| background `rect x` | 35 | |
| 101 | +| background `rect y` | 21 | |
| 102 | +| background `rect fill` | #524D49 | |
| 103 | +| other `rect fill` properties | #FFF | |
| 104 | + |
| 105 | +## Step 2: Add appearance configurations |
| 106 | + |
| 107 | +Appearance configurations connect the data entered in a content type's form to its HTML templates. For example, the Heading content type has an `<html>` config element (`<html name="heading_text" />`) that maps the text entered into the content type's `heading_text` form field (`<field name="heading_text" formElement="input">`) to the Heading's Knockout template binding (`html="data.main.html"`), as illustrated here: |
| 108 | + |
| 109 | + |
| 110 | +_Appearance configurations explained_ |
| 111 | + |
| 112 | +The same concept applies to `styles`, `attributes`, and `css` elements. These elements ensure that the form settings (both content and styles) get applied to the templates as expected. |
| 113 | + |
| 114 | +To add a new Banner appearance configuration, create a content type config file named `banner.xml`, as shown here: |
| 115 | + |
| 116 | + |
| 117 | +_Add additional appearances to Banner content type_ |
| 118 | + |
| 119 | +{: .bs-callout .bs-callout-info } |
| 120 | +This procedure applies to any native content type you want to extend. In other words, you can use the file name (`heading.xml`) and type name (`<type name="heading">`) of any native Page Builder content type to extend it based on Magento's XML merging behavior. |
| 121 | + |
| 122 | +An example `banner.xml` config file for a new `simple-poster` appearance is shown here: |
| 123 | + |
| 124 | +```xml |
| 125 | +<?xml version="1.0"?> |
| 126 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_PageBuilder:etc/content_type.xsd"> |
| 127 | + <type name="banner"> |
| 128 | + <children default_policy="deny"/> |
| 129 | + <appearances> |
| 130 | + <appearance name="simple-poster" |
| 131 | + preview_template="Example_PageBuilderBannerAppearance/content-type/banner/simple-poster/preview" |
| 132 | + master_template="Example_PageBuilderBannerAppearance/content-type/banner/simple-poster/master" |
| 133 | + reader="Magento_PageBuilder/js/master-format/read/configurable"> |
| 134 | + <elements> |
| 135 | + <element name="main"> |
| 136 | + <style name="display" source="display" converter="Magento_PageBuilder/js/converter/style/display" preview_converter="Magento_PageBuilder/js/converter/style/preview/display"/> |
| 137 | + <style name="margins" storage_key="margins_and_padding" reader="Magento_PageBuilder/js/property/margins" converter="Magento_PageBuilder/js/converter/style/margins"/> |
| 138 | + <attribute name="name" source="data-content-type"/> |
| 139 | + <attribute name="appearance" source="data-appearance"/> |
| 140 | + <attribute name="show_button" source="data-show-button"/> |
| 141 | + <attribute name="show_overlay" source="data-show-overlay"/> |
| 142 | + <css name="css_classes"/> |
| 143 | + </element> |
| 144 | + . . . |
| 145 | + </appearance> |
| 146 | + </appearances> |
| 147 | + </type> |
| 148 | +</config> |
| 149 | +``` |
| 150 | +_Additional simple-poster appearance for the Banner content type_ |
| 151 | + |
| 152 | +## Step 3: Add appearance forms |
| 153 | + |
| 154 | +Appearances for a content type can share the same form or use different forms. For example, the Products content type uses two different forms, one for each appearance. The Grid appearance uses the `pagebuilder_products_form.xml` to provide options for displaying products in a grid. While the Carousel appearance uses the `pagebuilder_products_carousel_form.xml` to provide extra options for displaying products in a carousel. |
| 155 | + |
| 156 | +To add a form for an appearance, add a `<form>` element as a child of the `<appearance>` element in your content type's config file. As mentioned, the Products content type does this for its Carousel appearance, shown here: |
| 157 | + |
| 158 | +```xml |
| 159 | +<appearance name="carousel" |
| 160 | + preview_template="Magento_PageBuilder/content-type/products/grid/preview" |
| 161 | + master_template="Magento_PageBuilder/content-type/products/grid/master" |
| 162 | + reader="Magento_PageBuilder/js/master-format/read/configurable"> |
| 163 | + <form>pagebuilder_products_carousel_form</form> |
| 164 | + <elements> |
| 165 | + . . . |
| 166 | +``` |
| 167 | +_Adding a form to an appearance_ |
| 168 | + |
| 169 | +## Step 4: Add appearance templates |
| 170 | + |
| 171 | +Appearances use different HTML templates to create different layouts and apply different styles to those layouts. For example, the Banner content type uses four sets of `preview.html` and `master.html` templates, to create the visual differences for each content type: |
| 172 | + |
| 173 | + |
| 174 | +_Banner appearance templates_ |
| 175 | + |
| 176 | +Each template set is located within a folder named after the appearance for which they are used. The folder names must match the appearance names assigned in the content type's config file. |
| 177 | + |
| 178 | +To create appearance templates for additional Banner appearances, such as the `simple-poster` appearance described in steps 1 and 2, you must add a new appearance folder for your module's templates, as shown here: |
| 179 | + |
| 180 | + |
| 181 | +_Adding additional appearance templates_ |
| 182 | + |
| 183 | +## Step 5: Add appearance styles |
| 184 | + |
| 185 | +Appearances use different CSS/LESS files to create different visual styles for a content type. For example, the Banner content type uses a different `.less` file for each appearance, as shown here: |
| 186 | + |
| 187 | + |
| 188 | +_Banner stylesheets_ |
| 189 | + |
| 190 | +To create a LESS stylesheet for an additional Banner appearances, such as the `simple-poster`, you must add a new appearance folder for your module's templates, as shown here: |
| 191 | + |
| 192 | + |
| 193 | +_Adding additional stylesheets_ |
| 194 | + |
| 195 | +The `_module.less` file is an import file that ensures the additional stylesheet gets added to the existing Banner styles. The `_module.xml` file for our `simple-poster.less` file looks like this: |
| 196 | + |
| 197 | +```scss |
| 198 | +@import "_simple-poster"; |
| 199 | +``` |
| 200 | +_Use _module.less for import statements_ |
| 201 | + |
| 202 | +## Conclusion |
| 203 | + |
| 204 | +Using appearances to extend Page Builder's native content types represents one of Page Builder's best practices for creating a variety of new content building blocks based on existing content types. We hope this topic has helped adequately describe this best practice. |
| 205 | + |
| 206 | +## Example Module |
6 | 207 |
|
7 |
| -## Step 1: Add appearance to config file |
| 208 | +An example module for this topic is available for download in the `pagebuilder-examples` repository here: |
8 | 209 |
|
9 |
| -In progress... |
| 210 | +https://github.com/magento-devdocs/pagebuilder-examples/tree/master/Example/PageBuilderBannerAppearance |
0 commit comments