如何使用 php [Magento 2] 在 cms_page_from 中添加字段
How to add field in cms_page_from using php [Magento 2]
我正在尝试在我的 CMS 页面表单(我们创建 CMS 页面的页面)上为每个商店添加一个输入字段,但要注意的是它需要是动态的,我希望每个商店都显示输入存储而不是添加静态字段。
类似于@Dhiren Vasoya 在这里所做的事情:https://magecomp.com/blog/magento-2-add-new-field-in-admin-user-create-form/
而是以cms页面形式。
提前感谢您抽出时间!
解决方法在这里!
第一步:
首先,将下面给定的代码添加到您的表单文件中(在本例中 cms_page_form.xml 在 app\code\Vendor\Extension\view\adminhtml\ui_component 文件夹中:
<fieldset name="dynamic_fieldset" class="Vendor\Extension\Ui\Component\Form\Fieldset">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Dynamic Fields</item>
<item name="sortOrder" xsi:type="number">1000</item>
</item>
</argument>
</fieldset>
第 2 步:完成上述步骤后,在 app\code\Vendor\Extension\Ui\Component\Form 文件夹中创建 Fieldset.php 文件并添加以下代码:
<?php
namespace Vendor\Extension\Ui\Component\Form;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Form\FieldFactory;
class Fieldset extends \Magento\Ui\Component\Form\Fieldset
{
protected $fieldFactory;
public function __construct(
ContextInterface $context,
array $components = [],
array $data = [],
FieldFactory $fieldFactory)
{
parent::__construct($context, $components, $data);
$this->fieldFactory = $fieldFactory;
}
public function getChildComponents()
{
$options = [
0 => [
'label' => 'Option 1',
'value' => 1
],
1 => [
'label' => 'Option 2',
'value' => 2
],
2 => [
'label' => 'Option 3',
'value' => 3
],
];
$fields = [
[
'label' => __('Label'),
'value' => __('Label Value'),
'formElement' => 'input',
],
[
'label' => __('Checkbox'),
'value' => __('0'),
'formElement' => 'checkbox',
],
[
'label' => __('Dropdown'),
'options' => $options,
'formElement' => 'select',
],
];
foreach ($fields as $key => $field) {
$fieldInstance = $this->fieldFactory->create();
$name = 'custom_field_' . $key;
$fieldInstance->setData(
[
'config' => $field,
'name' => $name
]
);
$fieldInstance->prepare();
$this->addComponent($name, $fieldInstance);
}
return parent::getChildComponents();
}
}
第三步:最后,按照上述步骤清除缓存。
cc: https://magecomp.com/blog/dynamically-add-fields-in-custom-admin-form-using-ui-component/
我正在尝试在我的 CMS 页面表单(我们创建 CMS 页面的页面)上为每个商店添加一个输入字段,但要注意的是它需要是动态的,我希望每个商店都显示输入存储而不是添加静态字段。
类似于@Dhiren Vasoya 在这里所做的事情:https://magecomp.com/blog/magento-2-add-new-field-in-admin-user-create-form/
而是以cms页面形式。
提前感谢您抽出时间!
解决方法在这里!
第一步: 首先,将下面给定的代码添加到您的表单文件中(在本例中 cms_page_form.xml 在 app\code\Vendor\Extension\view\adminhtml\ui_component 文件夹中:
<fieldset name="dynamic_fieldset" class="Vendor\Extension\Ui\Component\Form\Fieldset">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Dynamic Fields</item>
<item name="sortOrder" xsi:type="number">1000</item>
</item>
</argument>
</fieldset>
第 2 步:完成上述步骤后,在 app\code\Vendor\Extension\Ui\Component\Form 文件夹中创建 Fieldset.php 文件并添加以下代码:
<?php
namespace Vendor\Extension\Ui\Component\Form;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Form\FieldFactory;
class Fieldset extends \Magento\Ui\Component\Form\Fieldset
{
protected $fieldFactory;
public function __construct(
ContextInterface $context,
array $components = [],
array $data = [],
FieldFactory $fieldFactory)
{
parent::__construct($context, $components, $data);
$this->fieldFactory = $fieldFactory;
}
public function getChildComponents()
{
$options = [
0 => [
'label' => 'Option 1',
'value' => 1
],
1 => [
'label' => 'Option 2',
'value' => 2
],
2 => [
'label' => 'Option 3',
'value' => 3
],
];
$fields = [
[
'label' => __('Label'),
'value' => __('Label Value'),
'formElement' => 'input',
],
[
'label' => __('Checkbox'),
'value' => __('0'),
'formElement' => 'checkbox',
],
[
'label' => __('Dropdown'),
'options' => $options,
'formElement' => 'select',
],
];
foreach ($fields as $key => $field) {
$fieldInstance = $this->fieldFactory->create();
$name = 'custom_field_' . $key;
$fieldInstance->setData(
[
'config' => $field,
'name' => $name
]
);
$fieldInstance->prepare();
$this->addComponent($name, $fieldInstance);
}
return parent::getChildComponents();
}
}
第三步:最后,按照上述步骤清除缓存。
cc: https://magecomp.com/blog/dynamically-add-fields-in-custom-admin-form-using-ui-component/