1

I created custom eav attribute and set in custom eav type. This is my attribute code :

$attributes['custom_country'] = [
            'group' => 'General',
            'type' => 'int',
            'label' => 'Country',
            'input' => 'select',
            'source' => \Vendor\Module\Model\ResourceModel\Attribute\Source\Country::class,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'required' => '0',
            'user_defined' => false,
            'default' => '',
            'unique' => false,
            'position' => '50',
            'note' => '',
            'visible' => '1',
            'wysiwyg_enabled' => '0',
        ];

And this is resourcemodel/source file code :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Model\ResourceModel\Attribute\Source;

class Custom extends \Magento\Eav\Model\Entity\Attribute\Source\Table {
    /**
     * @var \Magento\Eav\Model\Config
     */
    protected $eavConfig;

    /**
     * [__construct description]
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory [description]
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory            $attrOptionFactory           [description]
     * @param \Magento\Eav\Model\Config                                                  $eavConfig                   [description]
     */
    public function __construct(
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
        \Magento\Eav\Model\Config $eavConfig
    ) {
        $this->eavConfig = $eavConfig;
        parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
    }

    public function getAllOptions($withEmpty = true, $defaultValues = false) {
        $attribute = $this->eavConfig->getAttribute('custom_eav_entity', 'attr_code');
        return $attribute->getSource()->getAllOptions($withEmpty,$defaultValues); // here returns error
    }

    public function getOptionsArray($withEmpty = true) {
        $options = [];
        foreach ($this->getAllOptions($withEmpty) as $option) {
            $options[$option['value']] = $option['label'];
        }
        return $options;
    }
}

Error Screenshot :

enter image description here

How can I solve this error ?

Any help would be appreciated.

Thanks.

M1 code (which is working) :

<?php
/**
 * Vendor_Module extension
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 * 
 * @category       Vendor
 * @package        Vendor_Module
 * @copyright      Copyright (c) 2014
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 */
/**
 * Admin source model for Socket
 *
 * @category    Vendor
 * @package     Vendor_Module
 * @author      Ultimate Module Creator
 */
class Vendor_Module_Model_CustomModel_Attribute_Source_CustomCountry
    extends Mage_Eav_Model_Entity_Attribute_Source_Table {
    /**
     * get possible values
     * @access public
     * @param bool $withEmpty
     * @param bool $defaultValues
     * @return array
     * @author Ultimate Module Creator
     */
    public function getAllOptions($withEmpty = true, $defaultValues = false){
        $source  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attr_code');
        return $source->getSource()->getAllOptions($withEmpty, $defaultValues);
    }
    /**
     * get options as array
     * @access public
     * @param bool $withEmpty
     * @return string
     * @author Ultimate Module Creator
     */
    public function getOptionsArray($withEmpty = true) {
        $options = array();
        foreach ($this->getAllOptions($withEmpty) as $option) {
            $options[$option['value']] = $option['label'];
        }
        return $options;
    }
}
  • can you paste the controller class? – Avesh Naik Apr 22 at 11:52
  • which controller? – Rohan Hapani Apr 22 at 11:53
  • The controller which you fired, from which this error is showing. – Avesh Naik Apr 22 at 11:57
  • It's just index controller. Error display from this file which i mentioned here. – Rohan Hapani Apr 22 at 12:04
  • I think its something to do with controller. – Avesh Naik Apr 22 at 12:15

Your Answer

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

Browse other questions tagged or ask your own question.