8

I'm learning UI component.

I want to add custom section in product edit/add backend form so for that

I've created following files.

vendor/module/view/adminhtml/ui_component/product_form.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <fieldset name="mobile">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Time</item>
                    <item name="provider" xsi:type="string">product</item>
                    <item name="dataScope" xsi:type="string">data.product</item>
                    <item name="sortOrder" xsi:type="number">2</item>
                    <item name="collapsible" xsi:type="boolean">true</item>
                    <item name="opened" xsi:type="boolean">false</item>
                    <item name="ns" xsi:type="string">product_form</item>
                </item>
            </argument>
            <container name="monday_time_group">
                <argument name="data" xsi:type="array">
                    <item name="type" xsi:type="string">group</item>
                    <item name="config" xsi:type="array">
                        <item name="additionalClasses" xsi:type="string">admin__control-grouped-date</item>
                        <item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
                        <item name="label" xsi:type="string" translate="true">Monday</item>
                        <item name="required" xsi:type="boolean">false</item>
                        <item name="sortOrder" xsi:type="number">220</item>
                        <item name="breakLine" xsi:type="boolean">false</item>
                        <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                    </item>
                </argument>
                <field name="monday_design_from">
                    <argument name="data" xsi:type="array">
                        <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                        <item name="config" xsi:type="array">
                            <item name="label" xsi:type="string" translate="true">Monday</item>
                            <item name="additionalClasses" xsi:type="string">admin__field-date</item>
                            <item name="sortOrder" xsi:type="number">230</item>
                            <item name="dataType" xsi:type="string">string</item>
                            <item name="formElement" xsi:type="string">select</item>
                        </item>
                    </argument>
                </field>
                 <field name="monday_design_to">
                    <argument name="data" xsi:type="array">
                        <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                        <item name="config" xsi:type="array">
                            <item name="additionalClasses" xsi:type="string">admin__field-date</item>
                            <item name="sortOrder" xsi:type="number">240</item>
                            <item name="dataType" xsi:type="string">string</item>
                            <item name="formElement" xsi:type="string">select</item>
                        </item>
                    </argument>
                </field>
            </container>
        </fieldset>
    </form>

Vendor\Module\Model\Config\Source\TimeSetup.php

<?php
namespace Vendor\Module\Model\Config\Source;

class TimeSetup extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $_optionsData;

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => 'one', 'label' => __('one')],
                ['value' => 'two', 'label' => __('two')]
            ];
        }
        return $this->_options;
    }
    final public function toOptionArray()
    {
         return array(
            array('value' => 'one', 'label' => __('one')),
            array('value' => 'two', 'label' => __('two'))
         );
     }
}

It's showing in backend properly as I wanted but when try to save product it's not saving this field value.

Do I need to create Attribute "monday_time_group"? Is this proper way to do so? How can I get the value of this in frontend for Product?

UPDATE:

I've created attribute monday_time_group but still not working.

  • can anyone help? – Kaushal Suthar Aug 3 '17 at 7:10
  • Other fields values saved or not? – Suresh Chikani Aug 4 '17 at 10:50
  • @SHPatel: Other default Magento fields are working fine , Only these two fields values are not saved. – Kaushal Suthar Aug 4 '17 at 10:52
  • Have you created these attributes from backend? – Suresh Chikani Aug 4 '17 at 10:59
  • 1
    I'd try using a plugin. In di.xml you can put something like: <type name="Magento\Catalog\Model\ResourceModel\Product"> <plugin name="AddFilter" type="Namespace\Modulename\Model\ResourceModel\Product"/> Then use devdocs.magento.com/guides/v2.0/extension-dev-guide/… as reference to make and afterSave function and put logic in there to fetch the data and save it. Not perfect but should work. – Wolfe Aug 7 '17 at 22:19
6

Okay finally I've resolved this by myself here is the full code of my module...

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
</config>

