To remove the attrbute add below code in your module UpgradeData.php
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'your_attribute_code');
You will need to add source model to render data from custom model here's the sample code to add dropdown attribute:
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'your_attribute_code',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Product Attribute Label',
'input' => 'boolean',
'class' => '',
'source' => 'Vendor\Module\Model\Config\Source\Options',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => ''
]
);
Create Vendor/Module/Model/Config/Source/Options.php
file and add below code:
namespace Vendor\Module\Model\Config\Source;
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
public $manufacturer;
public function __construct(
\Vendor\Module\Model\ResourceModel\Manufacturer\Collection $manufacturer
) {
$this->manufacturer = $manufacturer;
}
public function getAllOptions()
{
// $this->_options = [ ['label' => __('Test'), 'value'=>'0'] ];
$arr = [];
if (null === $this->_options) {
foreach($this->manufacturer as $manufacturer) {
$arr[] = [
'label' => __($manufacturer->getName()),
'value' => $manufacturer->getId()
];
}
$this->_options = $arr;
}
return $this->_options;
}
}
Hope This Help!
You can do this using js
in your UI component form add this feild
<field name="color">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="disabled" xsi:type="boolean">false</item>
<item name="conponent" xsi:type="boolean">Vendor_ModuleName/js/form/element/options</item>
<item name="label" xsi:type="string" translate="true">Color</item>
<item name="formElement" xsi:type="string">select</item>
<item name="enabled" xsi:type="boolean">false</item>
<item name="source" xsi:type="string">Magento\Catalog\Model\Product\AttributeSet\Options</item>
<item name="dataScope" xsi:type="string">color</item>
</item>
</argument>
</field>
Hope it will Help You
in your
Vendor\Module\Model\Size.php
<?php
namespace Vendor\Module\Model;
class Size implements ConfigProviderInterface
{
protected $scopeConfig;
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}
public function getConfig()
{
\\ Get Your Data from Backend and form an array
$config =[
[
'label'=>'Large',
'value'=> '36'
],
[
'label'=>'Medium',
'value'=> '32'
],
[
'label'=>'Small',
'value'=> '32'
]
];
return $config;
}
}
now in
app/code/Vendor/ModuleName/etc/adminhtml/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="size_config_provider" xsi:type="object">Vendor\Modulename\Model\Size</item>
</argument>
</arguments>
</type>
</config>
now in you
Vendor\Module\view\adminhtml\web\js\options.js
Add this Code
define([
'underscore',
'uiRegistry',
'Magento_Ui/js/form/element/select',
'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
'use strict';
return select.extend({
initialize: function () {
var value=this._super().initialValue;
console.log('Selected Value: ' + value);
var color= uiRegistry.get('index = color');
var size= uiRegistry.get('index = size');
var arrayValues1=window.size_config_provider;
if (color=='blue' ) {
size.append(new Option(arrayValues1));
} else {
size.append(new Option(arrayValues1));
}
return this._super();
},
onUpdate: function (value) {
console.log('Selected Value: ' + value);
var color= uiRegistry.get('index = color');
var size= uiRegistry.get('index = size');
var arrayValues1=window.size_config_provider;
if (color=='blue' ) {
size.append(new Option(arrayValues1));
} else {
size.append(new Option(arrayValues1));
}
return this._super();
},
});
});
Best Answer
I guess you would like to render the Manufacturer options in the attribute dropdown from a custom Manufacturer table?
If yes, You have to write the UpgradeData schema for your module and update the existing Manufacturer source model to your custom source model class. Finally, Your source model class will look like: