5

Ho can I use a custom source in the source_model of the eav_attribute table? Thats my source

* 
* @category  Mcas
* @package   Mcas_Industries
* @copyright Copyright (c) 2019
* @license   http://opensource.org/licenses/mit-license.php MIT License
*/
namespace Mcas\Industries\Source;

class Industry implements \Magento\Framework\Option\ArrayInterface
{
/**
 * Industry repository
 * 
 * @var \Mcas\Industries\Api\IndustryRepositoryInterface
 */
protected $industryRepository;

/**
 * Search Criteria Builder
 * 
 * @var \Magento\Framework\Api\SearchCriteriaBuilder
 */
protected $searchCriteriaBuilder;

/**
 * Filter Builder
 * 
 * @var \Magento\Framework\Api\FilterBuilder
 */
protected $filterBuilder;

/**
 * Options
 * 
 * @var array
 */
protected $options;

/**
 * constructor
 * 
 * @param \Mcas\Industries\Api\IndustryRepositoryInterface $industryRepository
 * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
 * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
 */
public function __construct(
    \Mcas\Industries\Api\IndustryRepositoryInterface $industryRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Framework\Api\FilterBuilder $filterBuilder
) {
    $this->industryRepository    = $industryRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder         = $filterBuilder;
}

/**
 * Retrieve all Industries as an option array
 *
 * @return array
 * @throws StateException
 */
public function getAllOptions()
{
    if (empty($this->options)) {
        $options = [];
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $searchResults = $this->industryRepository->getList($searchCriteria);
        foreach ($searchResults->getItems() as $industry) {
            $options[] = [
                'value' => $industry->getIndustryId(),
                'label' => $industry->getIndustry(),
            ];
        }
        $this->options = $options;
    }

    return $this->options;
}

/**
 * @return array
 */
public function toOptionArray()
{
    return $this->getAllOptions();
}
}
1

Add Source Model When You Create Attribute

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       $eavSetup->addAttribute(
               \Magento\Catalog\Model\Product::ENTITY,
           'international',
           [
               'group' => 'General',
               'type' => 'int',
               'label' => 'International',
               'backend' => '',
               'input' => 'select',
               'wysiwyg_enabled'   => false,
               'source' => 'Namespace\ModuleName\Model\Config\Source\YesNo',
               'required' => true,
               'sort_order' => 15,
               'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
               'used_in_product_listing' => false,
               'visible_on_front' => false,
       ]
   );
  • I created a dropdown attribute in the admin, then I have replaced the source_model value on the eav_atttribute table. In the past it has worked, but now I get a blank page. I dont understand why. – Dr. Christian Kusche Mar 1 at 13:59
  • i think the attribute can not get any options – Rk Rathod Mar 1 at 14:13
  • I dont understand why it doesnt work. In 'eav_attribute', there are 'backend_model' and 'source_model'. Backend_model is empty, in 'source_model' I have placed my source 'Mcas\Industries\Source\Industry'. In my custom modules it is working, but when I call the 'product edit page' I get a blank screen – Dr. Christian Kusche Mar 2 at 4:15

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.