Setup/InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    public function __construct(
        EavSetupFactory $eavSetupFactory
    )
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
          \Magento\Catalog\Model\Product::ENTITY,
          "testone",
          [
          'group' => "",
          'label' => "Test One",
          'is_html_allowed_on_front' => true,
          'default' => '1',
          'note' => '',
          'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
          'visible' => true,
          'required' => false,
          'user_defined' => false,
          'searchable' => false,
          'filterable' => false,
          'comparable' => false,
          'visible_on_front' => true,
          'visible_in_advanced_search' => false,
          'unique' => false,
          "frontend_class" => "",
          "used_in_product_listing" => true,
          "input" => "select",
          "type" => "varchar",
          "source" => "Vendor\Module\Model\Config\Source\TimeSetup",
          'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
          ]
      );
        $eavSetup->addAttribute(
          \Magento\Catalog\Model\Product::ENTITY,
          "testtwo",
          [
          'group' => "",
          'label' => "Test Two",
          'is_html_allowed_on_front' => true,
          'default' => '1',
          'note' => '',
          'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
          'visible' => true,
          'required' => false,
          'user_defined' => false,
          'searchable' => false,
          'filterable' => false,
          'comparable' => false,
          'visible_on_front' => true,
          'visible_in_advanced_search' => false,
          'unique' => false,
          "frontend_class" => "",
          "used_in_product_listing" => true,
          "input" => "select",
          "type" => "varchar",
          "source" => "Vendor\Module\Model\Config\Source\TimeSetup",
          'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
          ]
      );
    }
}

Model\Config\Source\TimeSetup.php

<?php
namespace Vendor\Module\Model\Config\Source;

class TimeSetup extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $_optionsData;

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => 'one', 'label' => __('one')],
                ['value' => 'two', 'label' => __('two')]
            ];
        }
        return $this->_options;
    }
    final public function toOptionArray()
    {
         return array(
            array('value' => 'one', 'label' => __('one')),
            array('value' => 'two', 'label' => __('two'))
         );
     }
}

view/adminhtml/ui_component/product_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="testingproduct">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Testing Group</item>
                <item name="provider" xsi:type="string">product</item>
                <item name="dataScope" xsi:type="string">data.product</item>
                <item name="sortOrder" xsi:type="number">2</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">false</item>
                <item name="ns" xsi:type="string">product_form</item>
            </item>
        </argument>
        <container name="testing_group">
            <argument name="data" xsi:type="array">
                <item name="type" xsi:type="string">group</item>
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">container</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
                    <item name="label" xsi:type="string" translate="true">Testing Group</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="number">220</item>
                    <item name="breakLine" xsi:type="boolean">false</item>
                    <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                </item>
            </argument>
            <field name="testone">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                    <item name="config" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">Testing Row</item>
                        <item name="sortOrder" xsi:type="number">230</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                    </item>
                </argument>
            </field>
             <field name="testtwo">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">240</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                    </item>
                </argument>
            </field>
        </container>
    </fieldset>
</form>

Using above code, Data saving properly, Only issues I'm facing now is that These attributes are appearing in General section as well in my created custom section which is "Testing Group" so as of now I've hide them from General section using CSS.

  • how to add a field to existing section ? for example i need to add a text field to product detail section. How it can be done? – jafar pinjar Nov 8 '18 at 14:03
2

From what I see here there is nothing special about your attributes.
They are regular ones.
You can simply add the attributes monday_design_from, monday_design_from and place them in a separate group.
I recommend you do that via code and not manually because I assume you will have logic on these attributes.

Here is an example on how you can do that: https://magento.stackexchange.com/a/162115/146

The only thing you need to change if you want your attributes to appear in a separate section is to add in the configuration array this

'group' => 'Time',
  • ♦ Thanks for the reply, Yes there is nothing special about attributes, I want to display them into custom section but two should be in the same group like we see in "Set New Design From" attribute in it there are two date box in the same row, I want to display my attribute in that way. There are total 16 attributes I want to create and two attributes will be in the same group of my custom section so there will be total 8 rows and in each row there will be one label and two dropdown attribute. I hope you understood my requirement, Let me know if you want to share me a screenshot of it... – Kaushal Suthar Aug 5 '17 at 18:37
0

Perfect solution available here:

Add new tab in product edit page in admin Magento 2

you can find solution for any version of magento2

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.