简体   繁体   English

如何使用 php [Magento 2] 在 cms_page_from 中添加字段

[英]How to add field in cms_page_from using php [Magento 2]

I am trying to add an input field per store on my CMS page form (the page where we create the CMS page), but the catch is that it needs to be dynamic, I want the input's to appear for each store instead of adding static fields.我正在尝试在我的 CMS 页面表单(我们创建 CMS 页面的页面)上为每个商店添加一个输入字段,但问题是它需要是动态的,我希望每个商店都显示输入而不是添加静态领域。

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

but to the cms page form.但是到cms页面表单。

Thank you for your time in advance!提前感谢您的时间!

Here is the solution!这是解决方案!

Step 1: Firstly, add the below given code in your form file( in this case cms_page_form.xml in the app\\code\\Vendor\\Extension\\view\\adminhtml\\ui_component folder:步骤 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>

Step 2: After the above step create Fieldset.php file in the app\\code\\Vendor\\Extension\\Ui\\Component\\Form folder and add the below given code:第 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();
        }
    }

Step 3: Lastly, after following the above steps clear cache.第 3 步:最后,按照上述步骤清除缓存。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM