I've registered a custom select option to my categories using the files below in my module. The select option appears in the backend with my custom options however it doesn't save the selected option when you save the categories?
Could anyone please advise whether I am missing anything to get this to save or advise how I can get the below to work?
app/code/[vendor]/[module]/Setup/UpgradeData.php
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
private $eavSetupFactory;
public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '9.0.1', '<')) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'category_icon_select',
[
'group' => 'General',
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Select Category Icon',
'input' => 'select',
'class' => '',
'source' => 'Vendor\Module\Model\Config\Source\Options',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false
]);
}
$setup->endSetup();
}
}
app/code/[vendor]/[module]/view/adminhtml/ui_component/category_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="category_fields">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Additional Settings</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
<field name="category_icon_select">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\Options</item>
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">60</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Category Icon</item>
</item>
</argument>
</field>
</fieldset>
</form>
app/code/[vendor]/[module]/Model/Config/Source/Options.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model\Config\Source;
/**
* Catalog category landing page attribute source
*
* @author Magento Core Team <[email protected]>
*/
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
['value' => 'car', 'label' => __('Car')],
['value' => 'truck', 'label' => __('Truck')],
['value' => 'van', 'label' => __('Van')],
];
}
return $this->_options;
}
/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
'car' => __('Car'),
'truck' => __('Truck'),
'van' => __('Van'),
];
}
}