1

I am creating custom attribute of type select, its creating but not picking values from defined source, instead its showing 'yes/no' option to select .

Here is mine code in Setup/InstallData.php

   namespace Mymodule\CatAttributeSet\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

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

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){



        $eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'cat_attribute_set');


            $eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'cat_attribute_set', [
                        'type' => 'int',
                        'label' => 'Associated Attribute set',
                        'input' => 'select',
                        'source' => 'Mymodule\CatAttributeSet\Model\Config\Source\AttributesetOptions',
                        'required' => true,
                        'sort_order' => 110,
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'group' => 'General Information',
                        "note"       => "It is using on frontend while seller creating products."
            ]
            );

        }

    }
}

While I am trying to debug and apply the print statement in defined source model than its printing my source model values but in admin category detail page its giving option "Yes/No" in my custom attribute to select.

Here is my source model file's code -

    namespace Mymodule\CatAttributeSet\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;


class AttributesetOptions extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * @var OptionFactory
     */
    protected $optionFactory;

    /**
     * @param OptionFactory $optionFactory
     */
    public function __construct(OptionFactory $optionFactory)
    {
        $this->optionFactory = $optionFactory;
        //you can use this if you want to prepare options dynamically
        //$coll = $objectManager->create(\Magento\Catalog\Model\Product\AttributeSet\Options::class);
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        /* your Attribute options list*/

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $coll = $objectManager->create(\Magento\Catalog\Model\Product\AttributeSet\Options::class);

        $this->_options=[ ['label'=>'Select Attribute Set', 'value'=>'']];

        foreach($coll->toOptionArray() as $d){
            if($d['label'] !== 'Default') {
                $this->_options[] = ['label' => $d['label'], 'value' => $d['value']];
            }
        }
        //echo "<pre>"; print_r($this->_options); die;
        return $this->_options;
    }

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


}

Please let me know what I am doing wrong. My magento version is - 2.1.6 .

Thanks

  • Have you created AttributesetOptions.php file? can you please share code? – Abdul May 28 '18 at 6:31
  • Yes...As I mention above, when I am applying "echo "<pre>"; print_r($this->_options); die;" in that file its printing required array.... – Atul May 28 '18 at 6:35
  • can you please share code? – Abdul May 28 '18 at 6:36
  • I have updated the my question and added "AttributesetOptions.php" files code ....Please check – Atul May 28 '18 at 6:43
  • Ya it solved- Issue was in Mymodule/CatAttributeSet/view/adminhtml/ui_component/category_form.xml.... thx @Abdul – Atul May 29 '18 at 10:32
0

Try this code in your AttributesetOptions.php file

<?php
namespace Mymodule\CatAttributeSet\Model\Config\Source;

class AttributesetOptions extends \Magento\Eav\Model\Entity\Attribute\Source\Table
{
     /**
     * @var \Magento\Framework\Convert\DataObject
     */
    protected $_converter;

    /**
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
     * @param \Magento\Framework\Convert\DataObject $converter
     */
    public function __construct(
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
        \Magento\Framework\Convert\DataObject $converter
    ) {
        $this->_converter = $converter;
        parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        /* your Attribute options list*/

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $coll = $objectManager->create(\Magento\Catalog\Model\Product\AttributeSet\Options::class);
        $this->_options[] = ['value' => ' ', 'label' => __('Select Attribute Set')];  
        foreach($coll->toOptionArray() as $d){
            if($d['label'] !== 'Default') {
                $this->_options[] = ['label' => $d['label'], 'value' => $d['value']];
            }
        }
        return $this->_options;
    }

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


}

After run below commands:

php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
  • Okay let me try... – Atul May 28 '18 at 6:55
  • Still same ...., I have updated my question now you can see InstallData.php file full code ... – Atul May 28 '18 at 7:03
0

Please try this,InstallData.php

<?php
namespace Category\Attributes\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

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

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'school_name',
            [
                'type' => 'int',
                'label' => 'Name',
                'input' => 'select',
                'sort_order' => 333,
                'source' => 'Mymodule\CatAttributeSet\Model\Config\Source\AttributesetOptions',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
            ]
        );
    }
}

Source model file,

Mymodule\CatAttributeSet\Model\Config\Source\AttributesetOptions.php

namespace Category\Attributes\Model\Category\Attribute\Source;
use Namespace\Modulename\Model\Name;



class Name extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $_optionsData;

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct($options,CreateFactory $modelCreateFactory)
    {
        $this->_modelCreateFactory = $modelCreateFactory;
        $this->_optionsData = $options;
    }

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {

        $model = $this->_modelCreateFactory->create();
        $data = $school_model->getCollection()->getData();
        $value1 = array();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $collection = $objectManager->create('Namespace\Modulename\Model\Name');
        $option = $collection->getCollection();
        $optionsArray =[];
        foreach($option as $optionValues) { 
          $optionsArray [] = [
            'label' =>  $optionValues['name'],
            'value' => $optionValues['id']
        ];
        }
        return $optionsArray;
    }
}

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.