2

I have part of setup code in the setup/upgradeData.php to be like this :

 $dataSetup->addAttribute(
            'vendor', 'allowed_type', array(
                'group'             => 'General Information',
                'visible'           => true,
                'required'          => false,
                'global'            => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'type'              => 'text',
                'input'             => 'multiselect',

                'source'            => 'Test\Market\Model\Vendor\Attribute\Source\Offer',
                'backend'           => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'default'           => <some_of_values>,

                'label'             => 'Allowed Type',
                'sort_order'        => 300
            )
        );

then in the default part, I want to add value by calling model like this :

'default' => [VendorName]\[ModuleName]\Model\ClassName,

is it possible? if yes how can I write the class?

  • yes you can do it. – John Aug 31 '18 at 11:33
  • hi @John, could you explain and post it in the answer column :) ? – mrfizh Aug 31 '18 at 11:58
1

if you want to add options then you have to call your model class into 'source' like :

'source' => 'VendorName\ModuleName\Model\Options'

Ans inside this model like :

<?php

namespace VendorName\ModuleName\Model;

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

class Options extends AbstractSource
{
    public function getAllOptions()
    {
        return [
            'option1' => [
                'label' => 'Option 1',
                'value' => 'option1'
            ],
            'option2' => [
                'label' => 'Option 2',
                'value' => 'option2'
            ],
            'option3' => [
                'label' => 'Option 3',
                'value' => 'option3'
            ]
        ];
    }
}

Thanks

|improve this answer|||||
  • hi @Hardik, thanks for your answer, but it's only for displaying multiselect options by source model, then what I asked is, this is possible to set default selected value by calling function in the model :) ? – mrfizh Aug 31 '18 at 14:06
  • Yes i think you can do it using config.xml, Using this file you can set default value for your system config field – Hardik Patel Nov 30 '18 at 11:36

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.