2

How to add a custom field in product_form.xml, or rather, to the 'Search Engine Optimization' section. enter image description here

  • Any news about your issue? Check my answer bellow, I think is the best way to get it. Hope it helps – lpFranz Mar 14 '18 at 11:51
1

It is some special when create product form. Magento did not use XML file to create form. They have used "Modifier Class" to create them. You can find some construction inside Magento\Catalog\Ui\DataProvider\Product\Form\Modifier folder. If you just want to add a new text field inside product_form. Add the following code:

Create SearchEngineCustomization class in Vendor\Module\Ui\DataProvider\Product\Form\Modifier and add the code below:

<?php
namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;

class SearchEngineCustomization extends AbstractModifier
{
    public function modifyMeta(array $meta) {
        $meta = [
            'search-engine-customization' => [
                'children' => [
                    'container_custom_field' => [
                        'arguments' => [
                            'data' => [
                                'config' => [
                                    'label' => __('Custom Field'),
                                    'required' => 0,
                                    'componentType' => 'container',
                                    'formElement' => 'container',
                                    'sortOrder' => 0
                                ]
                            ]
                        ],
                        'children' => [
                            'custom_field' => [
                                'argements' => [
                                    'data' => [
                                        'config' => [
                                            'dataType' => 'text',
                                            'formElement' => 'input',
                                            'visible' => 1,
                                            'label' => 'Custom Field',
                                            'componentType' => 'field',
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
        return $meta;
    }

    public function modifyData(array $data) {
        return $data;
    }
}

After modify the class, add it to your product_form by adminhtml\di.xml:

<type name="Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider">
    <arguments>
        <argument name="pool" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool</argument>
    </arguments>
</type>
<virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="search-engine-customization" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Product\Form\Modifier\SearchEngineCustomization</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
    </arguments>
</virtualType>

Hope it useful!

| improve this answer | |
  • Many thanks, but there's something missing in your code. I receive an error: "The configuration parameter "componentType" is a required for "search-engine-customization" component." – Artem M. Mar 14 '18 at 7:54
  • I have tired this code, but the values are not saved in the database. Do I have to create the fields manually in the database? – W.M. Nov 23 '19 at 16:30
0

I have created a new filed just in there, in SEO group, but I did it with installData script of my module, in other words, I use the existent magento EAV, so I've created a new attribute to the product, exactly the same way you can do manually on admin panel, I just don't know how to set your new attribute to "SEO" Group in product form when it is manually created, but in code, is like this: app/code/YourVendor/YurModule/Setup/installData.php

class InstallData implements InstallDataInterface {

private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute
     */
    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'your_filed_code');
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'your_filed_code',
        [
            //here you set the group, in this case this new field will apper on "SEO" group
            'group' => 'Search Engine Optimization',
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Your Field Name',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => false,
            'filterable' => true,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );
  }
}

And then you change the Your Field Name to your field name and your_filed_code to your field code. This script will run when you install your module, with the comand bin/magento setup:upgrade and will adds your custom field on your products form. I hope it helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.