Magento2: add custom dropdown to product edit form

2.9k Views Asked by At

I want to add a new dropdown to the product edit form in magento2. The data should come from a custom table I created in a custom module. How do I do this, is there a good example or tutorial out there?

I tried to create an observer, like this:

<?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_attribute_edit_prepare_form">
    <observer name="custom_product_fields" instance="Vendorname\Custom\Observer\CatalogProductEditPrepareForm"/>
    <!-- CatalogProductEditPrepareForm is name of class in which we'll add custom fields in form-->
  </event>
</config>

And the Observer looks like this:

class CatalogProductEditPrepareForm implements ObserverInterface
{
  protected $_coreRegistry;

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

  public function execute(\Magento\Framework\Event\Observer $observer){
    echo "why is this not called?!";
  }
  ...
}

Thanks!

1

There are 1 best solutions below

0
On

You have create attribute with InstallData.php file in custom extension, Code is here.

<?php
namespace Vendor\Extension\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;
use Magento\Framework\Module\Setup\Migration;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;


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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        // Product Attribute
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'attribute_id',
            [
                'type' => 'varchar',
                'label' => 'Attribute Label',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'input' => 'select',
                'source' => 'Vendor\Extension\Model\Source\Mysource',
                'required' => false,
                'sort_order' => 6,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'searchable'        => false,
                'filterable'        => false
            ]
        );
        $setup->endSetup();
    }
}

Above code create product attribute, and you need to create custom source file here,

Vendor\Extension\Model\Source\Mysource.php

<?php
namespace Vendor\Extension\Model\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Mysource extends AbstractSource
{
     public function getOptionArray()
    {
        return [
            ['value' => 1, 'label'=>__('Label-1')],
            ['value' => 2, 'label'=>__('Label-2')],
        ];
    }
}