module – Custom product attribute values ​​editable through the dashboard

I have created a module that installs and upgrades the custom product attribute programmatically. It works as expected, however, when displaying the attribute via the admin dashboard, I can not change the attribute options (my attribute values).

What is the right way to create a custom product attribute programmatically, so that its options can then be modified or expanded via the admin dashboard?

here is my InstallData.php:

namespace Shop  Attributes  Setup;

use Magento  Framework  Setup  InstallDataInterface;
use Magento  Framework  Setup  ModuleContextInterface;
use Magento  Framework  Setup  ModuleDataSetupInterface;

/ **
* @CodeCoverageIgnore
* /
InstallData class implements InstallDataInterface
{
/ **
* Eav installation plant
* @var EavSetupFactory
* /
private $ eavSetupFactory;

/ **
* Init
* @param EavSetupFactory $ eavSetupFactory
* /
public function __construct ( Magento  Eav  Setup  EavSetupFactory $ eavSetupFactory)
{
$ this-> eavSetupFactory = $ eavSetupFactory;
}

/ **
* {@inheritdoc}
* @SuppressWarnings (PHPMD.CyclomaticComplexity)
* @SuppressWarnings (PHPMD.ExcessiveMethodLength)
* @SuppressWarnings (PHPMD.NPathComplexity)
* /
Public function install (moduleDataSetupInterface $ setup, ModuleContextInterface $ context)
{
$ eavSetup = $ this-> eavSetupFactory-> create ();
$ eavSetup-> addAttribute (
 Magento  Catalog  Template  Product :: ENTITY,
& # 39; command switch & # 39 ;,
            [
                'group' => 'Attributes',
                'type' => 'varchar',
                'label' => 'Control Switch',
                'input' => 'multiselect',
                'source' => 'ShopAttributesModelAttributeSourceControlSwitch',
                'backend' => 'ShopAttributesModelAttributeBackendControlSwitch',
                'required' => false,
                'sort_order' => 50,
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'visible' => true,
                'is_html_allowed_on_front' => true,
                'visible_on_front' => true,
                'option' => [
                    'values' => [],
]])
}
}

And here is the Source file:





namespace Shop  Attributes  Model  Attribute  Source;

use Magento  Eav  Model  ResourceModel  Entity  Attribute  OptionFactory;
use Magento  Store  Model  StoreManagerInterface;
use Magento  Framework  App  ObjectManager;

ControlSwitch class extends  Magento  Eav  Model  Entity  Attribute  Source  AbstractSource
{

/ **
* @var StoreManagerInterface
* /
private $ storeManager;

/ **
* @var  Magento  Eav  Template  Resource_Template  Entity  Attribute  OptionFactory
* /
protected $ _attrOptionFactory;


public service __construct (
 Magento  Eav  Model  ResourceModel  Entity  Attribute  OptionFactory $ attrOptionFactory
) {
$ this -> _ attrOptionFactory = $ attrOptionFactory;
}

/ **
* Get a text for the value of the option
*
* @param string | integer $ value
* @back string | bool
* /
public function getOptionText ($ value)
{
foreach ($ this-> getAllOptions () as $ option) {
if ($ option['value'] == $ value) {
$ return option['label'];
}
}
returns false;
}

/ **
* Get all the options
* @return array
* /
public function getAllOptions ()
{
if (! $ this -> _ options) {
$ this -> _ options = [
                ['label' => __('Knob switch'), 'value' => 'knob switch'],
                ['label' => __('Switch'), 'value' => 'switch'],
                ['label' => __('Remote control'), 'value' => 'remote control'],
                ['label' => __('Touch Screen'), 'value' => 'touch screen'],
                ['label' => __('Pull string'), 'value' => 'pull string'],
                ['label' => __('Automatic shutter'), 'value' => 'automatic shutter'],
];
}
returns $ this -> _ options;
}

/ **
* Collect the columns for flat
*
* @return array
* /
public function getFlatColumns ()
{
$ columns = [];
$ attributeCode = $ this-> getAttribute () -> getAttributeCode ();
$ isMulti = $ this-> getAttribute () -> getFrontend () -> getInputType () ==> multiselect & # 39 ;;

$ type = $ isMulti?  Magento  Framework  DB  Ddl  Table :: TEXT_TYPE:  Magento  Framework  DB  Ddl  Table :: TYPE_INTEGER;
$ columns[$attributeCode] = [
            'type' => $type,
            'length' => $isMulti ? '255' : null,
            'unsigned' => false,
            'nullable' => true,
            'default' => null,
            'extra' => null,
            'comment' => $attributeCode . ' column',
        ];
if (! $ isMulti) {
$ columns[$attributeCode . '_value'] = [
                'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                'length' => 255,
                'unsigned' => false,
                'nullable' => true,
                'default' => null,
                'extra' => null,
                'comment' => $attributeCode . ' column',
            ];
}

returns $ columns;
}

/ **
* Collect indexes for dish
*
* @return array
* /
public function getFlatIndexes ()
{
$ indexes = [];

$ index = sprintf (# IDX_% s), strtoupper ($ this-> getAttribute () -> getAttributeCode ()));
$ index[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];

$ sortable = $ this-> getAttribute () -> getUsedForSortBy ();
if ($ sortable && $ this-> getAttribute () -> getFrontend () -> getInputType ()! = & # 39; multiselect & # 39;) {
$ index = sprintf (& # 39; IDX_% s_VALUE & # 39;; strtoupper ($ this-> getAttribute () -> getAttributeCode ()));

$ index[$index] = [
                'type' => 'index',
                'fields' => [$this->getAttribute()->getAttributeCode() . '_value'],
];
}

returns $ indexes;
}
}