2

I have this multiselect field, the data I get from a function and magento internally duplicates me the data at the time of showing them.

enter image description here

customer_form.xml

     <field name="mod_seasons">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Mod\Seasons\Model\Entity\Attribute\Source\Group</item>
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">varchar</item>
                <item name="formElement" xsi:type="string">multiselect</item>
                <item name="source" xsi:type="string">customer</item>
                <item name="sortOrder" xsi:type="number">800</item>
                <item name="label" xsi:type="string" translate="true">Seasons</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>

Mod\Seasons\Model\Entity\Attribute\Source\Group

class Group extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    protected $groupCollectionFactory;

    public function __construct(
        \Magento\Store\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory
    )
    {
        $this->groupCollectionFactory = $groupCollectionFactory->create();
    }

    public function getAllOptions()
    {
        return $this->groupCollectionFactory->toOptionArray();
    }
}

InstallData:

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

        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'mod_seasons');

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'mod_seasons',
            [
                'type' => 'text',
                'label' => __('Seasons'),
                'input' => 'multiselect',
                'source' => 'Gsp\Seasons\Model\Entity\Attribute\Source\Group',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'required' => 0,
                'visible' => 1,
                'user_defined' => 0,
                'system' => 0,
                'position' => 800
            ]
        );

        $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'gsp_seasons')
            ->setData('used_in_forms', ['adminhtml_customer'])
            ->save();

    }

UpgradeSchema:

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.1.0', '<')) {

            $setup->getConnection()->addColumn(
                $setup->getTable('store_group'),
                'season_add_days_delivery_date',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Seasons add days to delivery date',
                ]
            );

            $setup->getConnection()->addColumn(
                $setup->getTable('store_group'),
                'season_add_days_delivery_deadline',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Seasons add days to delivery deadline',
                ]
            );
        }
        $setup->endSetup();
    }

2 Answers 2

1
return $this->groupCollectionFactory->toOptionArray();

Hi please check this method is overwrite from \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource

1
  • From what I have seen it is not overwritten and the method comes from: \ Magento \ Store \ Model \ ResourceModel \ Group \ CollectionFactory
    – chdzma
    Jul 31, 2018 at 6:21
1

Are those value and the field created by? If yes then check your installData or updateData script. I think forgot to put

if(version_compare($context->getVersion(), 'x.x.x') < 0)

1
  • These are the different Stores, this method '$this-> groupCollectionFactory-> toOptionArray ()' returns the 3 stores correctly, but when it is displayed, it duplicates them with the same ID. Now I show install and upgrade methods
    – chdzma
    Jul 31, 2018 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.