|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +group: UI_Components_guide |
| 4 | +subgroup: how tos |
| 5 | +title: Declare a custom UI component |
| 6 | +menu_title: Declare a custom UI component |
| 7 | +menu_order: 1 |
| 8 | +version: 2.2 |
| 9 | +github_link: ui_comp_guide/howto/new_component_declaration.md |
| 10 | +--- |
| 11 | + |
| 12 | +Declaring a custom {% glossarytooltip 9bcc648c-bd08-4feb-906d-1e24c4f2f422 %}UI component{% endglossarytooltip %} refers to creating the {% glossarytooltip 8c0645c5-aa6b-4a52-8266-5659a8b9d079 %}XML{% endglossarytooltip %} configuration of your custom component, and is a part of a bigger task of creating a custom UI component. |
| 13 | + |
| 14 | +This topic describes the XML elements that must be used for declaring a custom component and where this declaration should be placed. |
| 15 | + |
| 16 | +## XML elements used for declaring the custom component |
| 17 | + |
| 18 | +A custom UI component can be declared using one of the following elements: `<component>` or `<container>`. |
| 19 | + |
| 20 | +The `<component>` element is typically used for declaring simple custom components that do not have other nested components. `<component>` uses the [uiElement]({{page.baseurl}}ui_comp_guide/concepts/ui_comp_uielement_concept.html) constructor by default. |
| 21 | + |
| 22 | +The `<container>` element is typically used for declaring custom components that are collections, or in other words, can have nested components. `<container>` uses the [uiCollection]({{page.baseurl}}ui_comp_guide/concepts/ui_comp_uicollection_concept.html) constructor by default. |
| 23 | + |
| 24 | +### Attributes you can use |
| 25 | +The `<container>` and `<component>` elements have no mandatory attributes. The following optional attributes are available for both these elements: |
| 26 | + |
| 27 | +- `component`: link to the component's {% glossarytooltip 312b4baf-15f7-4968-944e-c814d53de218 %}JavaScript{% endglossarytooltip %} constructor. |
| 28 | +- `class`: link to the component's {% glossarytooltip bf703ab1-ca4b-48f9-b2b7-16a81fd46e02 %}PHP{% endglossarytooltip %} class. |
| 29 | +- `template`: link to component's `.html` template. |
| 30 | +- `provider`: link to component's data provider. |
| 31 | +- `sortOrder`: component's sort order |
| 32 | +- `displayArea`: the placeholder which defines the area for rendering the component in the {% glossarytooltip 73ab5daa-5857-4039-97df-11269b626134 %}layout{% endglossarytooltip %} file. |
| 33 | + |
| 34 | +<div class="bs-callout bs-callout-warning" markdown="1"> |
| 35 | + |
| 36 | +If the following elements are used inside `<container>` or `<component>`, they should be specified strictly in the following order: |
| 37 | + |
| 38 | +1. `<argument>` |
| 39 | +2. `<settings>` |
| 40 | +3. `<childComponent>` |
| 41 | + |
| 42 | +For the component configuration inside `<container>` and `<component>`, [use the "arbitrary" structure]({{page.baseurl}}ui_comp_guide/best-practices/semantic_config.html#info_structure_except). |
| 43 | +</div> |
| 44 | + |
| 45 | +## Declare a custom basic component |
| 46 | + |
| 47 | +If the custom component you create is a [basic UI component]({{page.baseurl}}ui_comp_guide/bk-ui_comps.html#general-structure) (like Form or Listing), you need to take the following steps to declare it: |
| 48 | + |
| 49 | +1. Specify the XML file with its configuration it in the page layout file in your module, as described in the [About XML сonfiguration of UI сomponents]({{page.baseurl}}ui_comp_guide/concepts/ui_comp_xmldeclaration_concept.html#about-the-layout-configuration-file-and-ui-component-declaration) topic. |
| 50 | +2. Declare the component in a separate `.xml` file using the `<container>` or `<component>` as parent node. |
| 51 | + |
| 52 | +## Declare a custom secondary component |
| 53 | + |
| 54 | +If the custom component you create is secondary (not a basic one), it is declared using `<container>` or `<component>` in the XML configuration file of the basic component. |
| 55 | + |
| 56 | +## Example: Creating a custom secondary (not basic) component |
| 57 | +Task: In the Customer form, replace the select field with a custom UI component. The custom component extends the Select component by adding a custom option that enables custom functionality. |
| 58 | + |
| 59 | +The Customer form configuration is defined in `<Magento_Customer_module_dir>/view/base/ui_component/customer_form.xml`. The default configuration of the select field we will extend is following: |
| 60 | + |
| 61 | +{%highlight xml%} |
| 62 | +<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> |
| 63 | + <argument name="data" xsi:type="array"> |
| 64 | + <fieldset name="customer"> |
| 65 | +... |
| 66 | + <field name="sendemail_store_id" formElement="select"> |
| 67 | + <argument name="data" xsi:type="array"> |
| 68 | + <item name="config" xsi:type="array"> |
| 69 | + <item name="source" xsi:type="string">customer</item> |
| 70 | + </item> |
| 71 | + </argument> |
| 72 | + <settings> |
| 73 | + <dataType>number</dataType> |
| 74 | + <label translate="true">Send Welcome Email From</label> |
| 75 | + <imports> |
| 76 | + <link name="value">${ $.provider }:data.customer.store_id</link> |
| 77 | + </imports> |
| 78 | + </settings> |
| 79 | + <formElements> |
| 80 | + <select> |
| 81 | + <settings> |
| 82 | + <options class="Magento\Store\Model\System\Store"/> |
| 83 | + </settings> |
| 84 | + </select> |
| 85 | + </formElements> |
| 86 | + </field> |
| 87 | + ... |
| 88 | + </fieldset> |
| 89 | + </argument> |
| 90 | +</form> |
| 91 | +{%endhighlight%} |
| 92 | + |
| 93 | +The custom component we create extends Select, and is also a simple component (does not have child components), so we use the `<component>` element for its declaration. To add the declaration, we need to create the `customer_form.xml` file in the custom module, and put it in the same location relative to the module directory as the original `customer_form.xml` resides. So the custom form configuration file will be: `<YourVendor_YourModule_dir>/view/base/ui_component/customer_form.xml`. |
| 94 | + |
| 95 | +We copy the `<field>` configuration, remove the `<select>` node and its configuration, and replace it with `<component>` and its configuration: |
| 96 | + |
| 97 | +{%highlight xml%} |
| 98 | +<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> |
| 99 | + <argument name="data" xsi:type="array"> |
| 100 | + <fieldset name="customer"> |
| 101 | + ... |
| 102 | + <field name="sendemail_store_id" formElement="my_select"> |
| 103 | + <argument name="data" xsi:type="array"> |
| 104 | + <item name="config" xsi:type="array"> |
| 105 | + <item name="source" xsi:type="string">customer</item> |
| 106 | + </item> |
| 107 | + </argument> |
| 108 | + <settings> |
| 109 | + <dataType>number</dataType> |
| 110 | + <label translate="true">Send Welcome Email From</label> |
| 111 | + <imports> |
| 112 | + <link name="value">${ $.provider }:data.customer.store_id</link> |
| 113 | + </imports> |
| 114 | + </settings> |
| 115 | + <formElements> |
| 116 | + <component name="my_select" |
| 117 | + component="path/to/the/custom/JS_component" |
| 118 | + template="path/to/the/custom/html_template"> |
| 119 | + <arguments name="data" xsi:type="array"> |
| 120 | + <item name="options" xsi:type="">Magento\Store \Model\System\Store</item> |
| 121 | + <item name="config" xsi:type="array"> |
| 122 | + <item name="customPropertyName" xsi:type="string">customPropertyValue</item> |
| 123 | + </item> |
| 124 | + </arguments> |
| 125 | + </component> |
| 126 | + </formElements> |
| 127 | + </field> |
| 128 | + |
| 129 | + ... |
| 130 | + </fieldset> |
| 131 | + </argument> |
| 132 | +</form> |
| 133 | +{%endhighlight%} |
| 134 | + |
0 commit comments