1

Here is my etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="adminhtml_catalog_product_edit_prepare_form">
    <observer name="custom_product_fields" instance="Vendor\Module\Observer\CatalogProductEditPrepareForm" />
</event>

Here is my Observer/CatalogProductEditPrepareForm.php

  <?php

  namespace Vendor\Module\Observer; 
  use Magento\Framework\Event\ObserverInterface;

  class CatalogProductEditPrepareForm implements ObserverInterface
  {
protected $_coreRegistry;

/**
 * @param \Magento\Framework\Registry  $coreRegistry
 */
public function __construct(
    \Magento\Framework\Registry  $coreRegistry
) {
    $this->_coreRegistry = $coreRegistry;
}

/**
 * product from prepare event handler.
 * @param \Magento\Framework\Event\Observer $observer
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{  
    $form = $observer->getForm(); // $observer contain form object

    // get your group element in which you want to add custom filed
    $fieldset = $form->getElement('product-details'); 

    if ($fieldset) {

        // get current product if you want to use any data from product
        $product = $this->_coreRegistry->registry('product');
        $fieldset ->addField(
            'custom-field-1',
            'text',
            [
                'name' => 'custom-field-1',
                'label' => __('Custom Field 1'),
                'id' => 'custom-field-1'
            ]
        );

        $fieldset ->addField(
            'custom-field-2',
            'text',
            [
                'name' => 'custom-field-2',
                'label' => __('Custom Field 2'),
                'id' => 'custom-field-2'
            ]
        );

        // You can set any data in these elements for display default value
        $form->addValues(
            [
                'custom-field-1' => 'data according to you', // ex. $product->getName()
                'custom-field-2' => 'data according to you'
            ]
        );
     }
    return $this;
  }
 }

I am trying to add custom field into product details tab. seems my event observer is not working. Please anyone help on this. Thanks

Jafar Pinjar
  • 1,929
  • 7
  • 64
  • 139

1 Answers1

0

Im not getting why you are adding form with the Observer . What version of magento 2 do you have ? Even if you are able to add them that way, how can you save the form ?

You can follow up this link and add forms with uicomponent Add new tab in product edit page in admin Magento 2 , there are answers for magento 2.1 and 2.2 . The Webkul blog you are refering https://webkul.com/blog/add-custom-form-elements-product-addedit-form-specific-group-section-magneto2/ ;) dates since 2 June 2016 .

If you have a magento 2 older version and you insist that your code may work , then check the module is enable , make some dies and vardumps . But i suggest you to go with the uicomponents way.

Ylgen Guxholli
  • 2,585
  • 12
  • 20