|
| 1 | +# How to add additional configurations |
| 2 | + |
| 3 | +<!-- {% raw %} --> |
| 4 | + |
| 5 | +## What's in this topic |
| 6 | + |
| 7 | +This topic describes how to extend and configure Page Builder content types to accommodate any preferred setting that is not addressed in the confines of our existing `content_type.xsd` schema definition. |
| 8 | + |
| 9 | +`additional_data` allows you to provide extra configuration, such as `maxFileSize` or `allowedExtensions`, for various content types. |
| 10 | + |
| 11 | +For example, if you want to load data from the backend, you can use objects, `xsi:type="object"`, to implement `Magento\PageBuilder\Model\Config\ContentType\AdditionalData\ProviderInterface` to process the data and dynamically load information based on the system config. |
| 12 | + |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +To add customization to a Page Builder content type: |
| 17 | + |
| 18 | +1. [Add additional data to the XML config](#additional-data) |
| 19 | +2. [Implement `ProviderInterface` for conversion](#conversion) (Optional, as it is only required if you're using `xsi:type="object"` to obtain dynamic configuration at runtime) |
| 20 | + |
| 21 | +## Add additional data to the XML config {#additional-data} |
| 22 | + |
| 23 | +Use `additional_data` in your `Vendor/ModuleName/view/adminhtml/pagebuilder/content_type/<your-content-type>.xml` XML config file to add custom configuration to a content type: |
| 24 | + |
| 25 | +``` xml |
| 26 | +<additional_data> |
| 27 | + <item name="uploaderConfig" xsi:type="array"> |
| 28 | + <item name="maxFileSize" xsi:type="object">ImageMaxFileSizeDesktop</item> |
| 29 | + <item name="allowedExtensions" xsi:type="string">jpg jpeg gif png</item> |
| 30 | + <item name="component" xsi:type="string">Magento_PageBuilder/js/form/element/image-uploader</item> |
| 31 | + <item name="componentType" xsi:type="string">imageUploader</item> |
| 32 | + <item name="dataScope" xsi:type="string">image</item> |
| 33 | + <item name="formElement" xsi:type="string">imageUploader</item> |
| 34 | + <item name="uploaderConfig" xsi:type="array"> |
| 35 | + <item name="url" xsi:type="object">Magento\PageBuilder\Model\Config\ContentType\AdditionalData\Provider\Uploader\SaveUrl</item> |
| 36 | + </item> |
| 37 | + <item name="previewTmpl" xsi:type="string">Magento_PageBuilder/form/element/uploader/preview</item> |
| 38 | + <item name="template" xsi:type="string">Magento_PageBuilder/form/element/uploader/preview/image</item> |
| 39 | + <item name="mediaGallery" xsi:type="array"> |
| 40 | + <item name="openDialogUrl" xsi:type="object">Magento\PageBuilder\Model\Config\ContentType\AdditionalData\Provider\Uploader\OpenDialogUrl</item> |
| 41 | + <item name="openDialogTitle" xsi:type="string" translate="true">Insert Images...</item> |
| 42 | + <item name="initialOpenSubpath" xsi:type="string">wysiwyg</item> |
| 43 | + <item name="storeId" xsi:type="object">\Magento\PageBuilder\Model\Config\ContentType\AdditionalData\Provider\StoreId</item> |
| 44 | + </item> |
| 45 | + <item name="validation" xsi:type="array"> |
| 46 | + <item name="required-entry" xsi:type="boolean">true</item> |
| 47 | + </item> |
| 48 | + </item> |
| 49 | +</additional_data> |
| 50 | + ``` |
| 51 | + |
| 52 | +## Implement `ProviderInterface` for conversion {#conversion} |
| 53 | + |
| 54 | +Array and scalar types, `xsi:type="array"` and `xsi:type="string"` for example (but also boolean, integer, and constant), designated in the XML file are provided as-is to the additional data configuration payload. |
| 55 | + |
| 56 | +Object content types, `xsi:type="object"`, must implement `ProviderInterface` in `/app/code/Magento/PageBuilder/Model/Config/ContentType/AdditionalData/ProviderInterface.php` to be converted: |
| 57 | + |
| 58 | +``` php |
| 59 | +<?php |
| 60 | +/** |
| 61 | + * Copyright © Magento, Inc. All rights reserved. |
| 62 | + * See COPYING.txt for license details. |
| 63 | + */ |
| 64 | + |
| 65 | +declare(strict_types=1); |
| 66 | + |
| 67 | +namespace Magento\PageBuilder\Model\Config\ContentType\AdditionalData; |
| 68 | + |
| 69 | +/** |
| 70 | + * Provides runtime-specific data for additional data content types configuration |
| 71 | + */ |
| 72 | +interface ProviderInterface |
| 73 | +{ |
| 74 | + /** |
| 75 | + * Get data from the provider |
| 76 | + * @param string $itemName - the name of the item to use as key in returned array |
| 77 | + * @return array |
| 78 | + */ |
| 79 | + public function getData(string $itemName) : array; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +`getData` then returns an array that must contain the key of the configuration item you're providing. |
| 84 | + |
| 85 | +### Example use case |
| 86 | + |
| 87 | +In the `additional_data` XML config there is a `storeId` item with a `storeId` provider class, which must return `['storeId' => ...value here...]` to be integrated properly: |
| 88 | + |
| 89 | +``` |
| 90 | +<item name="storeId" xsi:type="object">\Magento\PageBuilder\Model\Config\ContentType\AdditionalData\Provider\StoreId</item> |
| 91 | +``` |
| 92 | + |
| 93 | +Custom configuration is injected into relevant content type block constructors, such as for the image block shown here, and accessed in `config.additional_data` within the content block type in `/app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/<your-content-type>/preview.js`: |
| 94 | + |
| 95 | +``` js |
| 96 | +var uploaderConfiguration = Object.assign( |
| 97 | + {}, |
| 98 | + config.additional_data.uploaderConfig, |
| 99 | + { |
| 100 | + value: this.dataStore.get().image, |
| 101 | + }, |
| 102 | +); |
| 103 | + |
| 104 | +// Create uploader |
| 105 | +this.uploader = new Uploader( |
| 106 | + this.id, |
| 107 | + "imageuploader_" + this.id, |
| 108 | + Object.assign({}, uploaderConfiguration, { |
| 109 | + value: this.dataStore.get().image, |
| 110 | + }), |
| 111 | + uploaderConfiguration, |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +<!-- {% endraw %} --> |
0 commit comments