【问题标题】:How to add field in cms_page_from using php [Magento 2]如何使用 php [Magento 2] 在 cms_page_from 中添加字段
【发布时间】:2021-11-08 17:53:10
【问题描述】:

我正在尝试在我的 CMS 页面表单(我们创建 CMS 页面的页面)上为每个商店添加一个输入字段,但问题是它需要是动态的,我希望每个商店都显示输入添加静态字段。

类似于@Dhiren Vasoya 在这里所做的事情:https://magecomp.com/blog/magento-2-add-new-field-in-admin-user-create-form/

但到 cms 页面表单。

提前感谢您的宝贵时间!

【问题讨论】:

    标签: php magento magento2 mage


    【解决方案1】:

    这是解决方案!

    第 1 步: 首先,在您的表单文件中添加以下给定代码(在本例中为 app\code\Vendor\Extension\view\adminhtml\ui_component 文件夹中的 cms_page_form.xml:

    <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();
            }
        }
    

    第 3 步:最后,按照上述步骤清除缓存。

    抄送:https://magecomp.com/blog/dynamically-add-fields-in-custom-admin-form-using-ui-component/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多