1

I have create an attribute and I want to render an cms blocks as attribute values in Magento 2. Please do provide some go on it and I have done research about it but no clue so far. Thanks.

  • Refer this link. – Pratik Oza Jan 29 '18 at 15:52
  • Thanks for the update, but What I want is from admin product create section, I want to render all the cms blocks which the store has...I mean source to get all the collection from product attribute – Prathap Gunasekaran Jan 30 '18 at 6:04
1
<?php

namespace Commerceshop\Artifex\Setup;

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory /* For Attribute create  */;
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; 
    /* assign object to class global variable for use in other class methods */
    }

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

    /**
     * Add attributes to the eav/attribute
     */
    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'custom_product_tab');
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'custom_product_tab',/* Custom Attribute Code */
        [
            'group' => 'Product Details',/* Group name in which you want 
                                          to display your custom attribute */
            'type' => 'text',/* Data type in which formate your value save in database*/
            'backend' => '',
            'frontend' => '',
            'label' => 'Custom Product Tab', /* lablel of your attribute*/
            'input' => 'select',
            'class' => '',
            'source' => 'Commerceshop\Artifex\Model\Config\Source\Options',
                            /* Source of your select type custom attribute options*/
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                                /*Scope of your attribute */
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false
        ]
    );
    }
}

In source file you just need to have cms/block collection as follows.

<?php

namespace Commerceshop\Artifex\Model\Config\Source;

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

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource 
{ 
    /** 
     * Get all options 
     * 
     * @return array 
     */ 
    public function getAllOptions() 
    { 

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $tableName = $resource->getTableName('cms_block');
    $sql = $connection->select()->from(
             ["tn" => $tableName]
        );
    if(!$this->_options)
    {
        $result = $connection->fetchAll($sql);
                     $this->_options = array(
                    array(
                            'value' => '',
                            'label' => '',
                    )
            );
        $this->_options=[ ['label'=>'Select Options', 'value'=>'']];
        foreach($result as $s)
        {
            $this->_options[] = array('value' => $s['identifier'], 'label' => $s['title']);
        }
    }
    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;
    }
}
0

UpgradeData.php example with new catalog product EAV attribute with a source model as a Block list.

There is a native source module available for it:

'source' => \Magento\Catalog\Model\Category\Attribute\Source\Page::class,

Full UpgradeData script:

<?php

namespace [Vendor]\Catalog\Setup;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    private $eavSetupFactory;

    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.0', '<')) {
            $this->installAdditionCmsBlockAttribute($setup);
        }
        $setup->endSetup();
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @return void
     */
    public function installAdditionCmsBlockAttribute(ModuleDataSetupInterface $setup)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            ProductAttributeInterface::ENTITY_TYPE_CODE,
            'product_additional_cms_content',
            [
                'label' => 'Additional CMS Content',
                'type' => 'int',
                'input' => 'select',
                'visible' => true,
                'source' => \Magento\Catalog\Model\Category\Attribute\Source\Page::class,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'required' => false,
                'used_in_product_listing' => 1,
                'user_defined' => true,
                'sort_order' => 100
            ]
        );
    }
}

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.