3

I'm trying to add a new custom field in product form using Ui Component so I've created the install script, My/Module/Setup/InstallData.php:

<?php

namespace My\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory /* For Attribute create  */;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $_eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $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->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'file_uploader_attribute', /* File Uploader Attribute */
            [
                'type' => 'varchar',
                'backend' => '',
                'label' => 'File Uploader Attribute (Filename)',
                'input' => 'file',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' => false,
                'required' => false,
                'visible_on_front' => true
            ]
        );
    }
}

and the product Ui Component, My/Module/view/adminhtml/ui_component/product_form.xml:

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="file_uploader_attribute_fieldset">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">More Product Links</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="string">2</item>
                <item name="canShow" xsi:type="boolean">true</item>
                <item name="componentType" xsi:type="string">fieldset</item>
            </item>
        </argument>
        <field name="product[file_uploader_attribute]">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">file</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">product[file_uploader_attribute]</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

It has created new eav attribute into db and it has added element into HTML product form, all correctly.

The only issue is that the HTML element has not name attribute (see image) so the post request doesn't send the field value.

enter image description here

If I force in the code to set name attribute all works fine.

How can I set name attribute by Ui Component?

4

Field name should be file_uploader_attribute, not product[file_uploader_attribute].

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="file_uploader_attribute_fieldset">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">More Product Links</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="string">2</item>
                <item name="canShow" xsi:type="boolean">true</item>
                <item name="componentType" xsi:type="string">fieldset</item>
            </item>
        </argument>
        <field name="file_uploader_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">file</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">product[file_uploader_attribute]</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

See: Magento2 DevDoc: Customize product creation form

  • I've already tried without success.. The product form has input file element without "name" attribute. – WaPoNe Jan 31 '17 at 7:56
  • I'm sorry I did not notice that it has blank name attribute. The best solution for now is to modify your product form using modifier pool. – Rendy Eko Prastiyo Jan 31 '17 at 8:28

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.