Right way to create butons in uiComponent form in Magento 2
-
02-10-2020 - |
質問
I've created an uiComponent form but there is no save button. In every form coponent example there is no save buttons and all the examples with save button that I found are FULLY created in Magento 1 way (I mean they are all generated by a php block).
I created a normal button using code below which looks like save one but I can't make him save the form.
<item name="buttons" xsi:type="array">
<item name="save" xsi:type="array">
<item name="name" xsi:type="string">save</item>
<item name="label" xsi:type="string" translate="true">Save</item>
<item name="class" xsi:type="string">primary</item>
</item>
</item>
If there is no other way to make the savable form as UI Component then what's the point of their partial existance?
解決
You can declare buttons inside the
<argument name="data" xsi:type="array">
tag as this
<item name="buttons" xsi:type="array">
<item name="back" xsi:type="string">[Namespace]\[Module]\Block\Adminhtml\Button\Back</item>
<item name="delete" xsi:type="string">[Namespace]\[Module]\Block\Adminhtml\Button\Delete</item>
<item name="save" xsi:type="string">[Namespace]\[Module]\Block\Adminhtml\Button\Save</item>
</item>
You can have additional buttons. As many as you want.
Each class declared inside the 'item' tag must implement the interface Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface
.
in the method getButtonData
you define how the button looks or behaves
For example, for the back button it can look like this:
public function getButtonData()
{
return [
'label' => __('Back'),
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
'class' => 'back',
'sort_order' => 10
];
}
A more detailed example can be found here